ocfs2: fix the end cluster offset of FIEMAP
[linux-3.10.git] / fs / ocfs2 / stackglue.c
index bd80541..39abf89 100644 (file)
@@ -6,7 +6,7 @@
  * Code which implements an OCFS2 specific interface to underlying
  * cluster stacks.
  *
- * Copyright (C) 2007 Oracle.  All rights reserved.
+ * Copyright (C) 2007, 2009 Oracle.  All rights reserved.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public
  * General Public License for more details.
  */
 
+#include <linux/list.h>
+#include <linux/spinlock.h>
+#include <linux/module.h>
 #include <linux/slab.h>
-#include <linux/crc32.h>
 #include <linux/kmod.h>
-
-/* Needed for AOP_TRUNCATED_PAGE in mlog_errno() */
 #include <linux/fs.h>
+#include <linux/kobject.h>
+#include <linux/sysfs.h>
+#include <linux/sysctl.h>
 
-#include "cluster/masklog.h"
-#include "cluster/nodemanager.h"
-#include "cluster/heartbeat.h"
+#include "ocfs2_fs.h"
 
 #include "stackglue.h"
 
-static struct ocfs2_locking_protocol *lproto;
+#define OCFS2_STACK_PLUGIN_O2CB                "o2cb"
+#define OCFS2_STACK_PLUGIN_USER                "user"
+#define OCFS2_MAX_HB_CTL_PATH          256
 
-struct o2dlm_private {
-       struct dlm_eviction_cb op_eviction_cb;
-};
+static struct ocfs2_protocol_version locking_max_version;
+static DEFINE_SPINLOCK(ocfs2_stack_lock);
+static LIST_HEAD(ocfs2_stack_list);
+static char cluster_stack_name[OCFS2_STACK_LABEL_LEN + 1];
+static char ocfs2_hb_ctl_path[OCFS2_MAX_HB_CTL_PATH] = "/sbin/ocfs2_hb_ctl";
+
+/*
+ * The stack currently in use.  If not null, active_stack->sp_count > 0,
+ * the module is pinned, and the locking protocol cannot be changed.
+ */
+static struct ocfs2_stack_plugin *active_stack;
 
-/* These should be identical */
-#if (DLM_LOCK_IV != LKM_IVMODE)
-# error Lock modes do not match
-#endif
-#if (DLM_LOCK_NL != LKM_NLMODE)
-# error Lock modes do not match
-#endif
-#if (DLM_LOCK_CR != LKM_CRMODE)
-# error Lock modes do not match
-#endif
-#if (DLM_LOCK_CW != LKM_CWMODE)
-# error Lock modes do not match
-#endif
-#if (DLM_LOCK_PR != LKM_PRMODE)
-# error Lock modes do not match
-#endif
-#if (DLM_LOCK_PW != LKM_PWMODE)
-# error Lock modes do not match
-#endif
-#if (DLM_LOCK_EX != LKM_EXMODE)
-# error Lock modes do not match
-#endif
-static inline int mode_to_o2dlm(int mode)
+static struct ocfs2_stack_plugin *ocfs2_stack_lookup(const char *name)
 {
-       BUG_ON(mode > LKM_MAXMODE);
+       struct ocfs2_stack_plugin *p;
 
-       return mode;
-}
+       assert_spin_locked(&ocfs2_stack_lock);
 
-#define map_flag(_generic, _o2dlm)             \
-       if (flags & (_generic)) {               \
-               flags &= ~(_generic);           \
-               o2dlm_flags |= (_o2dlm);        \
+       list_for_each_entry(p, &ocfs2_stack_list, sp_list) {
+               if (!strcmp(p->sp_name, name))
+                       return p;
        }
-static int flags_to_o2dlm(u32 flags)
+
+       return NULL;
+}
+
+static int ocfs2_stack_driver_request(const char *stack_name,
+                                     const char *plugin_name)
 {
-       int o2dlm_flags = 0;
-
-       map_flag(DLM_LKF_NOQUEUE, LKM_NOQUEUE);
-       map_flag(DLM_LKF_CANCEL, LKM_CANCEL);
-       map_flag(DLM_LKF_CONVERT, LKM_CONVERT);
-       map_flag(DLM_LKF_VALBLK, LKM_VALBLK);
-       map_flag(DLM_LKF_IVVALBLK, LKM_INVVALBLK);
-       map_flag(DLM_LKF_ORPHAN, LKM_ORPHAN);
-       map_flag(DLM_LKF_FORCEUNLOCK, LKM_FORCE);
-       map_flag(DLM_LKF_TIMEOUT, LKM_TIMEOUT);
-       map_flag(DLM_LKF_LOCAL, LKM_LOCAL);
-
-       /* map_flag() should have cleared every flag passed in */
-       BUG_ON(flags != 0);
-
-       return o2dlm_flags;
+       int rc;
+       struct ocfs2_stack_plugin *p;
+
+       spin_lock(&ocfs2_stack_lock);
+
+       /*
+        * If the stack passed by the filesystem isn't the selected one,
+        * we can't continue.
+        */
+       if (strcmp(stack_name, cluster_stack_name)) {
+               rc = -EBUSY;
+               goto out;
+       }
+
+       if (active_stack) {
+               /*
+                * If the active stack isn't the one we want, it cannot
+                * be selected right now.
+                */
+               if (!strcmp(active_stack->sp_name, plugin_name))
+                       rc = 0;
+               else
+                       rc = -EBUSY;
+               goto out;
+       }
+
+       p = ocfs2_stack_lookup(plugin_name);
+       if (!p || !try_module_get(p->sp_owner)) {
+               rc = -ENOENT;
+               goto out;
+       }
+
+       active_stack = p;
+       rc = 0;
+
+out:
+       /* If we found it, pin it */
+       if (!rc)
+               active_stack->sp_count++;
+
+       spin_unlock(&ocfs2_stack_lock);
+       return rc;
 }
