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>
32 #include "stackglue.h"
34 #define OCFS2_STACK_PLUGIN_O2CB "o2cb"
35 #define OCFS2_STACK_PLUGIN_USER "user"
37 static struct ocfs2_locking_protocol *lproto;
38 static DEFINE_SPINLOCK(ocfs2_stack_lock);
39 static LIST_HEAD(ocfs2_stack_list);
40 static char cluster_stack_name[OCFS2_STACK_LABEL_LEN + 1];
43 * The stack currently in use. If not null, active_stack->sp_count > 0,
44 * the module is pinned, and the locking protocol cannot be changed.
46 static struct ocfs2_stack_plugin *active_stack;
48 static struct ocfs2_stack_plugin *ocfs2_stack_lookup(const char *name)
50 struct ocfs2_stack_plugin *p;
52 assert_spin_locked(&ocfs2_stack_lock);
54 list_for_each_entry(p, &ocfs2_stack_list, sp_list) {
55 if (!strcmp(p->sp_name, name))
62 static int ocfs2_stack_driver_request(const char *stack_name,
63 const char *plugin_name)
66 struct ocfs2_stack_plugin *p;
68 spin_lock(&ocfs2_stack_lock);
71 * If the stack passed by the filesystem isn't the selected one,
74 if (strcmp(stack_name, cluster_stack_name)) {
81 * If the active stack isn't the one we want, it cannot
82 * be selected right now.
84 if (!strcmp(active_stack->sp_name, plugin_name))
91 p = ocfs2_stack_lookup(plugin_name);
92 if (!p || !try_module_get(p->sp_owner)) {
97 /* Ok, the stack is pinned */
104 spin_unlock(&ocfs2_stack_lock);
109 * This function looks up the appropriate stack and makes it active. If
110 * there is no stack, it tries to load it. It will fail if the stack still
111 * cannot be found. It will also fail if a different stack is in use.
113 static int ocfs2_stack_driver_get(const char *stack_name)
116 char *plugin_name = OCFS2_STACK_PLUGIN_O2CB;
119 * Classic stack does not pass in a stack name. This is
120 * compatible with older tools as well.
122 if (!stack_name || !*stack_name)
123 stack_name = OCFS2_STACK_PLUGIN_O2CB;
125 if (strlen(stack_name) != OCFS2_STACK_LABEL_LEN) {
127 "ocfs2 passed an invalid cluster stack label: \"%s\"\n",
132 /* Anything that isn't the classic stack is a user stack */
133 if (strcmp(stack_name, OCFS2_STACK_PLUGIN_O2CB))
134 plugin_name = OCFS2_STACK_PLUGIN_USER;
136 rc = ocfs2_stack_driver_request(stack_name, plugin_name);
138 request_module("ocfs2_stack_%s", plugin_name);
139 rc = ocfs2_stack_driver_request(stack_name, plugin_name);
144 "ocfs2: Cluster stack driver \"%s\" cannot be found\n",
146 } else if (rc == -EBUSY) {
148 "ocfs2: A different cluster stack is in use\n");
154 static void ocfs2_stack_driver_put(void)
156 spin_lock(&ocfs2_stack_lock);
157 BUG_ON(active_stack == NULL);
158 BUG_ON(active_stack->sp_count == 0);
160 active_stack->sp_count--;
161 if (!active_stack->sp_count) {
162 module_put(active_stack->sp_owner);
165 spin_unlock(&ocfs2_stack_lock);
168 int ocfs2_stack_glue_register(struct ocfs2_stack_plugin *plugin)
172 spin_lock(&ocfs2_stack_lock);
173 if (!ocfs2_stack_lookup(plugin->sp_name)) {
174 plugin->sp_count = 0;
175 plugin->sp_proto = lproto;
176 list_add(&plugin->sp_list, &ocfs2_stack_list);
177 printk(KERN_INFO "ocfs2: Registered cluster interface %s\n",
181 printk(KERN_ERR "ocfs2: Stack \"%s\" already registered\n",
185 spin_unlock(&ocfs2_stack_lock);
189 EXPORT_SYMBOL_GPL(ocfs2_stack_glue_register);
191 void ocfs2_stack_glue_unregister(struct ocfs2_stack_plugin *plugin)
193 struct ocfs2_stack_plugin *p;
195 spin_lock(&ocfs2_stack_lock);
196 p = ocfs2_stack_lookup(plugin->sp_name);
199 BUG_ON(plugin == active_stack);
200 BUG_ON(plugin->sp_count != 0);
201 list_del_init(&plugin->sp_list);
202 printk(KERN_INFO "ocfs2: Unregistered cluster interface %s\n",
205 printk(KERN_ERR "Stack \"%s\" is not registered\n",
208 spin_unlock(&ocfs2_stack_lock);
210 EXPORT_SYMBOL_GPL(ocfs2_stack_glue_unregister);
212 void ocfs2_stack_glue_set_locking_protocol(struct ocfs2_locking_protocol *proto)
214 struct ocfs2_stack_plugin *p;
216 BUG_ON(proto == NULL);
218 spin_lock(&ocfs2_stack_lock);
219 BUG_ON(active_stack != NULL);
222 list_for_each_entry(p, &ocfs2_stack_list, sp_list) {
223 p->sp_proto = lproto;
226 spin_unlock(&ocfs2_stack_lock);
228 EXPORT_SYMBOL_GPL(ocfs2_stack_glue_set_locking_protocol);
231 int ocfs2_dlm_lock(struct ocfs2_cluster_connection *conn,
233 union ocfs2_dlm_lksb *lksb,
236 unsigned int namelen,
239 BUG_ON(lproto == NULL);
241 return active_stack->sp_ops->dlm_lock(conn, mode, lksb, flags,
242 name, namelen, astarg);
244 EXPORT_SYMBOL_GPL(ocfs2_dlm_lock);
246 int ocfs2_dlm_unlock(struct ocfs2_cluster_connection *conn,
247 union ocfs2_dlm_lksb *lksb,
251 BUG_ON(lproto == NULL);
253 return active_stack->sp_ops->dlm_unlock(conn, lksb, flags, astarg);
255 EXPORT_SYMBOL_GPL(ocfs2_dlm_unlock);
257 int ocfs2_dlm_lock_status(union ocfs2_dlm_lksb *lksb)
259 return active_stack->sp_ops->lock_status(lksb);
261 EXPORT_SYMBOL_GPL(ocfs2_dlm_lock_status);
264 * Why don't we cast to ocfs2_meta_lvb? The "clean" answer is that we
265 * don't cast at the glue level. The real answer is that the header
266 * ordering is nigh impossible.
268 void *ocfs2_dlm_lvb(union ocfs2_dlm_lksb *lksb)
270 return active_stack->sp_ops->lock_lvb(lksb);
272 EXPORT_SYMBOL_GPL(ocfs2_dlm_lvb);
274 void ocfs2_dlm_dump_lksb(union ocfs2_dlm_lksb *lksb)
276 active_stack->sp_ops->dump_lksb(lksb);
278 EXPORT_SYMBOL_GPL(ocfs2_dlm_dump_lksb);
280 int ocfs2_cluster_connect(const char *stack_name,
283 void (*recovery_handler)(int node_num,
284 void *recovery_data),
286 struct ocfs2_cluster_connection **conn)
289 struct ocfs2_cluster_connection *new_conn;
291 BUG_ON(group == NULL);
292 BUG_ON(conn == NULL);
293 BUG_ON(recovery_handler == NULL);
295 if (grouplen > GROUP_NAME_MAX) {
300 new_conn = kzalloc(sizeof(struct ocfs2_cluster_connection),
307 memcpy(new_conn->cc_name, group, grouplen);
308 new_conn->cc_namelen = grouplen;
309 new_conn->cc_recovery_handler = recovery_handler;
310 new_conn->cc_recovery_data = recovery_data;
312 /* Start the new connection at our maximum compatibility level */
313 new_conn->cc_version = lproto->lp_max_version;
315 /* This will pin the stack driver if successful */
316 rc = ocfs2_stack_driver_get(stack_name);
320 rc = active_stack->sp_ops->connect(new_conn);
322 ocfs2_stack_driver_put();
335 EXPORT_SYMBOL_GPL(ocfs2_cluster_connect);
337 /* If hangup_pending is 0, the stack driver will be dropped */
338 int ocfs2_cluster_disconnect(struct ocfs2_cluster_connection *conn,
343 BUG_ON(conn == NULL);
345 ret = active_stack->sp_ops->disconnect(conn, hangup_pending);
347 /* XXX Should we free it anyway? */
351 ocfs2_stack_driver_put();
356 EXPORT_SYMBOL_GPL(ocfs2_cluster_disconnect);
358 void ocfs2_cluster_hangup(const char *group, int grouplen)
360 BUG_ON(group == NULL);
361 BUG_ON(group[grouplen] != '\0');
363 active_stack->sp_ops->hangup(group, grouplen);
365 /* cluster_disconnect() was called with hangup_pending==1 */
366 ocfs2_stack_driver_put();
368 EXPORT_SYMBOL_GPL(ocfs2_cluster_hangup);
370 int ocfs2_cluster_this_node(unsigned int *node)
372 return active_stack->sp_ops->this_node(node);
374 EXPORT_SYMBOL_GPL(ocfs2_cluster_this_node);
381 static ssize_t ocfs2_max_locking_protocol_show(struct kobject *kobj,
382 struct kobj_attribute *attr,
387 spin_lock(&ocfs2_stack_lock);
389 ret = snprintf(buf, PAGE_SIZE, "%u.%u\n",
390 lproto->lp_max_version.pv_major,
391 lproto->lp_max_version.pv_minor);
392 spin_unlock(&ocfs2_stack_lock);
397 static struct kobj_attribute ocfs2_attr_max_locking_protocol =
398 __ATTR(max_locking_protocol, S_IFREG | S_IRUGO,
399 ocfs2_max_locking_protocol_show, NULL);
401 static ssize_t ocfs2_loaded_cluster_plugins_show(struct kobject *kobj,
402 struct kobj_attribute *attr,
405 ssize_t ret = 0, total = 0, remain = PAGE_SIZE;
406 struct ocfs2_stack_plugin *p;
408 spin_lock(&ocfs2_stack_lock);
409 list_for_each_entry(p, &ocfs2_stack_list, sp_list) {
410 ret = snprintf(buf, remain, "%s\n",
417 /* snprintf() didn't fit */
424 spin_unlock(&ocfs2_stack_lock);
429 static struct kobj_attribute ocfs2_attr_loaded_cluster_plugins =
430 __ATTR(loaded_cluster_plugins, S_IFREG | S_IRUGO,
431 ocfs2_loaded_cluster_plugins_show, NULL);
433 static ssize_t ocfs2_active_cluster_plugin_show(struct kobject *kobj,
434 struct kobj_attribute *attr,
439 spin_lock(&ocfs2_stack_lock);
441 ret = snprintf(buf, PAGE_SIZE, "%s\n",
442 active_stack->sp_name);
443 if (ret == PAGE_SIZE)
446 spin_unlock(&ocfs2_stack_lock);
451 static struct kobj_attribute ocfs2_attr_active_cluster_plugin =
452 __ATTR(active_cluster_plugin, S_IFREG | S_IRUGO,
453 ocfs2_active_cluster_plugin_show, NULL);
455 static ssize_t ocfs2_cluster_stack_show(struct kobject *kobj,
456 struct kobj_attribute *attr,
460 spin_lock(&ocfs2_stack_lock);
461 ret = snprintf(buf, PAGE_SIZE, "%s\n", cluster_stack_name);
462 spin_unlock(&ocfs2_stack_lock);
467 static ssize_t ocfs2_cluster_stack_store(struct kobject *kobj,
468 struct kobj_attribute *attr,
469 const char *buf, size_t count)
477 if (buf[len - 1] == '\n')
480 if ((len != OCFS2_STACK_LABEL_LEN) ||
481 (strnlen(buf, len) != len))
484 spin_lock(&ocfs2_stack_lock);
486 if (!strncmp(buf, cluster_stack_name, len))
491 memcpy(cluster_stack_name, buf, len);
494 spin_unlock(&ocfs2_stack_lock);
500 static struct kobj_attribute ocfs2_attr_cluster_stack =
501 __ATTR(cluster_stack, S_IFREG | S_IRUGO | S_IWUSR,
502 ocfs2_cluster_stack_show,
503 ocfs2_cluster_stack_store);
505 static struct attribute *ocfs2_attrs[] = {
506 &ocfs2_attr_max_locking_protocol.attr,
507 &ocfs2_attr_loaded_cluster_plugins.attr,
508 &ocfs2_attr_active_cluster_plugin.attr,
509 &ocfs2_attr_cluster_stack.attr,
513 static struct attribute_group ocfs2_attr_group = {
514 .attrs = ocfs2_attrs,
517 static struct kset *ocfs2_kset;
519 static void ocfs2_sysfs_exit(void)
521 kset_unregister(ocfs2_kset);
524 static int ocfs2_sysfs_init(void)
528 ocfs2_kset = kset_create_and_add("ocfs2", NULL, fs_kobj);
532 ret = sysfs_create_group(&ocfs2_kset->kobj, &ocfs2_attr_group);
539 kset_unregister(ocfs2_kset);
543 static int __init ocfs2_stack_glue_init(void)
545 strcpy(cluster_stack_name, OCFS2_STACK_PLUGIN_O2CB);
547 return ocfs2_sysfs_init();
550 static void __exit ocfs2_stack_glue_exit(void)
556 MODULE_AUTHOR("Oracle");
557 MODULE_DESCRIPTION("ocfs2 cluter stack glue layer");
558 MODULE_LICENSE("GPL");
559 module_init(ocfs2_stack_glue_init);
560 module_exit(ocfs2_stack_glue_exit);