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>
29 #include <linux/sysctl.h>
33 #include "stackglue.h"
35 #define OCFS2_STACK_PLUGIN_O2CB "o2cb"
36 #define OCFS2_STACK_PLUGIN_USER "user"
38 static struct ocfs2_locking_protocol *lproto;
39 static DEFINE_SPINLOCK(ocfs2_stack_lock);
40 static LIST_HEAD(ocfs2_stack_list);
41 static char cluster_stack_name[OCFS2_STACK_LABEL_LEN + 1];
44 * The stack currently in use. If not null, active_stack->sp_count > 0,
45 * the module is pinned, and the locking protocol cannot be changed.
47 static struct ocfs2_stack_plugin *active_stack;
49 static struct ocfs2_stack_plugin *ocfs2_stack_lookup(const char *name)
51 struct ocfs2_stack_plugin *p;
53 assert_spin_locked(&ocfs2_stack_lock);
55 list_for_each_entry(p, &ocfs2_stack_list, sp_list) {
56 if (!strcmp(p->sp_name, name))
63 static int ocfs2_stack_driver_request(const char *stack_name,
64 const char *plugin_name)
67 struct ocfs2_stack_plugin *p;
69 spin_lock(&ocfs2_stack_lock);
72 * If the stack passed by the filesystem isn't the selected one,
75 if (strcmp(stack_name, cluster_stack_name)) {
82 * If the active stack isn't the one we want, it cannot
83 * be selected right now.
85 if (!strcmp(active_stack->sp_name, plugin_name))
92 p = ocfs2_stack_lookup(plugin_name);
93 if (!p || !try_module_get(p->sp_owner)) {
98 /* Ok, the stack is pinned */
105 spin_unlock(&ocfs2_stack_lock);
110 * This function looks up the appropriate stack and makes it active. If
111 * there is no stack, it tries to load it. It will fail if the stack still
112 * cannot be found. It will also fail if a different stack is in use.
114 static int ocfs2_stack_driver_get(const char *stack_name)
117 char *plugin_name = OCFS2_STACK_PLUGIN_O2CB;
120 * Classic stack does not pass in a stack name. This is
121 * compatible with older tools as well.
123 if (!stack_name || !*stack_name)
124 stack_name = OCFS2_STACK_PLUGIN_O2CB;
126 if (strlen(stack_name) != OCFS2_STACK_LABEL_LEN) {
128 "ocfs2 passed an invalid cluster stack label: \"%s\"\n",
133 /* Anything that isn't the classic stack is a user stack */
134 if (strcmp(stack_name, OCFS2_STACK_PLUGIN_O2CB))
135 plugin_name = OCFS2_STACK_PLUGIN_USER;
137 rc = ocfs2_stack_driver_request(stack_name, plugin_name);
139 request_module("ocfs2_stack_%s", plugin_name);
140 rc = ocfs2_stack_driver_request(stack_name, plugin_name);
145 "ocfs2: Cluster stack driver \"%s\" cannot be found\n",
147 } else if (rc == -EBUSY) {
149 "ocfs2: A different cluster stack is in use\n");
155 static void ocfs2_stack_driver_put(void)
157 spin_lock(&ocfs2_stack_lock);
158 BUG_ON(active_stack == NULL);
159 BUG_ON(active_stack->sp_count == 0);
161 active_stack->sp_count--;
162 if (!active_stack->sp_count) {
163 module_put(active_stack->sp_owner);
166 spin_unlock(&ocfs2_stack_lock);
169 int ocfs2_stack_glue_register(struct ocfs2_stack_plugin *plugin)
173 spin_lock(&ocfs2_stack_lock);
174 if (!ocfs2_stack_lookup(plugin->sp_name)) {
175 plugin->sp_count = 0;
176 plugin->sp_proto = lproto;
177 list_add(&plugin->sp_list, &ocfs2_stack_list);
178 printk(KERN_INFO "ocfs2: Registered cluster interface %s\n",
182 printk(KERN_ERR "ocfs2: Stack \"%s\" already registered\n",
186 spin_unlock(&ocfs2_stack_lock);
190 EXPORT_SYMBOL_GPL(ocfs2_stack_glue_register);
192 void ocfs2_stack_glue_unregister(struct ocfs2_stack_plugin *plugin)
194 struct ocfs2_stack_plugin *p;
196 spin_lock(&ocfs2_stack_lock);
197 p = ocfs2_stack_lookup(plugin->sp_name);
200 BUG_ON(plugin == active_stack);
201 BUG_ON(plugin->sp_count != 0);
202 list_del_init(&plugin->sp_list);
203 printk(KERN_INFO "ocfs2: Unregistered cluster interface %s\n",
206 printk(KERN_ERR "Stack \"%s\" is not registered\n",
209 spin_unlock(&ocfs2_stack_lock);
211 EXPORT_SYMBOL_GPL(ocfs2_stack_glue_unregister);
213 void ocfs2_stack_glue_set_locking_protocol(struct ocfs2_locking_protocol *proto)
215 struct ocfs2_stack_plugin *p;
217 BUG_ON(proto == NULL);
219 spin_lock(&ocfs2_stack_lock);
220 BUG_ON(active_stack != NULL);
223 list_for_each_entry(p, &ocfs2_stack_list, sp_list) {
224 p->sp_proto = lproto;
227 spin_unlock(&ocfs2_stack_lock);
229 EXPORT_SYMBOL_GPL(ocfs2_stack_glue_set_locking_protocol);
233 * The ocfs2_dlm_lock() and ocfs2_dlm_unlock() functions take
234 * "struct ocfs2_lock_res *astarg" instead of "void *astarg" because the
235 * underlying stack plugins need to pilfer the lksb off of the lock_res.
236 * If some other structure needs to be passed as an astarg, the plugins
237 * will need to be given a different avenue to the lksb.
239 int ocfs2_dlm_lock(struct ocfs2_cluster_connection *conn,
241 union ocfs2_dlm_lksb *lksb,
244 unsigned int namelen,
245 struct ocfs2_lock_res *astarg)
247 BUG_ON(lproto == NULL);
249 return active_stack->sp_ops->dlm_lock(conn, mode, lksb, flags,
250 name, namelen, astarg);
252 EXPORT_SYMBOL_GPL(ocfs2_dlm_lock);
254 int ocfs2_dlm_unlock(struct ocfs2_cluster_connection *conn,
255 union ocfs2_dlm_lksb *lksb,
257 struct ocfs2_lock_res *astarg)
259 BUG_ON(lproto == NULL);
261 return active_stack->sp_ops->dlm_unlock(conn, lksb, flags, astarg);
263 EXPORT_SYMBOL_GPL(ocfs2_dlm_unlock);
265 int ocfs2_dlm_lock_status(union ocfs2_dlm_lksb *lksb)
267 return active_stack->sp_ops->lock_status(lksb);
269 EXPORT_SYMBOL_GPL(ocfs2_dlm_lock_status);
272 * Why don't we cast to ocfs2_meta_lvb? The "clean" answer is that we
273 * don't cast at the glue level. The real answer is that the header
274 * ordering is nigh impossible.
276 void *ocfs2_dlm_lvb(union ocfs2_dlm_lksb *lksb)
278 return active_stack->sp_ops->lock_lvb(lksb);
280 EXPORT_SYMBOL_GPL(ocfs2_dlm_lvb);
282 void ocfs2_dlm_dump_lksb(union ocfs2_dlm_lksb *lksb)
284 active_stack->sp_ops->dump_lksb(lksb);
286 EXPORT_SYMBOL_GPL(ocfs2_dlm_dump_lksb);
288 int ocfs2_cluster_connect(const char *stack_name,
291 void (*recovery_handler)(int node_num,
292 void *recovery_data),
294 struct ocfs2_cluster_connection **conn)
297 struct ocfs2_cluster_connection *new_conn;
299 BUG_ON(group == NULL);
300 BUG_ON(conn == NULL);
301 BUG_ON(recovery_handler == NULL);
303 if (grouplen > GROUP_NAME_MAX) {
308 new_conn = kzalloc(sizeof(struct ocfs2_cluster_connection),
315 memcpy(new_conn->cc_name, group, grouplen);
316 new_conn->cc_namelen = grouplen;
317 new_conn->cc_recovery_handler = recovery_handler;
318 new_conn->cc_recovery_data = recovery_data;
320 /* Start the new connection at our maximum compatibility level */
321 new_conn->cc_version = lproto->lp_max_version;
323 /* This will pin the stack driver if successful */
324 rc = ocfs2_stack_driver_get(stack_name);
328 rc = active_stack->sp_ops->connect(new_conn);
330 ocfs2_stack_driver_put();
343 EXPORT_SYMBOL_GPL(ocfs2_cluster_connect);
345 /* If hangup_pending is 0, the stack driver will be dropped */
346 int ocfs2_cluster_disconnect(struct ocfs2_cluster_connection *conn,
351 BUG_ON(conn == NULL);
353 ret = active_stack->sp_ops->disconnect(conn, hangup_pending);
355 /* XXX Should we free it anyway? */
359 ocfs2_stack_driver_put();
364 EXPORT_SYMBOL_GPL(ocfs2_cluster_disconnect);
366 void ocfs2_cluster_hangup(const char *group, int grouplen)
368 BUG_ON(group == NULL);
369 BUG_ON(group[grouplen] != '\0');
371 if (active_stack->sp_ops->hangup)
372 active_stack->sp_ops->hangup(group, grouplen);
374 /* cluster_disconnect() was called with hangup_pending==1 */
375 ocfs2_stack_driver_put();
377 EXPORT_SYMBOL_GPL(ocfs2_cluster_hangup);
379 int ocfs2_cluster_this_node(unsigned int *node)
381 return active_stack->sp_ops->this_node(node);
383 EXPORT_SYMBOL_GPL(ocfs2_cluster_this_node);
390 static ssize_t ocfs2_max_locking_protocol_show(struct kobject *kobj,
391 struct kobj_attribute *attr,
396 spin_lock(&ocfs2_stack_lock);
398 ret = snprintf(buf, PAGE_SIZE, "%u.%u\n",
399 lproto->lp_max_version.pv_major,
400 lproto->lp_max_version.pv_minor);
401 spin_unlock(&ocfs2_stack_lock);
406 static struct kobj_attribute ocfs2_attr_max_locking_protocol =
407 __ATTR(max_locking_protocol, S_IFREG | S_IRUGO,
408 ocfs2_max_locking_protocol_show, NULL);
410 static ssize_t ocfs2_loaded_cluster_plugins_show(struct kobject *kobj,
411 struct kobj_attribute *attr,
414 ssize_t ret = 0, total = 0, remain = PAGE_SIZE;
415 struct ocfs2_stack_plugin *p;
417 spin_lock(&ocfs2_stack_lock);
418 list_for_each_entry(p, &ocfs2_stack_list, sp_list) {
419 ret = snprintf(buf, remain, "%s\n",
426 /* snprintf() didn't fit */
433 spin_unlock(&ocfs2_stack_lock);
438 static struct kobj_attribute ocfs2_attr_loaded_cluster_plugins =
439 __ATTR(loaded_cluster_plugins, S_IFREG | S_IRUGO,
440 ocfs2_loaded_cluster_plugins_show, NULL);
442 static ssize_t ocfs2_active_cluster_plugin_show(struct kobject *kobj,
443 struct kobj_attribute *attr,
448 spin_lock(&ocfs2_stack_lock);
450 ret = snprintf(buf, PAGE_SIZE, "%s\n",
451 active_stack->sp_name);
452 if (ret == PAGE_SIZE)
455 spin_unlock(&ocfs2_stack_lock);
460 static struct kobj_attribute ocfs2_attr_active_cluster_plugin =
461 __ATTR(active_cluster_plugin, S_IFREG | S_IRUGO,
462 ocfs2_active_cluster_plugin_show, NULL);
464 static ssize_t ocfs2_cluster_stack_show(struct kobject *kobj,
465 struct kobj_attribute *attr,
469 spin_lock(&ocfs2_stack_lock);
470 ret = snprintf(buf, PAGE_SIZE, "%s\n", cluster_stack_name);
471 spin_unlock(&ocfs2_stack_lock);
476 static ssize_t ocfs2_cluster_stack_store(struct kobject *kobj,
477 struct kobj_attribute *attr,
478 const char *buf, size_t count)
486 if (buf[len - 1] == '\n')
489 if ((len != OCFS2_STACK_LABEL_LEN) ||
490 (strnlen(buf, len) != len))
493 spin_lock(&ocfs2_stack_lock);
495 if (!strncmp(buf, cluster_stack_name, len))
500 memcpy(cluster_stack_name, buf, len);
503 spin_unlock(&ocfs2_stack_lock);
509 static struct kobj_attribute ocfs2_attr_cluster_stack =
510 __ATTR(cluster_stack, S_IFREG | S_IRUGO | S_IWUSR,
511 ocfs2_cluster_stack_show,
512 ocfs2_cluster_stack_store);
514 static struct attribute *ocfs2_attrs[] = {
515 &ocfs2_attr_max_locking_protocol.attr,
516 &ocfs2_attr_loaded_cluster_plugins.attr,
517 &ocfs2_attr_active_cluster_plugin.attr,
518 &ocfs2_attr_cluster_stack.attr,
522 static struct attribute_group ocfs2_attr_group = {
523 .attrs = ocfs2_attrs,
526 static struct kset *ocfs2_kset;
528 static void ocfs2_sysfs_exit(void)
530 kset_unregister(ocfs2_kset);
533 static int ocfs2_sysfs_init(void)
537 ocfs2_kset = kset_create_and_add("ocfs2", NULL, fs_kobj);
541 ret = sysfs_create_group(&ocfs2_kset->kobj, &ocfs2_attr_group);
548 kset_unregister(ocfs2_kset);
555 * The sysctl lives at /proc/sys/fs/ocfs2/nm/hb_ctl_path. The 'nm' doesn't
556 * make as much sense in a multiple cluster stack world, but it's safer
557 * and easier to preserve the name.
560 #define FS_OCFS2_NM 1
562 #define OCFS2_MAX_HB_CTL_PATH 256
563 static char ocfs2_hb_ctl_path[OCFS2_MAX_HB_CTL_PATH] = "/sbin/ocfs2_hb_ctl";
565 static ctl_table ocfs2_nm_table[] = {
568 .procname = "hb_ctl_path",
569 .data = ocfs2_hb_ctl_path,
570 .maxlen = OCFS2_MAX_HB_CTL_PATH,
572 .proc_handler = &proc_dostring,
573 .strategy = &sysctl_string,
578 static ctl_table ocfs2_mod_table[] = {
580 .ctl_name = FS_OCFS2_NM,
585 .child = ocfs2_nm_table
590 static ctl_table ocfs2_kern_table[] = {
592 .ctl_name = FS_OCFS2,
597 .child = ocfs2_mod_table
602 static ctl_table ocfs2_root_table[] = {
609 .child = ocfs2_kern_table
614 static struct ctl_table_header *ocfs2_table_header = NULL;
616 const char *ocfs2_get_hb_ctl_path(void)
618 return ocfs2_hb_ctl_path;
620 EXPORT_SYMBOL_GPL(ocfs2_get_hb_ctl_path);
627 static int __init ocfs2_stack_glue_init(void)
629 strcpy(cluster_stack_name, OCFS2_STACK_PLUGIN_O2CB);
631 ocfs2_table_header = register_sysctl_table(ocfs2_root_table);
632 if (!ocfs2_table_header) {
634 "ocfs2 stack glue: unable to register sysctl\n");
635 return -ENOMEM; /* or something. */
638 return ocfs2_sysfs_init();
641 static void __exit ocfs2_stack_glue_exit(void)
645 if (ocfs2_table_header)
646 unregister_sysctl_table(ocfs2_table_header);
649 MODULE_AUTHOR("Oracle");
650 MODULE_DESCRIPTION("ocfs2 cluter stack glue layer");
651 MODULE_LICENSE("GPL");
652 module_init(ocfs2_stack_glue_init);
653 module_exit(ocfs2_stack_glue_exit);