-#undef map_flag
 
 /*
- * Map an o2dlm status to standard errno values.
- *
- * o2dlm only uses a handful of these, and returns even fewer to the
- * caller. Still, we try to assign sane values to each error.
- *
- * The following value pairs have special meanings to dlmglue, thus
- * the right hand side needs to stay unique - never duplicate the
- * mapping elsewhere in the table!
- *
- * DLM_NORMAL:         0
- * DLM_NOTQUEUED:      -EAGAIN
- * DLM_CANCELGRANT:    -EBUSY
- * DLM_CANCEL:         -DLM_ECANCEL
+ * This function looks up the appropriate stack and makes it active.  If
+ * there is no stack, it tries to load it.  It will fail if the stack still
+ * cannot be found.  It will also fail if a different stack is in use.
  */
-/* Keep in sync with dlmapi.h */
-static int status_map[] = {
-       [DLM_NORMAL]                    = 0,            /* Success */
-       [DLM_GRANTED]                   = -EINVAL,
-       [DLM_DENIED]                    = -EACCES,
-       [DLM_DENIED_NOLOCKS]            = -EACCES,
-       [DLM_WORKING]                   = -EACCES,
-       [DLM_BLOCKED]                   = -EINVAL,
-       [DLM_BLOCKED_ORPHAN]            = -EINVAL,
-       [DLM_DENIED_GRACE_PERIOD]       = -EACCES,
-       [DLM_SYSERR]                    = -ENOMEM,      /* It is what it is */
-       [DLM_NOSUPPORT]                 = -EPROTO,
-       [DLM_CANCELGRANT]               = -EBUSY,       /* Cancel after grant */
-       [DLM_IVLOCKID]                  = -EINVAL,
-       [DLM_SYNC]                      = -EINVAL,
-       [DLM_BADTYPE]                   = -EINVAL,
-       [DLM_BADRESOURCE]               = -EINVAL,
-       [DLM_MAXHANDLES]                = -ENOMEM,
-       [DLM_NOCLINFO]                  = -EINVAL,
-       [DLM_NOLOCKMGR]                 = -EINVAL,
-       [DLM_NOPURGED]                  = -EINVAL,
-       [DLM_BADARGS]                   = -EINVAL,
-       [DLM_VOID]                      = -EINVAL,
-       [DLM_NOTQUEUED]                 = -EAGAIN,      /* Trylock failed */
-       [DLM_IVBUFLEN]                  = -EINVAL,
-       [DLM_CVTUNGRANT]                = -EPERM,
-       [DLM_BADPARAM]                  = -EINVAL,
-       [DLM_VALNOTVALID]               = -EINVAL,
-       [DLM_REJECTED]                  = -EPERM,
-       [DLM_ABORT]                     = -EINVAL,
-       [DLM_CANCEL]                    = -DLM_ECANCEL, /* Successful cancel */
-       [DLM_IVRESHANDLE]               = -EINVAL,
-       [DLM_DEADLOCK]                  = -EDEADLK,
-       [DLM_DENIED_NOASTS]             = -EINVAL,
-       [DLM_FORWARD]                   = -EINVAL,
-       [DLM_TIMEOUT]                   = -ETIMEDOUT,
-       [DLM_IVGROUPID]                 = -EINVAL,
-       [DLM_VERS_CONFLICT]             = -EOPNOTSUPP,
-       [DLM_BAD_DEVICE_PATH]           = -ENOENT,
-       [DLM_NO_DEVICE_PERMISSION]      = -EPERM,
-       [DLM_NO_CONTROL_DEVICE]         = -ENOENT,
-       [DLM_RECOVERING]                = -ENOTCONN,
-       [DLM_MIGRATING]                 = -ERESTART,
-       [DLM_MAXSTATS]                  = -EINVAL,
-};
-
-static int dlm_status_to_errno(enum dlm_status status)
+static int ocfs2_stack_driver_get(const char *stack_name)
 {
-       BUG_ON(status > (sizeof(status_map) / sizeof(status_map[0])));
+       int rc;
+       char *plugin_name = OCFS2_STACK_PLUGIN_O2CB;
 
-       return status_map[status];
+       /*
+        * Classic stack does not pass in a stack name.  This is
+        * compatible with older tools as well.
+        */
+       if (!stack_name || !*stack_name)
+               stack_name = OCFS2_STACK_PLUGIN_O2CB;
+
+       if (strlen(stack_name) != OCFS2_STACK_LABEL_LEN) {
+               printk(KERN_ERR
+                      "ocfs2 passed an invalid cluster stack label: \"%s\"\n",
+                      stack_name);
+               return -EINVAL;
+       }
+
+       /* Anything that isn't the classic stack is a user stack */
+       if (strcmp(stack_name, OCFS2_STACK_PLUGIN_O2CB))
+               plugin_name = OCFS2_STACK_PLUGIN_USER;
+
+       rc = ocfs2_stack_driver_request(stack_name, plugin_name);
+       if (rc == -ENOENT) {
+               request_module("ocfs2_stack_%s", plugin_name);
+               rc = ocfs2_stack_driver_request(stack_name, plugin_name);
+       }
+
+       if (rc == -ENOENT) {
+               printk(KERN_ERR
+                      "ocfs2: Cluster stack driver \"%s\" cannot be found\n",
+                      plugin_name);
+       } else if (rc == -EBUSY) {
+               printk(KERN_ERR
+                      "ocfs2: A different cluster stack is in use\n");
+       }
+
+       return rc;
 }
 
