ocfs2: Change the recovery map to an array of node numbers.
[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
45 #include "buffer_head_io.h"
46
47 static inline void __ocfs2_node_map_set_bit(struct ocfs2_node_map *map,
48                                             int bit);
49 static inline void __ocfs2_node_map_clear_bit(struct ocfs2_node_map *map,
50                                               int bit);
51
52 /* special case -1 for now
53  * TODO: should *really* make sure the calling func never passes -1!!  */
54 static void ocfs2_node_map_init(struct ocfs2_node_map *map)
55 {
56         map->num_nodes = OCFS2_NODE_MAP_MAX_NODES;
57         memset(map->map, 0, BITS_TO_LONGS(OCFS2_NODE_MAP_MAX_NODES) *
58                sizeof(unsigned long));
59 }
60
61 void ocfs2_init_node_maps(struct ocfs2_super *osb)
62 {
63         spin_lock_init(&osb->node_map_lock);
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
87 /* Called from the dlm when it's about to evict a node. We may also
88  * get a heartbeat callback later. */
89 static void ocfs2_dlm_eviction_cb(int node_num,
90                                   void *data)
91 {
92         struct ocfs2_super *osb = (struct ocfs2_super *) data;
93         struct super_block *sb = osb->sb;
94
95         mlog(ML_NOTICE, "device (%u,%u): dlm has evicted node %d\n",
96              MAJOR(sb->s_dev), MINOR(sb->s_dev), node_num);
97
98         ocfs2_do_node_down(node_num, osb);
99 }
100
101 void ocfs2_setup_hb_callbacks(struct ocfs2_super *osb)
102 {
103         /* Not exactly a heartbeat callback, but leads to essentially
104          * the same path so we set it up here. */
105         dlm_setup_eviction_cb(&osb->osb_eviction_cb,
106                               ocfs2_dlm_eviction_cb,
107                               osb);
108 }
109
110 void ocfs2_stop_heartbeat(struct ocfs2_super *osb)
111 {
112         int ret;
113         char *argv[5], *envp[3];
114
115         if (ocfs2_mount_local(osb))
116                 return;
117
118         if (!osb->uuid_str) {
119                 /* This can happen if we don't get far enough in mount... */
120                 mlog(0, "No UUID with which to stop heartbeat!\n\n");
121                 return;
122         }
123
124         argv[0] = (char *)o2nm_get_hb_ctl_path();
125         argv[1] = "-K";
126         argv[2] = "-u";
127         argv[3] = osb->uuid_str;
128         argv[4] = NULL;
129
130         mlog(0, "Run: %s %s %s %s\n", argv[0], argv[1], argv[2], argv[3]);
131
132         /* minimal command environment taken from cpu_run_sbin_hotplug */
133         envp[0] = "HOME=/";
134         envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
135         envp[2] = NULL;
136
137         ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
138         if (ret < 0)
139                 mlog_errno(ret);
140 }
141
142 static inline void __ocfs2_node_map_set_bit(struct ocfs2_node_map *map,
143                                             int bit)
144 {
145         set_bit(bit, map->map);
146 }
147
148 void ocfs2_node_map_set_bit(struct ocfs2_super *osb,
149                             struct ocfs2_node_map *map,
150                             int bit)
151 {
152         if (bit==-1)
153                 return;
154         BUG_ON(bit >= map->num_nodes);
155         spin_lock(&osb->node_map_lock);
156         __ocfs2_node_map_set_bit(map, bit);
157         spin_unlock(&osb->node_map_lock);
158 }
159
160 static inline void __ocfs2_node_map_clear_bit(struct ocfs2_node_map *map,
161                                               int bit)
162 {
163         clear_bit(bit, map->map);
164 }
165
166 void ocfs2_node_map_clear_bit(struct ocfs2_super *osb,
167                               struct ocfs2_node_map *map,
168                               int bit)
169 {
170         if (bit==-1)
171                 return;
172         BUG_ON(bit >= map->num_nodes);
173         spin_lock(&osb->node_map_lock);
174         __ocfs2_node_map_clear_bit(map, bit);
175         spin_unlock(&osb->node_map_lock);
176 }
177
178 int ocfs2_node_map_test_bit(struct ocfs2_super *osb,
179                             struct ocfs2_node_map *map,
180                             int bit)
181 {
182         int ret;
183         if (bit >= map->num_nodes) {
184                 mlog(ML_ERROR, "bit=%d map->num_nodes=%d\n", bit, map->num_nodes);
185                 BUG();
186         }
187         spin_lock(&osb->node_map_lock);
188         ret = test_bit(bit, map->map);
189         spin_unlock(&osb->node_map_lock);
190         return ret;
191 }
192