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 <linux/kobject.h>
28 #include <linux/sysfs.h>
30 #include "stackglue.h"
32 static struct ocfs2_locking_protocol *lproto;
33 static DEFINE_SPINLOCK(ocfs2_stack_lock);
34 static LIST_HEAD(ocfs2_stack_list);
37 * The stack currently in use. If not null, active_stack->sp_count > 0,
38 * the module is pinned, and the locking protocol cannot be changed.
40 static struct ocfs2_stack_plugin *active_stack;
42 static struct ocfs2_stack_plugin *ocfs2_stack_lookup(const char *name)
44 struct ocfs2_stack_plugin *p;
46 assert_spin_locked(&ocfs2_stack_lock);
48 list_for_each_entry(p, &ocfs2_stack_list, sp_list) {
49 if (!strcmp(p->sp_name, name))
56 static int ocfs2_stack_driver_request(const char *name)
59 struct ocfs2_stack_plugin *p;
61 spin_lock(&ocfs2_stack_lock);
65 * If the active stack isn't the one we want, it cannot
66 * be selected right now.
68 if (!strcmp(active_stack->sp_name, name))
75 p = ocfs2_stack_lookup(name);
76 if (!p || !try_module_get(p->sp_owner)) {
81 /* Ok, the stack is pinned */
88 spin_unlock(&ocfs2_stack_lock);
93 * This function looks up the appropriate stack and makes it active. If
94 * there is no stack, it tries to load it. It will fail if the stack still
95 * cannot be found. It will also fail if a different stack is in use.
97 static int ocfs2_stack_driver_get(const char *name)
101 rc = ocfs2_stack_driver_request(name);
103 request_module("ocfs2_stack_%s", name);
104 rc = ocfs2_stack_driver_request(name);
109 "ocfs2: Cluster stack driver \"%s\" cannot be found\n",
111 } else if (rc == -EBUSY) {
113 "ocfs2: A different cluster stack driver is in use\n");
119 static void ocfs2_stack_driver_put(void)
121 spin_lock(&ocfs2_stack_lock);
122 BUG_ON(active_stack == NULL);
123 BUG_ON(active_stack->sp_count == 0);
125 active_stack->sp_count--;
126 if (!active_stack->sp_count) {
127 module_put(active_stack->sp_owner);
130 spin_unlock(&ocfs2_stack_lock);
133 int ocfs2_stack_glue_register(struct ocfs2_stack_plugin *plugin)
137 spin_lock(&ocfs2_stack_lock);
138 if (!ocfs2_stack_lookup(plugin->sp_name)) {
139 plugin->sp_count = 0;
140 plugin->sp_proto = lproto;
141 list_add(&plugin->sp_list, &ocfs2_stack_list);
142 printk(KERN_INFO "ocfs2: Registered cluster interface %s\n",
146 printk(KERN_ERR "ocfs2: Stack \"%s\" already registered\n",
150 spin_unlock(&ocfs2_stack_lock);
154 EXPORT_SYMBOL_GPL(ocfs2_stack_glue_register);
156 void ocfs2_stack_glue_unregister(struct ocfs2_stack_plugin *plugin)
158 struct ocfs2_stack_plugin *p;
160 spin_lock(&ocfs2_stack_lock);
161 p = ocfs2_stack_lookup(plugin->sp_name);
164 BUG_ON(plugin == active_stack);
165 BUG_ON(plugin->sp_count != 0);
166 list_del_init(&plugin->sp_list);
167 printk(KERN_INFO "ocfs2: Unregistered cluster interface %s\n",
170 printk(KERN_ERR "Stack \"%s\" is not registered\n",
173 spin_unlock(&ocfs2_stack_lock);
175 EXPORT_SYMBOL_GPL(ocfs2_stack_glue_unregister);
177 void ocfs2_stack_glue_set_locking_protocol(struct ocfs2_locking_protocol *proto)
179 struct ocfs2_stack_plugin *p;
181 BUG_ON(proto == NULL);
183 spin_lock(&ocfs2_stack_lock);
184 BUG_ON(active_stack != NULL);
187 list_for_each_entry(p, &ocfs2_stack_list, sp_list) {
188 p->sp_proto = lproto;
191 spin_unlock(&ocfs2_stack_lock);
193 EXPORT_SYMBOL_GPL(ocfs2_stack_glue_set_locking_protocol);
196 int ocfs2_dlm_lock(struct ocfs2_cluster_connection *conn,
198 union ocfs2_dlm_lksb *lksb,
201 unsigned int namelen,
204 BUG_ON(lproto == NULL);
206 return active_stack->sp_ops->dlm_lock(conn, mode, lksb, flags,
207 name, namelen, astarg);
209 EXPORT_SYMBOL_GPL(ocfs2_dlm_lock);
211 int ocfs2_dlm_unlock(struct ocfs2_cluster_connection *conn,
212 union ocfs2_dlm_lksb *lksb,
216 BUG_ON(lproto == NULL);
218 return active_stack->sp_ops->dlm_unlock(conn, lksb, flags, astarg);
220 EXPORT_SYMBOL_GPL(ocfs2_dlm_unlock);
222 int ocfs2_dlm_lock_status(union ocfs2_dlm_lksb *lksb)
224 return active_stack->sp_ops->lock_status(lksb);
226 EXPORT_SYMBOL_GPL(ocfs2_dlm_lock_status);
229 * Why don't we cast to ocfs2_meta_lvb? The "clean" answer is that we
230 * don't cast at the glue level. The real answer is that the header
231 * ordering is nigh impossible.
233 void *ocfs2_dlm_lvb(union ocfs2_dlm_lksb *lksb)
235 return active_stack->sp_ops->lock_lvb(lksb);
237 EXPORT_SYMBOL_GPL(ocfs2_dlm_lvb);
239 void ocfs2_dlm_dump_lksb(union ocfs2_dlm_lksb *lksb)
241 active_stack->sp_ops->dump_lksb(lksb);
243 EXPORT_SYMBOL_GPL(ocfs2_dlm_dump_lksb);
245 int ocfs2_cluster_connect(const char *group,
247 void (*recovery_handler)(int node_num,
248 void *recovery_data),
250 struct ocfs2_cluster_connection **conn)
253 struct ocfs2_cluster_connection *new_conn;
255 BUG_ON(group == NULL);
256 BUG_ON(conn == NULL);
257 BUG_ON(recovery_handler == NULL);
259 if (grouplen > GROUP_NAME_MAX) {
264 new_conn = kzalloc(sizeof(struct ocfs2_cluster_connection),
271 memcpy(new_conn->cc_name, group, grouplen);
272 new_conn->cc_namelen = grouplen;
273 new_conn->cc_recovery_handler = recovery_handler;
274 new_conn->cc_recovery_data = recovery_data;
276 /* Start the new connection at our maximum compatibility level */
277 new_conn->cc_version = lproto->lp_max_version;
279 /* This will pin the stack driver if successful */
280 rc = ocfs2_stack_driver_get("o2cb");
284 rc = active_stack->sp_ops->connect(new_conn);
286 ocfs2_stack_driver_put();
299 EXPORT_SYMBOL_GPL(ocfs2_cluster_connect);
301 /* If hangup_pending is 0, the stack driver will be dropped */
302 int ocfs2_cluster_disconnect(struct ocfs2_cluster_connection *conn,
307 BUG_ON(conn == NULL);
309 ret = active_stack->sp_ops->disconnect(conn, hangup_pending);
311 /* XXX Should we free it anyway? */
315 ocfs2_stack_driver_put();
320 EXPORT_SYMBOL_GPL(ocfs2_cluster_disconnect);
322 void ocfs2_cluster_hangup(const char *group, int grouplen)
324 BUG_ON(group == NULL);
325 BUG_ON(group[grouplen] != '\0');
327 active_stack->sp_ops->hangup(group, grouplen);
329 /* cluster_disconnect() was called with hangup_pending==1 */
330 ocfs2_stack_driver_put();
332 EXPORT_SYMBOL_GPL(ocfs2_cluster_hangup);
334 int ocfs2_cluster_this_node(unsigned int *node)
336 return active_stack->sp_ops->this_node(node);
338 EXPORT_SYMBOL_GPL(ocfs2_cluster_this_node);
345 static ssize_t ocfs2_max_locking_protocol_show(struct kobject *kobj,
346 struct kobj_attribute *attr,
351 spin_lock(&ocfs2_stack_lock);
353 ret = snprintf(buf, PAGE_SIZE, "%u.%u\n",
354 lproto->lp_max_version.pv_major,
355 lproto->lp_max_version.pv_minor);
356 spin_unlock(&ocfs2_stack_lock);
361 static struct kobj_attribute ocfs2_attr_max_locking_protocol =
362 __ATTR(max_locking_protocol, S_IFREG | S_IRUGO,
363 ocfs2_max_locking_protocol_show, NULL);
365 static ssize_t ocfs2_loaded_cluster_plugins_show(struct kobject *kobj,
366 struct kobj_attribute *attr,
369 ssize_t ret = 0, total = 0, remain = PAGE_SIZE;
370 struct ocfs2_stack_plugin *p;
372 spin_lock(&ocfs2_stack_lock);
373 list_for_each_entry(p, &ocfs2_stack_list, sp_list) {
374 ret = snprintf(buf, remain, "%s\n",
381 /* snprintf() didn't fit */
388 spin_unlock(&ocfs2_stack_lock);
393 static struct kobj_attribute ocfs2_attr_loaded_cluster_plugins =
394 __ATTR(loaded_cluster_plugins, S_IFREG | S_IRUGO,
395 ocfs2_loaded_cluster_plugins_show, NULL);
397 static ssize_t ocfs2_active_cluster_plugin_show(struct kobject *kobj,
398 struct kobj_attribute *attr,
403 spin_lock(&ocfs2_stack_lock);
405 ret = snprintf(buf, PAGE_SIZE, "%s\n",
406 active_stack->sp_name);
407 if (ret == PAGE_SIZE)
410 spin_unlock(&ocfs2_stack_lock);
415 static struct kobj_attribute ocfs2_attr_active_cluster_plugin =
416 __ATTR(active_cluster_plugin, S_IFREG | S_IRUGO,
417 ocfs2_active_cluster_plugin_show, NULL);
419 static struct attribute *ocfs2_attrs[] = {
420 &ocfs2_attr_max_locking_protocol.attr,
421 &ocfs2_attr_loaded_cluster_plugins.attr,
422 &ocfs2_attr_active_cluster_plugin.attr,
426 static struct attribute_group ocfs2_attr_group = {
427 .attrs = ocfs2_attrs,
430 static struct kset *ocfs2_kset;
432 static void ocfs2_sysfs_exit(void)
434 kset_unregister(ocfs2_kset);
437 static int ocfs2_sysfs_init(void)
441 ocfs2_kset = kset_create_and_add("ocfs2", NULL, fs_kobj);
445 ret = sysfs_create_group(&ocfs2_kset->kobj, &ocfs2_attr_group);
452 kset_unregister(ocfs2_kset);
456 static int __init ocfs2_stack_glue_init(void)
458 return ocfs2_sysfs_init();
461 static void __exit ocfs2_stack_glue_exit(void)
467 MODULE_AUTHOR("Oracle");
468 MODULE_DESCRIPTION("ocfs2 cluter stack glue layer");
469 MODULE_LICENSE("GPL");
470 module_init(ocfs2_stack_glue_init);
471 module_exit(ocfs2_stack_glue_exit);