1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Code which implements an OCFS2 specific interface to underlying
9 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
21 #include <linux/list.h>
22 #include <linux/spinlock.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/kmod.h>
27 #include "stackglue.h"
29 static struct ocfs2_locking_protocol *lproto;
30 static DEFINE_SPINLOCK(ocfs2_stack_lock);
31 static LIST_HEAD(ocfs2_stack_list);
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.
37 static struct ocfs2_stack_plugin *active_stack;
39 static struct ocfs2_stack_plugin *ocfs2_stack_lookup(const char *name)
41 struct ocfs2_stack_plugin *p;
43 assert_spin_locked(&ocfs2_stack_lock);
45 list_for_each_entry(p, &ocfs2_stack_list, sp_list) {
46 if (!strcmp(p->sp_name, name))
53 static int ocfs2_stack_driver_request(const char *name)
56 struct ocfs2_stack_plugin *p;
58 spin_lock(&ocfs2_stack_lock);
62 * If the active stack isn't the one we want, it cannot
63 * be selected right now.
65 if (!strcmp(active_stack->sp_name, name))
72 p = ocfs2_stack_lookup(name);
73 if (!p || !try_module_get(p->sp_owner)) {
78 /* Ok, the stack is pinned */
85 spin_unlock(&ocfs2_stack_lock);
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.
94 static int ocfs2_stack_driver_get(const char *name)
98 rc = ocfs2_stack_driver_request(name);
100 request_module("ocfs2_stack_%s", name);
101 rc = ocfs2_stack_driver_request(name);
106 "ocfs2: Cluster stack driver \"%s\" cannot be found\n",
108 } else if (rc == -EBUSY) {
110 "ocfs2: A different cluster stack driver is in use\n");
116 static void ocfs2_stack_driver_put(void)
118 spin_lock(&ocfs2_stack_lock);
119 BUG_ON(active_stack == NULL);
120 BUG_ON(active_stack->sp_count == 0);
122 active_stack->sp_count--;
123 if (!active_stack->sp_count) {
124 module_put(active_stack->sp_owner);
127 spin_unlock(&ocfs2_stack_lock);
130 int ocfs2_stack_glue_register(struct ocfs2_stack_plugin *plugin)
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",
143 printk(KERN_ERR "ocfs2: Stack \"%s\" already registered\n",
147 spin_unlock(&ocfs2_stack_lock);
151 EXPORT_SYMBOL_GPL(ocfs2_stack_glue_register);
153 void ocfs2_stack_glue_unregister(struct ocfs2_stack_plugin *plugin)
155 struct ocfs2_stack_plugin *p;
157 spin_lock(&ocfs2_stack_lock);
158 p = ocfs2_stack_lookup(plugin->sp_name);
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",
167 printk(KERN_ERR "Stack \"%s\" is not registered\n",
170 spin_unlock(&ocfs2_stack_lock);
172 EXPORT_SYMBOL_GPL(ocfs2_stack_glue_unregister);
174 void ocfs2_stack_glue_set_locking_protocol(struct ocfs2_locking_protocol *proto)
176 struct ocfs2_stack_plugin *p;
178 BUG_ON(proto == NULL);
180 spin_lock(&ocfs2_stack_lock);
181 BUG_ON(active_stack != NULL);
184 list_for_each_entry(p, &ocfs2_stack_list, sp_list) {
185 p->sp_proto = lproto;
188 spin_unlock(&ocfs2_stack_lock);
190 EXPORT_SYMBOL_GPL(ocfs2_stack_glue_set_locking_protocol);
193 int ocfs2_dlm_lock(struct ocfs2_cluster_connection *conn,
195 union ocfs2_dlm_lksb *lksb,
198 unsigned int namelen,
201 BUG_ON(lproto == NULL);
203 return active_stack->sp_ops->dlm_lock(conn, mode, lksb, flags,
204 name, namelen, astarg);
206 EXPORT_SYMBOL_GPL(ocfs2_dlm_lock);
208 int ocfs2_dlm_unlock(struct ocfs2_cluster_connection *conn,
209 union ocfs2_dlm_lksb *lksb,
213 BUG_ON(lproto == NULL);
215 return active_stack->sp_ops->dlm_unlock(conn, lksb, flags, astarg);
217 EXPORT_SYMBOL_GPL(ocfs2_dlm_unlock);
219 int ocfs2_dlm_lock_status(union ocfs2_dlm_lksb *lksb)
221 return active_stack->sp_ops->lock_status(lksb);
223 EXPORT_SYMBOL_GPL(ocfs2_dlm_lock_status);
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.
230 void *ocfs2_dlm_lvb(union ocfs2_dlm_lksb *lksb)
232 return active_stack->sp_ops->lock_lvb(lksb);
234 EXPORT_SYMBOL_GPL(ocfs2_dlm_lvb);
236 void ocfs2_dlm_dump_lksb(union ocfs2_dlm_lksb *lksb)
238 active_stack->sp_ops->dump_lksb(lksb);
240 EXPORT_SYMBOL_GPL(ocfs2_dlm_dump_lksb);
242 int ocfs2_cluster_connect(const char *group,
244 void (*recovery_handler)(int node_num,
245 void *recovery_data),
247 struct ocfs2_cluster_connection **conn)
250 struct ocfs2_cluster_connection *new_conn;
252 BUG_ON(group == NULL);
253 BUG_ON(conn == NULL);
254 BUG_ON(recovery_handler == NULL);
256 if (grouplen > GROUP_NAME_MAX) {
261 new_conn = kzalloc(sizeof(struct ocfs2_cluster_connection),
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;
273 /* Start the new connection at our maximum compatibility level */
274 new_conn->cc_version = lproto->lp_max_version;
276 /* This will pin the stack driver if successful */
277 rc = ocfs2_stack_driver_get("o2cb");
281 rc = active_stack->sp_ops->connect(new_conn);
283 ocfs2_stack_driver_put();
296 EXPORT_SYMBOL_GPL(ocfs2_cluster_connect);
298 /* If hangup_pending is 0, the stack driver will be dropped */
299 int ocfs2_cluster_disconnect(struct ocfs2_cluster_connection *conn,
304 BUG_ON(conn == NULL);
306 ret = active_stack->sp_ops->disconnect(conn, hangup_pending);
308 /* XXX Should we free it anyway? */
312 ocfs2_stack_driver_put();
317 EXPORT_SYMBOL_GPL(ocfs2_cluster_disconnect);
319 void ocfs2_cluster_hangup(const char *group, int grouplen)
321 BUG_ON(group == NULL);
322 BUG_ON(group[grouplen] != '\0');
324 active_stack->sp_ops->hangup(group, grouplen);
326 /* cluster_disconnect() was called with hangup_pending==1 */
327 ocfs2_stack_driver_put();
329 EXPORT_SYMBOL_GPL(ocfs2_cluster_hangup);
331 int ocfs2_cluster_this_node(unsigned int *node)
333 return active_stack->sp_ops->this_node(node);
335 EXPORT_SYMBOL_GPL(ocfs2_cluster_this_node);
338 static int __init ocfs2_stack_glue_init(void)
343 static void __exit ocfs2_stack_glue_exit(void)
348 MODULE_AUTHOR("Oracle");
349 MODULE_DESCRIPTION("ocfs2 cluter stack glue layer");
350 MODULE_LICENSE("GPL");
351 module_init(ocfs2_stack_glue_init);
352 module_exit(ocfs2_stack_glue_exit);