-static void o2dlm_lock_ast_wrapper(void *astarg)
+static void ocfs2_stack_driver_put(void)
 {
-       BUG_ON(lproto == NULL);
-
-       lproto->lp_lock_ast(astarg);
+       spin_lock(&ocfs2_stack_lock);
+       BUG_ON(active_stack == NULL);
+       BUG_ON(active_stack->sp_count == 0);
+
+       active_stack->sp_count--;
+       if (!active_stack->sp_count) {
+               module_put(active_stack->sp_owner);
+               active_stack = NULL;
+       }
+       spin_unlock(&ocfs2_stack_lock);
 }
 
-static void o2dlm_blocking_ast_wrapper(void *astarg, int level)
+int ocfs2_stack_glue_register(struct ocfs2_stack_plugin *plugin)
 {
-       BUG_ON(lproto == NULL);
+       int rc;
+
+       spin_lock(&ocfs2_stack_lock);
+       if (!ocfs2_stack_lookup(plugin->sp_name)) {
+               plugin->sp_count = 0;
+               plugin->sp_max_proto = locking_max_version;
+               list_add(&plugin->sp_list, &ocfs2_stack_list);
+               printk(KERN_INFO "ocfs2: Registered cluster interface %s\n",
+                      plugin->sp_name);
+               rc = 0;
+       } else {
+               printk(KERN_ERR "ocfs2: Stack \"%s\" already registered\n",
+                      plugin->sp_name);
+               rc = -EEXIST;
+       }
+       spin_unlock(&ocfs2_stack_lock);
 
-       lproto->lp_blocking_ast(astarg, level);
+       return rc;
 }
+EXPORT_SYMBOL_GPL(ocfs2_stack_glue_register);
 
-static void o2dlm_unlock_ast_wrapper(void *astarg, enum dlm_status status)
+void ocfs2_stack_glue_unregister(struct ocfs2_stack_plugin *plugin)
 {
-       int error = dlm_status_to_errno(status);
+       struct ocfs2_stack_plugin *p;
+
+       spin_lock(&ocfs2_stack_lock);
+       p = ocfs2_stack_lookup(plugin->sp_name);
+       if (p) {
+               BUG_ON(p != plugin);
+               BUG_ON(plugin == active_stack);
+               BUG_ON(plugin->sp_count != 0);
+               list_del_init(&plugin->sp_list);
+               printk(KERN_INFO "ocfs2: Unregistered cluster interface %s\n",
+                      plugin->sp_name);
+       } else {
+               printk(KERN_ERR "Stack \"%s\" is not registered\n",
+                      plugin->sp_name);
+       }
+       spin_unlock(&ocfs2_stack_lock);
+}
+EXPORT_SYMBOL_GPL(ocfs2_stack_glue_unregister);
 
