ocfs2: Pass the locking protocol into ocfs2_cluster_connect().
[linux-3.10.git] / fs / ocfs2 / stackglue.c
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, 2009 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
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>
26 #include <linux/fs.h>
27 #include <linux/kobject.h>
28 #include <linux/sysfs.h>
29 #include <linux/sysctl.h>
30
31 #include "ocfs2_fs.h"
32
33 #include "stackglue.h"
34
35 #define OCFS2_STACK_PLUGIN_O2CB         "o2cb"
36 #define OCFS2_STACK_PLUGIN_USER         "user"
37 #define OCFS2_MAX_HB_CTL_PATH           256
38
39 static struct ocfs2_protocol_version locking_max_version;
40 static DEFINE_SPINLOCK(ocfs2_stack_lock);
41 static LIST_HEAD(ocfs2_stack_list);
42 static char cluster_stack_name[OCFS2_STACK_LABEL_LEN + 1];
43 static char ocfs2_hb_ctl_path[OCFS2_MAX_HB_CTL_PATH] = "/sbin/ocfs2_hb_ctl";
44
45 /*
46  * The stack currently in use.  If not null, active_stack->sp_count > 0,
47  * the module is pinned, and the locking protocol cannot be changed.
48  */
49 static struct ocfs2_stack_plugin *active_stack;
50
51 static struct ocfs2_stack_plugin *ocfs2_stack_lookup(const char *name)
52 {
53         struct ocfs2_stack_plugin *p;
54
55         assert_spin_locked(&ocfs2_stack_lock);
56
57         list_for_each_entry(p, &ocfs2_stack_list, sp_list) {
58                 if (!strcmp(p->sp_name, name))
59                         return p;
60         }
61
62         return NULL;
63 }
64
65 static int ocfs2_stack_driver_request(const char *stack_name,
66                                       const char *plugin_name)
67 {
68         int rc;
69         struct ocfs2_stack_plugin *p;
70
71         spin_lock(&ocfs2_stack_lock);
72
73         /*
74          * If the stack passed by the filesystem isn't the selected one,
75          * we can't continue.
76          */
77         if (strcmp(stack_name, cluster_stack_name)) {
78                 rc = -EBUSY;
79                 goto out;
80         }
81
82         if (active_stack) {
83                 /*
84                  * If the active stack isn't the one we want, it cannot
85                  * be selected right now.
86                  */
87                 if (!strcmp(active_stack->sp_name, plugin_name))
88                         rc = 0;
89                 else
90                         rc = -EBUSY;
91                 goto out;
92         }
93
94         p = ocfs2_stack_lookup(plugin_name);
95         if (!p || !try_module_get(p->sp_owner)) {
96                 rc = -ENOENT;
97                 goto out;
98         }
99
100         active_stack = p;
101         rc = 0;
102
103 out:
104         /* If we found it, pin it */
105         if (!rc)
106                 active_stack->sp_count++;
107
108         spin_unlock(&ocfs2_stack_lock);
109         return rc;
110 }
111
112 /*
113  * This function looks up the appropriate stack and makes it active.  If
114  * there is no stack, it tries to load it.  It will fail if the stack still
115  * cannot be found.  It will also fail if a different stack is in use.
116  */
117 static int ocfs2_stack_driver_get(const char *stack_name)
118 {
119         int rc;
120         char *plugin_name = OCFS2_STACK_PLUGIN_O2CB;
121
122         /*
123          * Classic stack does not pass in a stack name.  This is
124          * compatible with older tools as well.
125          */
126         if (!stack_name || !*stack_name)
127                 stack_name = OCFS2_STACK_PLUGIN_O2CB;
128
129         if (strlen(stack_name) != OCFS2_STACK_LABEL_LEN) {
130                 printk(KERN_ERR
131                        "ocfs2 passed an invalid cluster stack label: \"%s\"\n",
132                        stack_name);
133                 return -EINVAL;
134         }
135
136         /* Anything that isn't the classic stack is a user stack */
137         if (strcmp(stack_name, OCFS2_STACK_PLUGIN_O2CB))
138                 plugin_name = OCFS2_STACK_PLUGIN_USER;
139
140         rc = ocfs2_stack_driver_request(stack_name, plugin_name);
141         if (rc == -ENOENT) {
142                 request_module("ocfs2_stack_%s", plugin_name);
143                 rc = ocfs2_stack_driver_request(stack_name, plugin_name);
144         }
145
146         if (rc == -ENOENT) {
147                 printk(KERN_ERR
148                        "ocfs2: Cluster stack driver \"%s\" cannot be found\n",
149                        plugin_name);
150         } else if (rc == -EBUSY) {
151                 printk(KERN_ERR
152                        "ocfs2: A different cluster stack is in use\n");
153         }
154
155         return rc;
156 }
157
158 static void ocfs2_stack_driver_put(void)
159 {
160         spin_lock(&ocfs2_stack_lock);
161         BUG_ON(active_stack == NULL);
162         BUG_ON(active_stack->sp_count == 0);
163
164         active_stack->sp_count--;
165         if (!active_stack->sp_count) {
166                 module_put(active_stack->sp_owner);
167                 active_stack = NULL;
168         }
169         spin_unlock(&ocfs2_stack_lock);
170 }
171
172 int ocfs2_stack_glue_register(struct ocfs2_stack_plugin *plugin)
173 {
174         int rc;
175
176         spin_lock(&ocfs2_stack_lock);
177         if (!ocfs2_stack_lookup(plugin->sp_name)) {
178                 plugin->sp_count = 0;
179                 plugin->sp_max_proto = locking_max_version;
180                 list_add(&plugin->sp_list, &ocfs2_stack_list);
181                 printk(KERN_INFO "ocfs2: Registered cluster interface %s\n",
182                        plugin->sp_name);
183                 rc = 0;
184         } else {
185                 printk(KERN_ERR "ocfs2: Stack \"%s\" already registered\n",
186                        plugin->sp_name);
187                 rc = -EEXIST;
188         }
189         spin_unlock(&ocfs2_stack_lock);
190
191         return rc;
192 }
193 EXPORT_SYMBOL_GPL(ocfs2_stack_glue_register);
194
195 void ocfs2_stack_glue_unregister(struct ocfs2_stack_plugin *plugin)
196 {
197         struct ocfs2_stack_plugin *p;
198
199         spin_lock(&ocfs2_stack_lock);
200         p = ocfs2_stack_lookup(plugin->sp_name);
201         if (p) {
202                 BUG_ON(p != plugin);
203                 BUG_ON(plugin == active_stack);
204                 BUG_ON(plugin->sp_count != 0);
205                 list_del_init(&plugin->sp_list);
206                 printk(KERN_INFO "ocfs2: Unregistered cluster interface %s\n",
207                        plugin->sp_name);
208         } else {
209                 printk(KERN_ERR "Stack \"%s\" is not registered\n",
210                        plugin->sp_name);
211         }
212         spin_unlock(&ocfs2_stack_lock);
213 }
214 EXPORT_SYMBOL_GPL(ocfs2_stack_glue_unregister);
215
216 void ocfs2_stack_glue_set_max_proto_version(struct ocfs2_protocol_version *max_proto)
217 {
218         struct ocfs2_stack_plugin *p;
219
220         spin_lock(&ocfs2_stack_lock);
221         if (memcmp(max_proto, &locking_max_version,
222                    sizeof(struct ocfs2_protocol_version))) {
223                 BUG_ON(locking_max_version.pv_major != 0);
224
225                 locking_max_version = *max_proto;
226                 list_for_each_entry(p, &ocfs2_stack_list, sp_list) {
227                         p->sp_max_proto = locking_max_version;
228                 }
229         }
230         spin_unlock(&ocfs2_stack_lock);
231 }
232 EXPORT_SYMBOL_GPL(ocfs2_stack_glue_set_max_proto_version);
233
234
235 /*
236  * The ocfs2_dlm_lock() and ocfs2_dlm_unlock() functions take no argument
237  * for the ast and bast functions.  They will pass the lksb to the ast
238  * and bast.  The caller can wrap the lksb with their own structure to
239  * get more information.
240  */
241 int ocfs2_dlm_lock(struct ocfs2_cluster_connection *conn,
242                    int mode,
243                    struct ocfs2_dlm_lksb *lksb,
244                    u32 flags,
245                    void *name,
246                    unsigned int namelen)
247 {
248         if (!lksb->lksb_conn)
249                 lksb->lksb_conn = conn;
250         else
251                 BUG_ON(lksb->lksb_conn != conn);
252         return active_stack->sp_ops->dlm_lock(conn, mode, lksb, flags,
253                                               name, namelen);
254 }
255 EXPORT_SYMBOL_GPL(ocfs2_dlm_lock);
256
257 int ocfs2_dlm_unlock(struct ocfs2_cluster_connection *conn,
258                      struct ocfs2_dlm_lksb *lksb,
259                      u32 flags)
260 {
261         BUG_ON(lksb->lksb_conn == NULL);
262
263         return active_stack->sp_ops->dlm_unlock(conn, lksb, flags);
264 }
265 EXPORT_SYMBOL_GPL(ocfs2_dlm_unlock);
266
267 int ocfs2_dlm_lock_status(struct ocfs2_dlm_lksb *lksb)
268 {
269         return active_stack->sp_ops->lock_status(lksb);
270 }
271 EXPORT_SYMBOL_GPL(ocfs2_dlm_lock_status);
272
273 int ocfs2_dlm_lvb_valid(struct ocfs2_dlm_lksb *lksb)
274 {
275         return active_stack->sp_ops->lvb_valid(lksb);
276 }
277 EXPORT_SYMBOL_GPL(ocfs2_dlm_lvb_valid);
278
279 void *ocfs2_dlm_lvb(struct ocfs2_dlm_lksb *lksb)
280 {
281         return active_stack->sp_ops->lock_lvb(lksb);
282 }
283 EXPORT_SYMBOL_GPL(ocfs2_dlm_lvb);
284
285 void ocfs2_dlm_dump_lksb(struct ocfs2_dlm_lksb *lksb)
286 {
287         active_stack->sp_ops->dump_lksb(lksb);
288 }
289 EXPORT_SYMBOL_GPL(ocfs2_dlm_dump_lksb);
290
291 int ocfs2_stack_supports_plocks(void)
292 {
293         return active_stack && active_stack->sp_ops->plock;
294 }
295 EXPORT_SYMBOL_GPL(ocfs2_stack_supports_plocks);
296
297 /*
298  * ocfs2_plock() can only be safely called if
299  * ocfs2_stack_supports_plocks() returned true
300  */
301 int ocfs2_plock(struct ocfs2_cluster_connection *conn, u64 ino,
302                 struct file *file, int cmd, struct file_lock *fl)
303 {
304         WARN_ON_ONCE(active_stack->sp_ops->plock == NULL);
305         if (active_stack->sp_ops->plock)
306                 return active_stack->sp_ops->plock(conn, ino, file, cmd, fl);
307         return -EOPNOTSUPP;
308 }
309 EXPORT_SYMBOL_GPL(ocfs2_plock);
310
311 int ocfs2_cluster_connect(const char *stack_name,
312                           const char *group,
313                           int grouplen,
314                           struct ocfs2_locking_protocol *lproto,
315                           void (*recovery_handler)(int node_num,
316                                                    void *recovery_data),
317                           void *recovery_data,
318                           struct ocfs2_cluster_connection **conn)
319 {
320         int rc = 0;
321         struct ocfs2_cluster_connection *new_conn;
322
323         BUG_ON(group == NULL);
324         BUG_ON(conn == NULL);
325         BUG_ON(recovery_handler == NULL);
326
327         if (grouplen > GROUP_NAME_MAX) {
328                 rc = -EINVAL;
329                 goto out;
330         }
331
332         if (memcmp(&lproto->lp_max_version, &locking_max_version,
333                    sizeof(struct ocfs2_protocol_version))) {
334                 rc = -EINVAL;
335                 goto out;
336         }
337
338         new_conn = kzalloc(sizeof(struct ocfs2_cluster_connection),
339                            GFP_KERNEL);
340         if (!new_conn) {
341                 rc = -ENOMEM;
342                 goto out;
343         }
344
345         memcpy(new_conn->cc_name, group, grouplen);
346         new_conn->cc_namelen = grouplen;
347         new_conn->cc_recovery_handler = recovery_handler;
348         new_conn->cc_recovery_data = recovery_data;
349
350         new_conn->cc_proto = lproto;
351         /* Start the new connection at our maximum compatibility level */
352         new_conn->cc_version = lproto->lp_max_version;
353
354         /* This will pin the stack driver if successful */
355         rc = ocfs2_stack_driver_get(stack_name);
356         if (rc)
357                 goto out_free;
358
359         rc = active_stack->sp_ops->connect(new_conn);
360         if (rc) {
361                 ocfs2_stack_driver_put();
362                 goto out_free;
363         }
364
365         *conn = new_conn;
366
367 out_free:
368         if (rc)
369                 kfree(new_conn);
370
371 out:
372         return rc;
373 }
374 EXPORT_SYMBOL_GPL(ocfs2_cluster_connect);
375
376 /* If hangup_pending is 0, the stack driver will be dropped */
377 int ocfs2_cluster_disconnect(struct ocfs2_cluster_connection *conn,
378                              int hangup_pending)
379 {
380         int ret;
381
382         BUG_ON(conn == NULL);
383
384         ret = active_stack->sp_ops->disconnect(conn);
385
386         /* XXX Should we free it anyway? */
387         if (!ret) {
388                 kfree(conn);
389                 if (!hangup_pending)
390                         ocfs2_stack_driver_put();
391         }
392
393         return ret;
394 }
395 EXPORT_SYMBOL_GPL(ocfs2_cluster_disconnect);
396
397 /*
398  * Leave the group for this filesystem.  This is executed by a userspace
399  * program (stored in ocfs2_hb_ctl_path).
400  */
401 static void ocfs2_leave_group(const char *group)
402 {
403         int ret;
404         char *argv[5], *envp[3];
405
406         argv[0] = ocfs2_hb_ctl_path;
407         argv[1] = "-K";
408         argv[2] = "-u";
409         argv[3] = (char *)group;
410         argv[4] = NULL;
411
412         /* minimal command environment taken from cpu_run_sbin_hotplug */
413         envp[0] = "HOME=/";
414         envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
415         envp[2] = NULL;
416
417         ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
418         if (ret < 0) {
419                 printk(KERN_ERR
420                        "ocfs2: Error %d running user helper "
421                        "\"%s %s %s %s\"\n",
422                        ret, argv[0], argv[1], argv[2], argv[3]);
423         }
424 }
425
426 /*
427  * Hangup is a required post-umount.  ocfs2-tools software expects the
428  * filesystem to call "ocfs2_hb_ctl" during unmount.  This happens
429  * regardless of whether the DLM got started, so we can't do it
430  * in ocfs2_cluster_disconnect().  The ocfs2_leave_group() function does
431  * the actual work.
432  */
433 void ocfs2_cluster_hangup(const char *group, int grouplen)
434 {
435         BUG_ON(group == NULL);
436         BUG_ON(group[grouplen] != '\0');
437
438         ocfs2_leave_group(group);
439
440         /* cluster_disconnect() was called with hangup_pending==1 */
441         ocfs2_stack_driver_put();
442 }
443 EXPORT_SYMBOL_GPL(ocfs2_cluster_hangup);
444
445 int ocfs2_cluster_this_node(unsigned int *node)
446 {
447         return active_stack->sp_ops->this_node(node);
448 }
449 EXPORT_SYMBOL_GPL(ocfs2_cluster_this_node);
450
451
452 /*
453  * Sysfs bits
454  */
455
456 static ssize_t ocfs2_max_locking_protocol_show(struct kobject *kobj,
457                                                struct kobj_attribute *attr,
458                                                char *buf)
459 {
460         ssize_t ret = 0;
461
462         spin_lock(&ocfs2_stack_lock);
463         if (locking_max_version.pv_major)
464                 ret = snprintf(buf, PAGE_SIZE, "%u.%u\n",
465                                locking_max_version.pv_major,
466                                locking_max_version.pv_minor);
467         spin_unlock(&ocfs2_stack_lock);
468
469         return ret;
470 }
471
472 static struct kobj_attribute ocfs2_attr_max_locking_protocol =
473         __ATTR(max_locking_protocol, S_IFREG | S_IRUGO,
474                ocfs2_max_locking_protocol_show, NULL);
475
476 static ssize_t ocfs2_loaded_cluster_plugins_show(struct kobject *kobj,
477                                                  struct kobj_attribute *attr,
478                                                  char *buf)
479 {
480         ssize_t ret = 0, total = 0, remain = PAGE_SIZE;
481         struct ocfs2_stack_plugin *p;
482
483         spin_lock(&ocfs2_stack_lock);
484         list_for_each_entry(p, &ocfs2_stack_list, sp_list) {
485                 ret = snprintf(buf, remain, "%s\n",
486                                p->sp_name);
487                 if (ret < 0) {
488                         total = ret;
489                         break;
490                 }
491                 if (ret == remain) {
492                         /* snprintf() didn't fit */
493                         total = -E2BIG;
494                         break;
495                 }
496                 total += ret;
497                 remain -= ret;
498         }
499         spin_unlock(&ocfs2_stack_lock);
500
501         return total;
502 }
503
504 static struct kobj_attribute ocfs2_attr_loaded_cluster_plugins =
505         __ATTR(loaded_cluster_plugins, S_IFREG | S_IRUGO,
506                ocfs2_loaded_cluster_plugins_show, NULL);
507
508 static ssize_t ocfs2_active_cluster_plugin_show(struct kobject *kobj,
509                                                 struct kobj_attribute *attr,
510                                                 char *buf)
511 {
512         ssize_t ret = 0;
513
514         spin_lock(&ocfs2_stack_lock);
515         if (active_stack) {
516                 ret = snprintf(buf, PAGE_SIZE, "%s\n",
517                                active_stack->sp_name);
518                 if (ret == PAGE_SIZE)
519                         ret = -E2BIG;
520         }
521         spin_unlock(&ocfs2_stack_lock);
522
523         return ret;
524 }
525
526 static struct kobj_attribute ocfs2_attr_active_cluster_plugin =
527         __ATTR(active_cluster_plugin, S_IFREG | S_IRUGO,
528                ocfs2_active_cluster_plugin_show, NULL);
529
530 static ssize_t ocfs2_cluster_stack_show(struct kobject *kobj,
531                                         struct kobj_attribute *attr,
532                                         char *buf)
533 {
534         ssize_t ret;
535         spin_lock(&ocfs2_stack_lock);
536         ret = snprintf(buf, PAGE_SIZE, "%s\n", cluster_stack_name);
537         spin_unlock(&ocfs2_stack_lock);
538
539         return ret;
540 }
541
542 static ssize_t ocfs2_cluster_stack_store(struct kobject *kobj,
543                                          struct kobj_attribute *attr,
544                                          const char *buf, size_t count)
545 {
546         size_t len = count;
547         ssize_t ret;
548
549         if (len == 0)
550                 return len;
551
552         if (buf[len - 1] == '\n')
553                 len--;
554
555         if ((len != OCFS2_STACK_LABEL_LEN) ||
556             (strnlen(buf, len) != len))
557                 return -EINVAL;
558
559         spin_lock(&ocfs2_stack_lock);
560         if (active_stack) {
561                 if (!strncmp(buf, cluster_stack_name, len))
562                         ret = count;
563                 else
564                         ret = -EBUSY;
565         } else {
566                 memcpy(cluster_stack_name, buf, len);
567                 ret = count;
568         }
569         spin_unlock(&ocfs2_stack_lock);
570
571         return ret;
572 }
573
574
575 static struct kobj_attribute ocfs2_attr_cluster_stack =
576         __ATTR(cluster_stack, S_IFREG | S_IRUGO | S_IWUSR,
577                ocfs2_cluster_stack_show,
578                ocfs2_cluster_stack_store);
579
580 static struct attribute *ocfs2_attrs[] = {
581         &ocfs2_attr_max_locking_protocol.attr,
582         &ocfs2_attr_loaded_cluster_plugins.attr,
583         &ocfs2_attr_active_cluster_plugin.attr,
584         &ocfs2_attr_cluster_stack.attr,
585         NULL,
586 };
587
588 static struct attribute_group ocfs2_attr_group = {
589         .attrs = ocfs2_attrs,
590 };
591
592 static struct kset *ocfs2_kset;
593
594 static void ocfs2_sysfs_exit(void)
595 {
596         kset_unregister(ocfs2_kset);
597 }
598
599 static int ocfs2_sysfs_init(void)
600 {
601         int ret;
602
603         ocfs2_kset = kset_create_and_add("ocfs2", NULL, fs_kobj);
604         if (!ocfs2_kset)
605                 return -ENOMEM;
606
607         ret = sysfs_create_group(&ocfs2_kset->kobj, &ocfs2_attr_group);
608         if (ret)
609                 goto error;
610
611         return 0;
612
613 error:
614         kset_unregister(ocfs2_kset);
615         return ret;
616 }
617
618 /*
619  * Sysctl bits
620  *
621  * The sysctl lives at /proc/sys/fs/ocfs2/nm/hb_ctl_path.  The 'nm' doesn't
622  * make as much sense in a multiple cluster stack world, but it's safer
623  * and easier to preserve the name.
624  */
625
626 #define FS_OCFS2_NM             1
627
628 static ctl_table ocfs2_nm_table[] = {
629         {
630                 .procname       = "hb_ctl_path",
631                 .data           = ocfs2_hb_ctl_path,
632                 .maxlen         = OCFS2_MAX_HB_CTL_PATH,
633                 .mode           = 0644,
634                 .proc_handler   = proc_dostring,
635         },
636         { }
637 };
638
639 static ctl_table ocfs2_mod_table[] = {
640         {
641                 .procname       = "nm",
642                 .data           = NULL,
643                 .maxlen         = 0,
644                 .mode           = 0555,
645                 .child          = ocfs2_nm_table
646         },
647         { }
648 };
649
650 static ctl_table ocfs2_kern_table[] = {
651         {
652                 .procname       = "ocfs2",
653                 .data           = NULL,
654                 .maxlen         = 0,
655                 .mode           = 0555,
656                 .child          = ocfs2_mod_table
657         },
658         { }
659 };
660
661 static ctl_table ocfs2_root_table[] = {
662         {
663                 .procname       = "fs",
664                 .data           = NULL,
665                 .maxlen         = 0,
666                 .mode           = 0555,
667                 .child          = ocfs2_kern_table
668         },
669         { }
670 };
671
672 static struct ctl_table_header *ocfs2_table_header = NULL;
673
674
675 /*
676  * Initialization
677  */
678
679 static int __init ocfs2_stack_glue_init(void)
680 {
681         strcpy(cluster_stack_name, OCFS2_STACK_PLUGIN_O2CB);
682
683         ocfs2_table_header = register_sysctl_table(ocfs2_root_table);
684         if (!ocfs2_table_header) {
685                 printk(KERN_ERR
686                        "ocfs2 stack glue: unable to register sysctl\n");
687                 return -ENOMEM; /* or something. */
688         }
689
690         return ocfs2_sysfs_init();
691 }
692
693 static void __exit ocfs2_stack_glue_exit(void)
694 {
695         memset(&locking_max_version, 0,
696                sizeof(struct ocfs2_protocol_version));
697         locking_max_version.pv_major = 0;
698         locking_max_version.pv_minor = 0;
699         ocfs2_sysfs_exit();
700         if (ocfs2_table_header)
701                 unregister_sysctl_table(ocfs2_table_header);
702 }
703
704 MODULE_AUTHOR("Oracle");
705 MODULE_DESCRIPTION("ocfs2 cluter stack glue layer");
706 MODULE_LICENSE("GPL");
707 module_init(ocfs2_stack_glue_init);
708 module_exit(ocfs2_stack_glue_exit);