ocfs2: Move the hb_ctl_path sysctl into the stack glue.
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / ocfs2 / stackglue.c
CommitLineData
24ef1815
JB
1/* -*- 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
286eaa95
JB
21#include <linux/list.h>
22#include <linux/spinlock.h>
23#include <linux/module.h>
4670c46d 24#include <linux/slab.h>
6953b4c0 25#include <linux/kmod.h>
74ae4e10
JB
26#include <linux/fs.h>
27#include <linux/kobject.h>
28#include <linux/sysfs.h>
3878f110 29#include <linux/sysctl.h>
4670c46d 30
9c6c877c
JB
31#include "ocfs2_fs.h"
32
286eaa95 33#include "stackglue.h"
4670c46d 34
9c6c877c
JB
35#define OCFS2_STACK_PLUGIN_O2CB "o2cb"
36#define OCFS2_STACK_PLUGIN_USER "user"
37
286eaa95
JB
38static struct ocfs2_locking_protocol *lproto;
39static DEFINE_SPINLOCK(ocfs2_stack_lock);
40static LIST_HEAD(ocfs2_stack_list);
9c6c877c 41static char cluster_stack_name[OCFS2_STACK_LABEL_LEN + 1];
19fdb624 42
286eaa95
JB
43/*
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.
46 */
47static struct ocfs2_stack_plugin *active_stack;
48
49static struct ocfs2_stack_plugin *ocfs2_stack_lookup(const char *name)
50{
51 struct ocfs2_stack_plugin *p;
52
53 assert_spin_locked(&ocfs2_stack_lock);
54
55 list_for_each_entry(p, &ocfs2_stack_list, sp_list) {
56 if (!strcmp(p->sp_name, name))
57 return p;
58 }
59
60 return NULL;
61}
62
9c6c877c
JB
63static int ocfs2_stack_driver_request(const char *stack_name,
64 const char *plugin_name)
286eaa95
JB
65{
66 int rc;
67 struct ocfs2_stack_plugin *p;
68
69 spin_lock(&ocfs2_stack_lock);
70
9c6c877c
JB
71 /*
72 * If the stack passed by the filesystem isn't the selected one,
73 * we can't continue.
74 */
75 if (strcmp(stack_name, cluster_stack_name)) {
76 rc = -EBUSY;
77 goto out;
78 }
79
286eaa95
JB
80 if (active_stack) {
81 /*
82 * If the active stack isn't the one we want, it cannot
83 * be selected right now.
84 */
9c6c877c 85 if (!strcmp(active_stack->sp_name, plugin_name))
286eaa95
JB
86 rc = 0;
87 else
88 rc = -EBUSY;
89 goto out;
90 }
91
9c6c877c 92 p = ocfs2_stack_lookup(plugin_name);
286eaa95
JB
93 if (!p || !try_module_get(p->sp_owner)) {
94 rc = -ENOENT;
95 goto out;
96 }
97
98 /* Ok, the stack is pinned */
99 p->sp_count++;
100 active_stack = p;
101
102 rc = 0;
103
104out:
105 spin_unlock(&ocfs2_stack_lock);
106 return rc;
107}
108
109/*
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.
113 */
9c6c877c 114static int ocfs2_stack_driver_get(const char *stack_name)
286eaa95
JB
115{
116 int rc;
9c6c877c
JB
117 char *plugin_name = OCFS2_STACK_PLUGIN_O2CB;
118
119 /*
120 * Classic stack does not pass in a stack name. This is
121 * compatible with older tools as well.
122 */
123 if (!stack_name || !*stack_name)
124 stack_name = OCFS2_STACK_PLUGIN_O2CB;
125
126 if (strlen(stack_name) != OCFS2_STACK_LABEL_LEN) {
127 printk(KERN_ERR
128 "ocfs2 passed an invalid cluster stack label: \"%s\"\n",
129 stack_name);
130 return -EINVAL;
131 }
286eaa95 132
9c6c877c
JB
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;
136
137 rc = ocfs2_stack_driver_request(stack_name, plugin_name);
286eaa95 138 if (rc == -ENOENT) {
9c6c877c
JB
139 request_module("ocfs2_stack_%s", plugin_name);
140 rc = ocfs2_stack_driver_request(stack_name, plugin_name);
286eaa95
JB
141 }
142
143 if (rc == -ENOENT) {
144 printk(KERN_ERR
145 "ocfs2: Cluster stack driver \"%s\" cannot be found\n",
9c6c877c 146 plugin_name);
286eaa95
JB
147 } else if (rc == -EBUSY) {
148 printk(KERN_ERR
9c6c877c 149 "ocfs2: A different cluster stack is in use\n");
286eaa95
JB
150 }
151
152 return rc;
153}
24ef1815 154
286eaa95
JB
155static void ocfs2_stack_driver_put(void)
156{
157 spin_lock(&ocfs2_stack_lock);
158 BUG_ON(active_stack == NULL);
159 BUG_ON(active_stack->sp_count == 0);
160
161 active_stack->sp_count--;
162 if (!active_stack->sp_count) {
163 module_put(active_stack->sp_owner);
164 active_stack = NULL;
165 }
166 spin_unlock(&ocfs2_stack_lock);
167}
168
169int ocfs2_stack_glue_register(struct ocfs2_stack_plugin *plugin)
170{
171 int rc;
172
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",
179 plugin->sp_name);
180 rc = 0;
181 } else {
182 printk(KERN_ERR "ocfs2: Stack \"%s\" already registered\n",
183 plugin->sp_name);
184 rc = -EEXIST;
185 }
186 spin_unlock(&ocfs2_stack_lock);
187
188 return rc;
189}
190EXPORT_SYMBOL_GPL(ocfs2_stack_glue_register);
191
192void ocfs2_stack_glue_unregister(struct ocfs2_stack_plugin *plugin)
193{
194 struct ocfs2_stack_plugin *p;
195
196 spin_lock(&ocfs2_stack_lock);
197 p = ocfs2_stack_lookup(plugin->sp_name);
198 if (p) {
199 BUG_ON(p != plugin);
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",
204 plugin->sp_name);
205 } else {
206 printk(KERN_ERR "Stack \"%s\" is not registered\n",
207 plugin->sp_name);
208 }
209 spin_unlock(&ocfs2_stack_lock);
210}
211EXPORT_SYMBOL_GPL(ocfs2_stack_glue_unregister);
212
213void ocfs2_stack_glue_set_locking_protocol(struct ocfs2_locking_protocol *proto)
214{
215 struct ocfs2_stack_plugin *p;
216
217 BUG_ON(proto == NULL);
218
219 spin_lock(&ocfs2_stack_lock);
220 BUG_ON(active_stack != NULL);
221
222 lproto = proto;
223 list_for_each_entry(p, &ocfs2_stack_list, sp_list) {
224 p->sp_proto = lproto;
225 }
226
227 spin_unlock(&ocfs2_stack_lock);
228}
229EXPORT_SYMBOL_GPL(ocfs2_stack_glue_set_locking_protocol);
7431cd7e 230
24ef1815 231
cf4d8d75
DT
232/*
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.
238 */
553aa7e4
JB
239int ocfs2_dlm_lock(struct ocfs2_cluster_connection *conn,
240 int mode,
241 union ocfs2_dlm_lksb *lksb,
242 u32 flags,
243 void *name,
244 unsigned int namelen,
cf4d8d75 245 struct ocfs2_lock_res *astarg)
553aa7e4 246{
286eaa95 247 BUG_ON(lproto == NULL);
bd3e7610 248
286eaa95
JB
249 return active_stack->sp_ops->dlm_lock(conn, mode, lksb, flags,
250 name, namelen, astarg);
24ef1815 251}
286eaa95 252EXPORT_SYMBOL_GPL(ocfs2_dlm_lock);
24ef1815 253
553aa7e4
JB
254int ocfs2_dlm_unlock(struct ocfs2_cluster_connection *conn,
255 union ocfs2_dlm_lksb *lksb,
256 u32 flags,
cf4d8d75 257 struct ocfs2_lock_res *astarg)
553aa7e4 258{
286eaa95 259 BUG_ON(lproto == NULL);
553aa7e4 260
286eaa95 261 return active_stack->sp_ops->dlm_unlock(conn, lksb, flags, astarg);
8f2c9c1b 262}
286eaa95 263EXPORT_SYMBOL_GPL(ocfs2_dlm_unlock);
8f2c9c1b 264
553aa7e4
JB
265int ocfs2_dlm_lock_status(union ocfs2_dlm_lksb *lksb)
266{
286eaa95 267 return active_stack->sp_ops->lock_status(lksb);
553aa7e4 268}
286eaa95 269EXPORT_SYMBOL_GPL(ocfs2_dlm_lock_status);
553aa7e4 270
8f2c9c1b
JB
271/*
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.
275 */
553aa7e4
JB
276void *ocfs2_dlm_lvb(union ocfs2_dlm_lksb *lksb)
277{
286eaa95 278 return active_stack->sp_ops->lock_lvb(lksb);
cf0acdcd 279}
286eaa95 280EXPORT_SYMBOL_GPL(ocfs2_dlm_lvb);
cf0acdcd 281
553aa7e4
JB
282void ocfs2_dlm_dump_lksb(union ocfs2_dlm_lksb *lksb)
283{
286eaa95 284 active_stack->sp_ops->dump_lksb(lksb);
553aa7e4 285}
286eaa95 286EXPORT_SYMBOL_GPL(ocfs2_dlm_dump_lksb);
553aa7e4 287
9c6c877c
JB
288int ocfs2_cluster_connect(const char *stack_name,
289 const char *group,
4670c46d
JB
290 int grouplen,
291 void (*recovery_handler)(int node_num,
292 void *recovery_data),
293 void *recovery_data,
294 struct ocfs2_cluster_connection **conn)
295{
296 int rc = 0;
297 struct ocfs2_cluster_connection *new_conn;
4670c46d
JB
298
299 BUG_ON(group == NULL);
300 BUG_ON(conn == NULL);
301 BUG_ON(recovery_handler == NULL);
302
303 if (grouplen > GROUP_NAME_MAX) {
304 rc = -EINVAL;
305 goto out;
306 }
307
308 new_conn = kzalloc(sizeof(struct ocfs2_cluster_connection),
309 GFP_KERNEL);
310 if (!new_conn) {
311 rc = -ENOMEM;
312 goto out;
313 }
314
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;
319
320 /* Start the new connection at our maximum compatibility level */
286eaa95
JB
321 new_conn->cc_version = lproto->lp_max_version;
322
323 /* This will pin the stack driver if successful */
9c6c877c 324 rc = ocfs2_stack_driver_get(stack_name);
286eaa95
JB
325 if (rc)
326 goto out_free;
4670c46d 327
286eaa95 328 rc = active_stack->sp_ops->connect(new_conn);
553aa7e4 329 if (rc) {
286eaa95 330 ocfs2_stack_driver_put();
4670c46d
JB
331 goto out_free;
332 }
333
4670c46d
JB
334 *conn = new_conn;
335
336out_free:
553aa7e4 337 if (rc)
4670c46d 338 kfree(new_conn);
4670c46d
JB
339
340out:
341 return rc;
342}
286eaa95 343EXPORT_SYMBOL_GPL(ocfs2_cluster_connect);
4670c46d 344
286eaa95
JB
345/* If hangup_pending is 0, the stack driver will be dropped */
346int ocfs2_cluster_disconnect(struct ocfs2_cluster_connection *conn,
347 int hangup_pending)
553aa7e4
JB
348{
349 int ret;
350
351 BUG_ON(conn == NULL);
352
286eaa95 353 ret = active_stack->sp_ops->disconnect(conn, hangup_pending);
553aa7e4
JB
354
355 /* XXX Should we free it anyway? */
286eaa95 356 if (!ret) {
553aa7e4 357 kfree(conn);
286eaa95
JB
358 if (!hangup_pending)
359 ocfs2_stack_driver_put();
360 }
553aa7e4
JB
361
362 return ret;
363}
286eaa95 364EXPORT_SYMBOL_GPL(ocfs2_cluster_disconnect);
553aa7e4 365
6953b4c0
JB
366void ocfs2_cluster_hangup(const char *group, int grouplen)
367{
368 BUG_ON(group == NULL);
369 BUG_ON(group[grouplen] != '\0');
370
cf4d8d75
DT
371 if (active_stack->sp_ops->hangup)
372 active_stack->sp_ops->hangup(group, grouplen);
286eaa95
JB
373
374 /* cluster_disconnect() was called with hangup_pending==1 */
375 ocfs2_stack_driver_put();
19fdb624 376}
286eaa95 377EXPORT_SYMBOL_GPL(ocfs2_cluster_hangup);
19fdb624 378
553aa7e4
JB
379int ocfs2_cluster_this_node(unsigned int *node)
380{
286eaa95 381 return active_stack->sp_ops->this_node(node);
553aa7e4 382}
286eaa95 383EXPORT_SYMBOL_GPL(ocfs2_cluster_this_node);
553aa7e4 384
286eaa95 385
74ae4e10
JB
386/*
387 * Sysfs bits
388 */
389
390static ssize_t ocfs2_max_locking_protocol_show(struct kobject *kobj,
391 struct kobj_attribute *attr,
392 char *buf)
393{
394 ssize_t ret = 0;
395
396 spin_lock(&ocfs2_stack_lock);
397 if (lproto)
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);
402
403 return ret;
404}
405
406static struct kobj_attribute ocfs2_attr_max_locking_protocol =
407 __ATTR(max_locking_protocol, S_IFREG | S_IRUGO,
408 ocfs2_max_locking_protocol_show, NULL);
409
410static ssize_t ocfs2_loaded_cluster_plugins_show(struct kobject *kobj,
411 struct kobj_attribute *attr,
412 char *buf)
24ef1815 413{
74ae4e10
JB
414 ssize_t ret = 0, total = 0, remain = PAGE_SIZE;
415 struct ocfs2_stack_plugin *p;
416
417 spin_lock(&ocfs2_stack_lock);
418 list_for_each_entry(p, &ocfs2_stack_list, sp_list) {
419 ret = snprintf(buf, remain, "%s\n",
420 p->sp_name);
421 if (ret < 0) {
422 total = ret;
423 break;
424 }
425 if (ret == remain) {
426 /* snprintf() didn't fit */
427 total = -E2BIG;
428 break;
429 }
430 total += ret;
431 remain -= ret;
432 }
433 spin_unlock(&ocfs2_stack_lock);
434
435 return total;
436}
437
438static struct kobj_attribute ocfs2_attr_loaded_cluster_plugins =
439 __ATTR(loaded_cluster_plugins, S_IFREG | S_IRUGO,
440 ocfs2_loaded_cluster_plugins_show, NULL);
441
442static ssize_t ocfs2_active_cluster_plugin_show(struct kobject *kobj,
443 struct kobj_attribute *attr,
444 char *buf)
445{
446 ssize_t ret = 0;
447
448 spin_lock(&ocfs2_stack_lock);
449 if (active_stack) {
450 ret = snprintf(buf, PAGE_SIZE, "%s\n",
451 active_stack->sp_name);
452 if (ret == PAGE_SIZE)
453 ret = -E2BIG;
454 }
455 spin_unlock(&ocfs2_stack_lock);
456
457 return ret;
458}
459
460static struct kobj_attribute ocfs2_attr_active_cluster_plugin =
461 __ATTR(active_cluster_plugin, S_IFREG | S_IRUGO,
462 ocfs2_active_cluster_plugin_show, NULL);
463
9c6c877c
JB
464static ssize_t ocfs2_cluster_stack_show(struct kobject *kobj,
465 struct kobj_attribute *attr,
466 char *buf)
467{
468 ssize_t ret;
469 spin_lock(&ocfs2_stack_lock);
470 ret = snprintf(buf, PAGE_SIZE, "%s\n", cluster_stack_name);
471 spin_unlock(&ocfs2_stack_lock);
472
473 return ret;
474}
475
476static ssize_t ocfs2_cluster_stack_store(struct kobject *kobj,
477 struct kobj_attribute *attr,
478 const char *buf, size_t count)
479{
480 size_t len = count;
481 ssize_t ret;
482
483 if (len == 0)
484 return len;
485
486 if (buf[len - 1] == '\n')
487 len--;
488
489 if ((len != OCFS2_STACK_LABEL_LEN) ||
490 (strnlen(buf, len) != len))
491 return -EINVAL;
492
493 spin_lock(&ocfs2_stack_lock);
494 if (active_stack) {
495 if (!strncmp(buf, cluster_stack_name, len))
496 ret = count;
497 else
498 ret = -EBUSY;
499 } else {
500 memcpy(cluster_stack_name, buf, len);
501 ret = count;
502 }
503 spin_unlock(&ocfs2_stack_lock);
504
505 return ret;
506}
507
508
509static 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);
513
74ae4e10
JB
514static 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,
9c6c877c 518 &ocfs2_attr_cluster_stack.attr,
74ae4e10
JB
519 NULL,
520};
521
522static struct attribute_group ocfs2_attr_group = {
523 .attrs = ocfs2_attrs,
524};
525
526static struct kset *ocfs2_kset;
527
528static void ocfs2_sysfs_exit(void)
529{
530 kset_unregister(ocfs2_kset);
531}
532
533static int ocfs2_sysfs_init(void)
534{
535 int ret;
536
537 ocfs2_kset = kset_create_and_add("ocfs2", NULL, fs_kobj);
538 if (!ocfs2_kset)
539 return -ENOMEM;
540
541 ret = sysfs_create_group(&ocfs2_kset->kobj, &ocfs2_attr_group);
542 if (ret)
543 goto error;
544
286eaa95 545 return 0;
74ae4e10
JB
546
547error:
548 kset_unregister(ocfs2_kset);
549 return ret;
550}
551
3878f110
JB
552/*
553 * Sysctl bits
554 *
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.
558 */
559
560#define FS_OCFS2_NM 1
561
562#define OCFS2_MAX_HB_CTL_PATH 256
563static char ocfs2_hb_ctl_path[OCFS2_MAX_HB_CTL_PATH] = "/sbin/ocfs2_hb_ctl";
564
565static ctl_table ocfs2_nm_table[] = {
566 {
567 .ctl_name = 1,
568 .procname = "hb_ctl_path",
569 .data = ocfs2_hb_ctl_path,
570 .maxlen = OCFS2_MAX_HB_CTL_PATH,
571 .mode = 0644,
572 .proc_handler = &proc_dostring,
573 .strategy = &sysctl_string,
574 },
575 { .ctl_name = 0 }
576};
577
578static ctl_table ocfs2_mod_table[] = {
579 {
580 .ctl_name = FS_OCFS2_NM,
581 .procname = "nm",
582 .data = NULL,
583 .maxlen = 0,
584 .mode = 0555,
585 .child = ocfs2_nm_table
586 },
587 { .ctl_name = 0}
588};
589
590static ctl_table ocfs2_kern_table[] = {
591 {
592 .ctl_name = FS_OCFS2,
593 .procname = "ocfs2",
594 .data = NULL,
595 .maxlen = 0,
596 .mode = 0555,
597 .child = ocfs2_mod_table
598 },
599 { .ctl_name = 0}
600};
601
602static ctl_table ocfs2_root_table[] = {
603 {
604 .ctl_name = CTL_FS,
605 .procname = "fs",
606 .data = NULL,
607 .maxlen = 0,
608 .mode = 0555,
609 .child = ocfs2_kern_table
610 },
611 { .ctl_name = 0 }
612};
613
614static struct ctl_table_header *ocfs2_table_header = NULL;
615
616const char *ocfs2_get_hb_ctl_path(void)
617{
618 return ocfs2_hb_ctl_path;
619}
620EXPORT_SYMBOL_GPL(ocfs2_get_hb_ctl_path);
621
622
623/*
624 * Initialization
625 */
626
74ae4e10
JB
627static int __init ocfs2_stack_glue_init(void)
628{
9c6c877c
JB
629 strcpy(cluster_stack_name, OCFS2_STACK_PLUGIN_O2CB);
630
3878f110
JB
631 ocfs2_table_header = register_sysctl_table(ocfs2_root_table);
632 if (!ocfs2_table_header) {
633 printk(KERN_ERR
634 "ocfs2 stack glue: unable to register sysctl\n");
635 return -ENOMEM; /* or something. */
636 }
637
74ae4e10 638 return ocfs2_sysfs_init();
286eaa95 639}
24ef1815 640
286eaa95
JB
641static void __exit ocfs2_stack_glue_exit(void)
642{
643 lproto = NULL;
74ae4e10 644 ocfs2_sysfs_exit();
3878f110
JB
645 if (ocfs2_table_header)
646 unregister_sysctl_table(ocfs2_table_header);
24ef1815
JB
647}
648
286eaa95
JB
649MODULE_AUTHOR("Oracle");
650MODULE_DESCRIPTION("ocfs2 cluter stack glue layer");
651MODULE_LICENSE("GPL");
652module_init(ocfs2_stack_glue_init);
653module_exit(ocfs2_stack_glue_exit);