-       BUG_ON(lproto == NULL);
+void ocfs2_stack_glue_set_max_proto_version(struct ocfs2_protocol_version *max_proto)
+{
+       struct ocfs2_stack_plugin *p;
 
-       /*
-        * In o2dlm, you can get both the lock_ast() for the lock being
-        * granted and the unlock_ast() for the CANCEL failing.  A
-        * successful cancel sends DLM_NORMAL here.  If the
-        * lock grant happened before the cancel arrived, you get
-        * DLM_CANCELGRANT.
-        *
-        * There's no need for the double-ast.  If we see DLM_CANCELGRANT,
-        * we just ignore it.  We expect the lock_ast() to handle the
-        * granted lock.
-        */
-       if (status == DLM_CANCELGRANT)
-               return;
+       spin_lock(&ocfs2_stack_lock);
+       if (memcmp(max_proto, &locking_max_version,
+                  sizeof(struct ocfs2_protocol_version))) {
+               BUG_ON(locking_max_version.pv_major != 0);
 
-       lproto->lp_unlock_ast(astarg, error);
+               locking_max_version = *max_proto;
+               list_for_each_entry(p, &ocfs2_stack_list, sp_list) {
+                       p->sp_max_proto = locking_max_version;
+               }
+       }
+       spin_unlock(&ocfs2_stack_lock);
 }
+EXPORT_SYMBOL_GPL(ocfs2_stack_glue_set_max_proto_version);
+
 
+/*
+ * The ocfs2_dlm_lock() and ocfs2_dlm_unlock() functions take no argument
+ * for the ast and bast functions.  They will pass the lksb to the ast
+ * and bast.  The caller can wrap the lksb with their own structure to
+ * get more information.
+ */
 int ocfs2_dlm_lock(struct ocfs2_cluster_connection *conn,
                   int mode,
-                  union ocfs2_dlm_lksb *lksb,
+                  struct ocfs2_dlm_lksb *lksb,
                   u32 flags,
                   void *name,
-                  unsigned int namelen,
-                  void *astarg)
+                  unsigned int namelen)
 {
-       enum dlm_status status;
-       int o2dlm_mode = mode_to_o2dlm(mode);
-       int o2dlm_flags = flags_to_o2dlm(flags);
-       int ret;
-
-       BUG_ON(lproto == NULL);
-
-       status = dlmlock(conn->cc_lockspace, o2dlm_mode, &lksb->lksb_o2dlm,
-                        o2dlm_flags, name, namelen,
-                        o2dlm_lock_ast_wrapper, astarg,
-                        o2dlm_blocking_ast_wrapper);
-       ret = dlm_status_to_errno(status);
-       return ret;
+       if (!lksb->lksb_conn)
+               lksb->lksb_conn = conn;
+       else
+               BUG_ON(lksb->lksb_conn != conn);
+       return active_stack->sp_ops->dlm_lock(conn, mode, lksb, flags,
+                                             name, namelen);
 }
+EXPORT_SYMBOL_GPL(ocfs2_dlm_lock);
 
 int ocfs2_dlm_unlock(struct ocfs2_cluster_connection *conn,
-                    union ocfs2_dlm_lksb *lksb,
-                    u32 flags,
-                    void *astarg)
+                    struct ocfs2_dlm_lksb *lksb,
+                    u32 flags)
 {
-       enum dlm_status status;
-       int o2dlm_flags = flags_to_o2dlm(flags);
-       int ret;
+       BUG_ON(lksb->lksb_conn == NULL);
 
-       BUG_ON(lproto == NULL);
+       return active_stack->sp_ops->dlm_unlock(conn, lksb, flags);
+}
+EXPORT_SYMBOL_GPL(ocfs2_dlm_unlock);
 
-       status = dlmunlock(conn->cc_lockspace, &lksb->lksb_o2dlm,
-                          o2dlm_flags, o2dlm_unlock_ast_wrapper, astarg);
-       ret = dlm_status_to_errno(status);
-       return ret;
+int ocfs2_dlm_lock_status(struct ocfs2_dlm_lksb *lksb)
+{
+       return active_stack->sp_ops->lock_status(lksb);
 }
+EXPORT_SYMBOL_GPL(ocfs2_dlm_lock_status);
 
-int ocfs2_dlm_lock_status(union ocfs2_dlm_lksb *lksb)
+int ocfs2_dlm_lvb_valid(struct ocfs2_dlm_lksb *lksb)
 {
-       return dlm_status_to_errno(lksb->lksb_o2dlm.status);
+       return active_stack->sp_ops->lvb_valid(lksb);
 }
+EXPORT_SYMBOL_GPL(ocfs2_dlm_lvb_valid);
 
