blob: 1978c9cff0e9c6910bf260233aec2c4ec32c4807 [file] [log] [blame]
Joel Becker24ef1812008-01-29 17:37:32 -08001/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * stackglue.c
5 *
6 * Code which implements an OCFS2 specific interface to underlying
7 * cluster stacks.
8 *
9 * Copyright (C) 2007 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, version 2.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 */
20
Joel Becker286eaa92008-02-01 15:03:57 -080021#include <linux/list.h>
22#include <linux/spinlock.h>
23#include <linux/module.h>
Joel Becker4670c462008-02-01 14:39:35 -080024#include <linux/slab.h>
Joel Becker6953b4c2008-01-29 16:59:56 -080025#include <linux/kmod.h>
Joel Becker4670c462008-02-01 14:39:35 -080026
Joel Becker24ef1812008-01-29 17:37:32 -080027#include "stackglue.h"
28
Joel Becker286eaa92008-02-01 15:03:57 -080029static struct ocfs2_locking_protocol *lproto;
30static DEFINE_SPINLOCK(ocfs2_stack_lock);
31static LIST_HEAD(ocfs2_stack_list);
32
33/*
34 * The stack currently in use. If not null, active_stack->sp_count > 0,
35 * the module is pinned, and the locking protocol cannot be changed.
36 */
37static struct ocfs2_stack_plugin *active_stack;
38
39static struct ocfs2_stack_plugin *ocfs2_stack_lookup(const char *name)
40{
41 struct ocfs2_stack_plugin *p;
42
43 assert_spin_locked(&ocfs2_stack_lock);
44
45 list_for_each_entry(p, &ocfs2_stack_list, sp_list) {
46 if (!strcmp(p->sp_name, name))
47 return p;
48 }
49
50 return NULL;
51}
52
53static int ocfs2_stack_driver_request(const char *name)
54{
55 int rc;
56 struct ocfs2_stack_plugin *p;
57
58 spin_lock(&ocfs2_stack_lock);
59
60 if (active_stack) {
61 /*
62 * If the active stack isn't the one we want, it cannot
63 * be selected right now.
64 */
65 if (!strcmp(active_stack->sp_name, name))
66 rc = 0;
67 else
68 rc = -EBUSY;
69 goto out;
70 }
71
72 p = ocfs2_stack_lookup(name);
73 if (!p || !try_module_get(p->sp_owner)) {
74 rc = -ENOENT;
75 goto out;
76 }
77
78 /* Ok, the stack is pinned */
79 p->sp_count++;
80 active_stack = p;
81
82 rc = 0;
83
84out:
85 spin_unlock(&ocfs2_stack_lock);
86 return rc;
87}
88
89/*
90 * This function looks up the appropriate stack and makes it active. If
91 * there is no stack, it tries to load it. It will fail if the stack still
92 * cannot be found. It will also fail if a different stack is in use.
93 */
94static int ocfs2_stack_driver_get(const char *name)
95{
96 int rc;
97
98 rc = ocfs2_stack_driver_request(name);
99 if (rc == -ENOENT) {
100 request_module("ocfs2_stack_%s", name);
101 rc = ocfs2_stack_driver_request(name);
102 }
103
104 if (rc == -ENOENT) {
105 printk(KERN_ERR
106 "ocfs2: Cluster stack driver \"%s\" cannot be found\n",
107 name);
108 } else if (rc == -EBUSY) {
109 printk(KERN_ERR
110 "ocfs2: A different cluster stack driver is in use\n");
111 }
112
113 return rc;
114}
115
116static void ocfs2_stack_driver_put(void)
117{
118 spin_lock(&ocfs2_stack_lock);
119 BUG_ON(active_stack == NULL);
120 BUG_ON(active_stack->sp_count == 0);
121
122 active_stack->sp_count--;
123 if (!active_stack->sp_count) {
124 module_put(active_stack->sp_owner);
125 active_stack = NULL;
126 }
127 spin_unlock(&ocfs2_stack_lock);
128}
129
130int ocfs2_stack_glue_register(struct ocfs2_stack_plugin *plugin)
131{
132 int rc;
133
134 spin_lock(&ocfs2_stack_lock);
135 if (!ocfs2_stack_lookup(plugin->sp_name)) {
136 plugin->sp_count = 0;
137 plugin->sp_proto = lproto;
138 list_add(&plugin->sp_list, &ocfs2_stack_list);
139 printk(KERN_INFO "ocfs2: Registered cluster interface %s\n",
140 plugin->sp_name);
141 rc = 0;
142 } else {
143 printk(KERN_ERR "ocfs2: Stack \"%s\" already registered\n",
144 plugin->sp_name);
145 rc = -EEXIST;
146 }
147 spin_unlock(&ocfs2_stack_lock);
148
149 return rc;
150}
151EXPORT_SYMBOL_GPL(ocfs2_stack_glue_register);
152
153void ocfs2_stack_glue_unregister(struct ocfs2_stack_plugin *plugin)
154{
155 struct ocfs2_stack_plugin *p;
156
157 spin_lock(&ocfs2_stack_lock);
158 p = ocfs2_stack_lookup(plugin->sp_name);
159 if (p) {
160 BUG_ON(p != plugin);
161 BUG_ON(plugin == active_stack);
162 BUG_ON(plugin->sp_count != 0);
163 list_del_init(&plugin->sp_list);
164 printk(KERN_INFO "ocfs2: Unregistered cluster interface %s\n",
165 plugin->sp_name);
166 } else {
167 printk(KERN_ERR "Stack \"%s\" is not registered\n",
168 plugin->sp_name);
169 }
170 spin_unlock(&ocfs2_stack_lock);
171}
172EXPORT_SYMBOL_GPL(ocfs2_stack_glue_unregister);
173
174void ocfs2_stack_glue_set_locking_protocol(struct ocfs2_locking_protocol *proto)
175{
176 struct ocfs2_stack_plugin *p;
177
178 BUG_ON(proto == NULL);
179
180 spin_lock(&ocfs2_stack_lock);
181 BUG_ON(active_stack != NULL);
182
183 lproto = proto;
184 list_for_each_entry(p, &ocfs2_stack_list, sp_list) {
185 p->sp_proto = lproto;
186 }
187
188 spin_unlock(&ocfs2_stack_lock);
189}
190EXPORT_SYMBOL_GPL(ocfs2_stack_glue_set_locking_protocol);
Joel Becker24ef1812008-01-29 17:37:32 -0800191
Joel Becker24ef1812008-01-29 17:37:32 -0800192
Joel Becker553aa7e2008-02-01 14:51:03 -0800193int ocfs2_dlm_lock(struct ocfs2_cluster_connection *conn,
194 int mode,
195 union ocfs2_dlm_lksb *lksb,
196 u32 flags,
197 void *name,
198 unsigned int namelen,
199 void *astarg)
200{
Joel Becker286eaa92008-02-01 15:03:57 -0800201 BUG_ON(lproto == NULL);
Joel Becker553aa7e2008-02-01 14:51:03 -0800202
Joel Becker286eaa92008-02-01 15:03:57 -0800203 return active_stack->sp_ops->dlm_lock(conn, mode, lksb, flags,
204 name, namelen, astarg);
Joel Becker24ef1812008-01-29 17:37:32 -0800205}
Joel Becker286eaa92008-02-01 15:03:57 -0800206EXPORT_SYMBOL_GPL(ocfs2_dlm_lock);
Joel Becker24ef1812008-01-29 17:37:32 -0800207
Joel Becker553aa7e2008-02-01 14:51:03 -0800208int ocfs2_dlm_unlock(struct ocfs2_cluster_connection *conn,
209 union ocfs2_dlm_lksb *lksb,
210 u32 flags,
211 void *astarg)
212{
Joel Becker286eaa92008-02-01 15:03:57 -0800213 BUG_ON(lproto == NULL);
Joel Becker553aa7e2008-02-01 14:51:03 -0800214
Joel Becker286eaa92008-02-01 15:03:57 -0800215 return active_stack->sp_ops->dlm_unlock(conn, lksb, flags, astarg);
Joel Becker8f2c9c12008-02-01 12:16:57 -0800216}
Joel Becker286eaa92008-02-01 15:03:57 -0800217EXPORT_SYMBOL_GPL(ocfs2_dlm_unlock);
Joel Becker8f2c9c12008-02-01 12:16:57 -0800218
Joel Becker553aa7e2008-02-01 14:51:03 -0800219int ocfs2_dlm_lock_status(union ocfs2_dlm_lksb *lksb)
220{
Joel Becker286eaa92008-02-01 15:03:57 -0800221 return active_stack->sp_ops->lock_status(lksb);
Joel Becker553aa7e2008-02-01 14:51:03 -0800222}
Joel Becker286eaa92008-02-01 15:03:57 -0800223EXPORT_SYMBOL_GPL(ocfs2_dlm_lock_status);
Joel Becker553aa7e2008-02-01 14:51:03 -0800224
Joel Becker8f2c9c12008-02-01 12:16:57 -0800225/*
226 * Why don't we cast to ocfs2_meta_lvb? The "clean" answer is that we
227 * don't cast at the glue level. The real answer is that the header
228 * ordering is nigh impossible.
229 */
Joel Becker553aa7e2008-02-01 14:51:03 -0800230void *ocfs2_dlm_lvb(union ocfs2_dlm_lksb *lksb)
231{
Joel Becker286eaa92008-02-01 15:03:57 -0800232 return active_stack->sp_ops->lock_lvb(lksb);
Joel Beckercf0acdc2008-01-29 16:59:55 -0800233}
Joel Becker286eaa92008-02-01 15:03:57 -0800234EXPORT_SYMBOL_GPL(ocfs2_dlm_lvb);
Joel Beckercf0acdc2008-01-29 16:59:55 -0800235
Joel Becker553aa7e2008-02-01 14:51:03 -0800236void ocfs2_dlm_dump_lksb(union ocfs2_dlm_lksb *lksb)
237{
Joel Becker286eaa92008-02-01 15:03:57 -0800238 active_stack->sp_ops->dump_lksb(lksb);
Joel Becker553aa7e2008-02-01 14:51:03 -0800239}
Joel Becker286eaa92008-02-01 15:03:57 -0800240EXPORT_SYMBOL_GPL(ocfs2_dlm_dump_lksb);
Joel Becker553aa7e2008-02-01 14:51:03 -0800241
Joel Becker4670c462008-02-01 14:39:35 -0800242int ocfs2_cluster_connect(const char *group,
243 int grouplen,
244 void (*recovery_handler)(int node_num,
245 void *recovery_data),
246 void *recovery_data,
247 struct ocfs2_cluster_connection **conn)
248{
249 int rc = 0;
250 struct ocfs2_cluster_connection *new_conn;
Joel Becker4670c462008-02-01 14:39:35 -0800251
252 BUG_ON(group == NULL);
253 BUG_ON(conn == NULL);
254 BUG_ON(recovery_handler == NULL);
255
256 if (grouplen > GROUP_NAME_MAX) {
257 rc = -EINVAL;
258 goto out;
259 }
260
261 new_conn = kzalloc(sizeof(struct ocfs2_cluster_connection),
262 GFP_KERNEL);
263 if (!new_conn) {
264 rc = -ENOMEM;
265 goto out;
266 }
267
268 memcpy(new_conn->cc_name, group, grouplen);
269 new_conn->cc_namelen = grouplen;
270 new_conn->cc_recovery_handler = recovery_handler;
271 new_conn->cc_recovery_data = recovery_data;
272
273 /* Start the new connection at our maximum compatibility level */
Joel Becker286eaa92008-02-01 15:03:57 -0800274 new_conn->cc_version = lproto->lp_max_version;
Joel Becker4670c462008-02-01 14:39:35 -0800275
Joel Becker286eaa92008-02-01 15:03:57 -0800276 /* This will pin the stack driver if successful */
277 rc = ocfs2_stack_driver_get("o2cb");
278 if (rc)
279 goto out_free;
280
281 rc = active_stack->sp_ops->connect(new_conn);
Joel Becker553aa7e2008-02-01 14:51:03 -0800282 if (rc) {
Joel Becker286eaa92008-02-01 15:03:57 -0800283 ocfs2_stack_driver_put();
Joel Becker4670c462008-02-01 14:39:35 -0800284 goto out_free;
285 }
286
Joel Becker4670c462008-02-01 14:39:35 -0800287 *conn = new_conn;
288
289out_free:
Joel Becker553aa7e2008-02-01 14:51:03 -0800290 if (rc)
Joel Becker4670c462008-02-01 14:39:35 -0800291 kfree(new_conn);
Joel Becker4670c462008-02-01 14:39:35 -0800292
293out:
294 return rc;
295}
Joel Becker286eaa92008-02-01 15:03:57 -0800296EXPORT_SYMBOL_GPL(ocfs2_cluster_connect);
Joel Becker4670c462008-02-01 14:39:35 -0800297
Joel Becker286eaa92008-02-01 15:03:57 -0800298/* If hangup_pending is 0, the stack driver will be dropped */
299int ocfs2_cluster_disconnect(struct ocfs2_cluster_connection *conn,
300 int hangup_pending)
Joel Becker553aa7e2008-02-01 14:51:03 -0800301{
302 int ret;
303
304 BUG_ON(conn == NULL);
305
Joel Becker286eaa92008-02-01 15:03:57 -0800306 ret = active_stack->sp_ops->disconnect(conn, hangup_pending);
Joel Becker553aa7e2008-02-01 14:51:03 -0800307
308 /* XXX Should we free it anyway? */
Joel Becker286eaa92008-02-01 15:03:57 -0800309 if (!ret) {
Joel Becker553aa7e2008-02-01 14:51:03 -0800310 kfree(conn);
Joel Becker286eaa92008-02-01 15:03:57 -0800311 if (!hangup_pending)
312 ocfs2_stack_driver_put();
313 }
Joel Becker553aa7e2008-02-01 14:51:03 -0800314
315 return ret;
316}
Joel Becker286eaa92008-02-01 15:03:57 -0800317EXPORT_SYMBOL_GPL(ocfs2_cluster_disconnect);
Joel Becker553aa7e2008-02-01 14:51:03 -0800318
Joel Becker6953b4c2008-01-29 16:59:56 -0800319void ocfs2_cluster_hangup(const char *group, int grouplen)
320{
321 BUG_ON(group == NULL);
322 BUG_ON(group[grouplen] != '\0');
323
Joel Becker286eaa92008-02-01 15:03:57 -0800324 active_stack->sp_ops->hangup(group, grouplen);
325
326 /* cluster_disconnect() was called with hangup_pending==1 */
327 ocfs2_stack_driver_put();
Joel Becker19fdb622008-01-30 15:38:24 -0800328}
Joel Becker286eaa92008-02-01 15:03:57 -0800329EXPORT_SYMBOL_GPL(ocfs2_cluster_hangup);
Joel Becker19fdb622008-01-30 15:38:24 -0800330
Joel Becker553aa7e2008-02-01 14:51:03 -0800331int ocfs2_cluster_this_node(unsigned int *node)
332{
Joel Becker286eaa92008-02-01 15:03:57 -0800333 return active_stack->sp_ops->this_node(node);
Joel Becker553aa7e2008-02-01 14:51:03 -0800334}
Joel Becker286eaa92008-02-01 15:03:57 -0800335EXPORT_SYMBOL_GPL(ocfs2_cluster_this_node);
Joel Becker553aa7e2008-02-01 14:51:03 -0800336
Joel Becker286eaa92008-02-01 15:03:57 -0800337
338static int __init ocfs2_stack_glue_init(void)
Joel Becker24ef1812008-01-29 17:37:32 -0800339{
Joel Becker286eaa92008-02-01 15:03:57 -0800340 return 0;
Joel Becker24ef1812008-01-29 17:37:32 -0800341}
342
Joel Becker286eaa92008-02-01 15:03:57 -0800343static void __exit ocfs2_stack_glue_exit(void)
344{
345 lproto = NULL;
346}
347
348MODULE_AUTHOR("Oracle");
349MODULE_DESCRIPTION("ocfs2 cluter stack glue layer");
350MODULE_LICENSE("GPL");
351module_init(ocfs2_stack_glue_init);
352module_exit(ocfs2_stack_glue_exit);