-/*
- * Why don't we cast to ocfs2_meta_lvb?  The "clean" answer is that we
- * don't cast at the glue level.  The real answer is that the header
- * ordering is nigh impossible.
- */
-void *ocfs2_dlm_lvb(union ocfs2_dlm_lksb *lksb)
+void *ocfs2_dlm_lvb(struct ocfs2_dlm_lksb *lksb)
+{
+       return active_stack->sp_ops->lock_lvb(lksb);
+}
+EXPORT_SYMBOL_GPL(ocfs2_dlm_lvb);
+
+void ocfs2_dlm_dump_lksb(struct ocfs2_dlm_lksb *lksb)
 {
-       return (void *)(lksb->lksb_o2dlm.lvb);
+       active_stack->sp_ops->dump_lksb(lksb);
 }
+EXPORT_SYMBOL_GPL(ocfs2_dlm_dump_lksb);
 
-void ocfs2_dlm_dump_lksb(union ocfs2_dlm_lksb *lksb)
+int ocfs2_stack_supports_plocks(void)
 {
-       dlm_print_one_lock(lksb->lksb_o2dlm.lockid);
+       return active_stack && active_stack->sp_ops->plock;
 }
+EXPORT_SYMBOL_GPL(ocfs2_stack_supports_plocks);
 
 /*
- * Called from the dlm when it's about to evict a node. This is how the
- * classic stack signals node death.
+ * ocfs2_plock() can only be safely called if
+ * ocfs2_stack_supports_plocks() returned true
  */
-static void o2dlm_eviction_cb(int node_num, void *data)
+int ocfs2_plock(struct ocfs2_cluster_connection *conn, u64 ino,
+               struct file *file, int cmd, struct file_lock *fl)
 {
-       struct ocfs2_cluster_connection *conn = data;
-
-       mlog(ML_NOTICE, "o2dlm has evicted node %d from group %.*s\n",
-            node_num, conn->cc_namelen, conn->cc_name);
-
-       conn->cc_recovery_handler(node_num, conn->cc_recovery_data);
+       WARN_ON_ONCE(active_stack->sp_ops->plock == NULL);
+       if (active_stack->sp_ops->plock)
+               return active_stack->sp_ops->plock(conn, ino, file, cmd, fl);
+       return -EOPNOTSUPP;
 }
+EXPORT_SYMBOL_GPL(ocfs2_plock);
 
-int ocfs2_cluster_connect(const char *group,
+int ocfs2_cluster_connect(const char *stack_name,
+                         const char *group,
                          int grouplen,
+                         struct ocfs2_locking_protocol *lproto,
                          void (*recovery_handler)(int node_num,
                                                   void *recovery_data),
                          void *recovery_data,
@@ -280,10 +319,6 @@ int ocfs2_cluster_connect(const char *group,
 {
        int rc = 0;
        struct ocfs2_cluster_connection *new_conn;
-       u32 dlm_key;
-       struct dlm_ctxt *dlm;
-       struct o2dlm_private *priv;
-       struct dlm_protocol_version dlm_version;
 
        BUG_ON(group == NULL);
        BUG_ON(conn == NULL);
@@ -294,9 +329,8 @@ int ocfs2_cluster_connect(const char *group,
                goto out;
        }
 
-       /* for now we only have one cluster/node, make sure we see it
-        * in the heartbeat universe */
-       if (!o2hb_check_local_node_heartbeating()) {
+       if (memcmp(&lproto->lp_max_version, &locking_max_version,
+                  sizeof(struct ocfs2_protocol_version))) {
                rc = -EINVAL;
                goto out;
        }
@@ -313,130 +347,380 @@ int ocfs2_cluster_connect(const char *group,
        new_conn->cc_recovery_handler = recovery_handler;
        new_conn->cc_recovery_data = recovery_data;
 
+       new_conn->cc_proto = lproto;
        /* Start the new connection at our maximum compatibility level */
        new_conn->cc_version = lproto->lp_max_version;
 
-       priv = kzalloc(sizeof(struct o2dlm_private), GFP_KERNEL);
-       if (!priv) {
-               rc = -ENOMEM;
+       /* This will pin the stack driver if successful */
+       rc = ocfs2_stack_driver_get(stack_name);
+       if (rc)
                goto out_free;
-       }
-
-       /* This just fills the structure in.  It is safe to use new_conn. */
-       dlm_setup_eviction_cb(&priv->op_eviction_cb, o2dlm_eviction_cb,
-                             new_conn);
 
-       new_conn->cc_private = priv;
-
-       /* used by the dlm code to make message headers unique, each
-        * node in this domain must agree on this. */
-       dlm_key = crc32_le(0, group, grouplen);
-       dlm_version.pv_major = new_conn->cc_version.pv_major;
-       dlm_version.pv_minor = new_conn->cc_version.pv_minor;
-
-       dlm = dlm_register_domain(group, dlm_key, &dlm_version);
-       if (IS_ERR(dlm)) {
-               rc = PTR_ERR(dlm);
-               mlog_errno(rc);
+       rc = active_stack->sp_ops->connect(new_conn);
+       if (rc) {
+               ocfs2_stack_driver_put();
                goto out_free;
        }
 
-       new_conn->cc_version.pv_major = dlm_version.pv_major;
-       new_conn->cc_version.pv_minor = dlm_version.pv_minor;
-       new_conn->cc_lockspace = dlm;
-
-       dlm_register_eviction_cb(dlm, &priv->op_eviction_cb);
-
        *conn = new_conn;
 
 out_free:
-       if (rc) {
-               kfree(new_conn->cc_private);
+       if (rc)
                kfree(new_conn);
-       }
 
 out:
        return rc;
 }
+EXPORT_SYMBOL_GPL(ocfs2_cluster_connect);
+
+/* The caller will ensure all nodes have the same cluster stack */
+int ocfs2_cluster_connect_agnostic(const char *group,
+                                  int grouplen,
+                                  struct ocfs2_locking_protocol *lproto,
+                                  void (*recovery_handler)(int node_num,
+                                                           void *recovery_data),
+                                  void *recovery_data,
+                                  struct ocfs2_cluster_connection **conn)
+{
+       char *stack_name = NULL;
 
+       if (cluster_stack_name[0])
+               stack_name = cluster_stack_name;
+       return ocfs2_cluster_connect(stack_name, group, grouplen, lproto,
+                                    recovery_handler, recovery_data, conn);
+}
+EXPORT_SYMBOL_GPL(ocfs2_cluster_connect_agnostic);
 
-int ocfs2_cluster_disconnect(struct ocfs2_cluster_connection *conn)
+/* If hangup_pending is 0, the stack driver will be dropped */
+int ocfs2_cluster_disconnect(struct ocfs2_cluster_connection *conn,
+                            int hangup_pending)
 {
-       struct dlm_ctxt *dlm = conn->cc_lockspace;
-       struct o2dlm_private *priv = conn->cc_private;
+       int ret;
 
-       dlm_unregister_eviction_cb(&priv->op_eviction_cb);
-       dlm_unregister_domain(dlm);
+       BUG_ON(conn == NULL);
 
-       kfree(priv);
-       kfree(conn);
+       ret = active_stack->sp_ops->disconnect(conn);
 
-       return 0;
+       /* XXX Should we free it anyway? */
+       if (!ret) {
+               kfree(conn);
+               if (!hangup_pending)
+                       ocfs2_stack_driver_put();
+       }
+
+       return ret;
 }
+EXPORT_SYMBOL_GPL(ocfs2_cluster_disconnect);
 
-static void o2hb_stop(const char *group)
+/*
+ * Leave the group for this filesystem.  This is executed by a userspace
+ * program (stored in ocfs2_hb_ctl_path).
+ */
+static void ocfs2_leave_group(const char *group)
 {
        int ret;
        char *argv[5], *envp[3];
 
-       argv[0] = (char *)o2nm_get_hb_ctl_path();
+       argv[0] = ocfs2_hb_ctl_path;
        argv[1] = "-K";
        argv[2] = "-u";
        argv[3] = (char *)group;
        argv[4] = NULL;
 
-       mlog(0, "Run: %s %s %s %s\n", argv[0], argv[1], argv[2], argv[3]);
-
        /* minimal command environment taken from cpu_run_sbin_hotplug */
        envp[0] = "HOME=/";
        envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
        envp[2] = NULL;
 
        ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
-       if (ret < 0)
-               mlog_errno(ret);
+       if (ret < 0) {
+               printk(KERN_ERR
+                      "ocfs2: Error %d running user helper "
+                      "\"%s %s %s %s\"\n",
+                      ret, argv[0], argv[1], argv[2], argv[3]);
+       }
 }
 
 /*
- * Hangup is a hack for tools compatibility.  Older ocfs2-tools software
- * expects the filesystem to call "ocfs2_hb_ctl" during unmount.  This
- * happens regardless of whether the DLM got started, so we can't do it
- * in ocfs2_cluster_disconnect().  We bring the o2hb_stop() function into
- * the glue and provide a "hangup" API for super.c to call.
- *
- * Other stacks will eventually provide a NULL ->hangup() pointer.
+ * Hangup is a required post-umount.  ocfs2-tools software expects the
+ * filesystem to call "ocfs2_hb_ctl" during unmount.  This happens
+ * regardless of whether the DLM got started, so we can't do it
+ * in ocfs2_cluster_disconnect().  The ocfs2_leave_group() function does
+ * the actual work.
  */
 void ocfs2_cluster_hangup(const char *group, int grouplen)
 {
        BUG_ON(group == NULL);
        BUG_ON(group[grouplen] != '\0');
 
-       o2hb_stop(group);
+       ocfs2_leave_group(group);
+
+       /* cluster_disconnect() was called with hangup_pending==1 */
+       ocfs2_stack_driver_put();
 }
+EXPORT_SYMBOL_GPL(ocfs2_cluster_hangup);
 
 int ocfs2_cluster_this_node(unsigned int *node)
 {
-       int node_num;
+       return active_stack->sp_ops->this_node(node);
+}
+EXPORT_SYMBOL_GPL(ocfs2_cluster_this_node);
+
+
+/*
+ * Sysfs bits
+ */
+
+static ssize_t ocfs2_max_locking_protocol_show(struct kobject *kobj,
+                                              struct kobj_attribute *attr,
+                                              char *buf)
+{
+       ssize_t ret = 0;
+
+       spin_lock(&ocfs2_stack_lock);
+       if (locking_max_version.pv_major)
+               ret = snprintf(buf, PAGE_SIZE, "%u.%u\n",
+                              locking_max_version.pv_major,
+                              locking_max_version.pv_minor);
+       spin_unlock(&ocfs2_stack_lock);
+
+       return ret;
+}
+
+static struct kobj_attribute ocfs2_attr_max_locking_protocol =
+       __ATTR(max_locking_protocol, S_IFREG | S_IRUGO,
+              ocfs2_max_locking_protocol_show, NULL);
+
+static ssize_t ocfs2_loaded_cluster_plugins_show(struct kobject *kobj,
+                                                struct kobj_attribute *attr,
+                                                char *buf)
+{
+       ssize_t ret = 0, total = 0, remain = PAGE_SIZE;
+       struct ocfs2_stack_plugin *p;
+
+       spin_lock(&ocfs2_stack_lock);
+       list_for_each_entry(p, &ocfs2_stack_list, sp_list) {
+               ret = snprintf(buf, remain, "%s\n",
+                              p->sp_name);
+               if (ret < 0) {
+                       total = ret;
+                       break;
+               }
+               if (ret == remain) {
+                       /* snprintf() didn't fit */
+                       total = -E2BIG;
+                       break;
+               }
+               total += ret;
+               remain -= ret;
+       }
+       spin_unlock(&ocfs2_stack_lock);
+
+       return total;
+}
+
+static struct kobj_attribute ocfs2_attr_loaded_cluster_plugins =
+       __ATTR(loaded_cluster_plugins, S_IFREG | S_IRUGO,
+              ocfs2_loaded_cluster_plugins_show, NULL);
+
+static ssize_t ocfs2_active_cluster_plugin_show(struct kobject *kobj,
+                                               struct kobj_attribute *attr,
+                                               char *buf)
+{
+       ssize_t ret = 0;
+
+       spin_lock(&ocfs2_stack_lock);
+       if (active_stack) {
+               ret = snprintf(buf, PAGE_SIZE, "%s\n",
+                              active_stack->sp_name);
+               if (ret == PAGE_SIZE)
+                       ret = -E2BIG;
+       }
+       spin_unlock(&ocfs2_stack_lock);
+
+       return ret;
+}
+
+static struct kobj_attribute ocfs2_attr_active_cluster_plugin =
+       __ATTR(active_cluster_plugin, S_IFREG | S_IRUGO,
+              ocfs2_active_cluster_plugin_show, NULL);
+
+static ssize_t ocfs2_cluster_stack_show(struct kobject *kobj,
+                                       struct kobj_attribute *attr,
+                                       char *buf)
+{
+       ssize_t ret;
+       spin_lock(&ocfs2_stack_lock);
+       ret = snprintf(buf, PAGE_SIZE, "%s\n", cluster_stack_name);
+       spin_unlock(&ocfs2_stack_lock);
+
+       return ret;
+}
+
+static ssize_t ocfs2_cluster_stack_store(struct kobject *kobj,
+                                        struct kobj_attribute *attr,
+                                        const char *buf, size_t count)
+{
+       size_t len = count;
+       ssize_t ret;
+
+       if (len == 0)
+               return len;
+
+       if (buf[len - 1] == '\n')
+               len--;
+
+       if ((len != OCFS2_STACK_LABEL_LEN) ||
+           (strnlen(buf, len) != len))
+               return -EINVAL;
+
+       spin_lock(&ocfs2_stack_lock);
+       if (active_stack) {
+               if (!strncmp(buf, cluster_stack_name, len))
+                       ret = count;
+               else
+                       ret = -EBUSY;
+       } else {
+               memcpy(cluster_stack_name, buf, len);
+               ret = count;
+       }
+       spin_unlock(&ocfs2_stack_lock);
+
+       return ret;
+}
 
-       node_num = o2nm_this_node();
-       if (node_num == O2NM_INVALID_NODE_NUM)
-               return -ENOENT;
 
-       if (node_num >= O2NM_MAX_NODES)
-               return -EOVERFLOW;
+static struct kobj_attribute ocfs2_attr_cluster_stack =
+       __ATTR(cluster_stack, S_IFREG | S_IRUGO | S_IWUSR,
+              ocfs2_cluster_stack_show,
+              ocfs2_cluster_stack_store);
+
+static struct attribute *ocfs2_attrs[] = {
+       &ocfs2_attr_max_locking_protocol.attr,
+       &ocfs2_attr_loaded_cluster_plugins.attr,
+       &ocfs2_attr_active_cluster_plugin.attr,
+       &ocfs2_attr_cluster_stack.attr,
+       NULL,
+};
+
+static struct attribute_group ocfs2_attr_group = {
+       .attrs = ocfs2_attrs,
+};
+
+static struct kset *ocfs2_kset;
+
+static void ocfs2_sysfs_exit(void)
+{
+       kset_unregister(ocfs2_kset);
+}
+
+static int ocfs2_sysfs_init(void)
+{
+       int ret;
+
+       ocfs2_kset = kset_create_and_add("ocfs2", NULL, fs_kobj);
+       if (!ocfs2_kset)
+               return -ENOMEM;
+
+       ret = sysfs_create_group(&ocfs2_kset->kobj, &ocfs2_attr_group);
+       if (ret)
+               goto error;
 
-       *node = node_num;
        return 0;
+
+error:
+       kset_unregister(ocfs2_kset);
+       return ret;
 }
 
-void o2cb_get_stack(struct ocfs2_locking_protocol *proto)
+/*
+ * Sysctl bits
+ *
+ * The sysctl lives at /proc/sys/fs/ocfs2/nm/hb_ctl_path.  The 'nm' doesn't
+ * make as much sense in a multiple cluster stack world, but it's safer
+ * and easier to preserve the name.
+ */
+
+#define FS_OCFS2_NM            1
+
+static ctl_table ocfs2_nm_table[] = {
+       {
+               .procname       = "hb_ctl_path",
+               .data           = ocfs2_hb_ctl_path,
+               .maxlen         = OCFS2_MAX_HB_CTL_PATH,
+               .mode           = 0644,
+               .proc_handler   = proc_dostring,
+       },
+       { }
+};
+
+static ctl_table ocfs2_mod_table[] = {
+       {
+               .procname       = "nm",
+               .data           = NULL,
+               .maxlen         = 0,
+               .mode           = 0555,
+               .child          = ocfs2_nm_table
+       },
+       { }
+};
+
+static ctl_table ocfs2_kern_table[] = {
+       {
+               .procname       = "ocfs2",
+               .data           = NULL,
+               .maxlen         = 0,
+               .mode           = 0555,
+               .child          = ocfs2_mod_table
+       },
+       { }
+};
+
+static ctl_table ocfs2_root_table[] = {
+       {
+               .procname       = "fs",
+               .data           = NULL,
+               .maxlen         = 0,
+               .mode           = 0555,
+               .child          = ocfs2_kern_table
+       },
+       { }
+};
+
+static struct ctl_table_header *ocfs2_table_header = NULL;
+
+
+/*
+ * Initialization
+ */
+
+static int __init ocfs2_stack_glue_init(void)
 {
-       BUG_ON(proto == NULL);
+       strcpy(cluster_stack_name, OCFS2_STACK_PLUGIN_O2CB);
 
-       lproto = proto;
+       ocfs2_table_header = register_sysctl_table(ocfs2_root_table);
+       if (!ocfs2_table_header) {
+               printk(KERN_ERR
+                      "ocfs2 stack glue: unable to register sysctl\n");
+               return -ENOMEM; /* or something. */
+       }
+
+       return ocfs2_sysfs_init();
 }
 
-void o2cb_put_stack(void)
+static void __exit ocfs2_stack_glue_exit(void)
 {
-       lproto = NULL;
+       memset(&locking_max_version, 0,
+              sizeof(struct ocfs2_protocol_version));
+       locking_max_version.pv_major = 0;
+       locking_max_version.pv_minor = 0;
+       ocfs2_sysfs_exit();
+       if (ocfs2_table_header)
+               unregister_sysctl_table(ocfs2_table_header);
 }
+
+MODULE_AUTHOR("Oracle");
+MODULE_DESCRIPTION("ocfs2 cluter stack glue layer");
+MODULE_LICENSE("GPL");
+module_init(ocfs2_stack_glue_init);
+module_exit(ocfs2_stack_glue_exit);