Joel Becker | 24ef181 | 2008-01-29 17:37:32 -0800 | [diff] [blame] | 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 | |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame^] | 21 | #include <linux/list.h> |
| 22 | #include <linux/spinlock.h> |
| 23 | #include <linux/module.h> |
Joel Becker | 4670c46 | 2008-02-01 14:39:35 -0800 | [diff] [blame] | 24 | #include <linux/slab.h> |
Joel Becker | 6953b4c | 2008-01-29 16:59:56 -0800 | [diff] [blame] | 25 | #include <linux/kmod.h> |
Joel Becker | 4670c46 | 2008-02-01 14:39:35 -0800 | [diff] [blame] | 26 | |
Joel Becker | 24ef181 | 2008-01-29 17:37:32 -0800 | [diff] [blame] | 27 | #include "stackglue.h" |
| 28 | |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame^] | 29 | static struct ocfs2_locking_protocol *lproto; |
| 30 | static DEFINE_SPINLOCK(ocfs2_stack_lock); |
| 31 | static LIST_HEAD(ocfs2_stack_list); |
| 32 | |
| 33 | /* |
| 34 | * The stack currently in use. If not null, active_stack->sp_count > 0, |
| 35 | * the module is pinned, and the locking protocol cannot be changed. |
| 36 | */ |
| 37 | static struct ocfs2_stack_plugin *active_stack; |
| 38 | |
| 39 | static struct ocfs2_stack_plugin *ocfs2_stack_lookup(const char *name) |
| 40 | { |
| 41 | struct ocfs2_stack_plugin *p; |
| 42 | |
| 43 | assert_spin_locked(&ocfs2_stack_lock); |
| 44 | |
| 45 | list_for_each_entry(p, &ocfs2_stack_list, sp_list) { |
| 46 | if (!strcmp(p->sp_name, name)) |
| 47 | return p; |
| 48 | } |
| 49 | |
| 50 | return NULL; |
| 51 | } |
| 52 | |
| 53 | static int ocfs2_stack_driver_request(const char *name) |
| 54 | { |
| 55 | int rc; |
| 56 | struct ocfs2_stack_plugin *p; |
| 57 | |
| 58 | spin_lock(&ocfs2_stack_lock); |
| 59 | |
| 60 | if (active_stack) { |
| 61 | /* |
| 62 | * If the active stack isn't the one we want, it cannot |
| 63 | * be selected right now. |
| 64 | */ |
| 65 | if (!strcmp(active_stack->sp_name, name)) |
| 66 | rc = 0; |
| 67 | else |
| 68 | rc = -EBUSY; |
| 69 | goto out; |
| 70 | } |
| 71 | |
| 72 | p = ocfs2_stack_lookup(name); |
| 73 | if (!p || !try_module_get(p->sp_owner)) { |
| 74 | rc = -ENOENT; |
| 75 | goto out; |
| 76 | } |
| 77 | |
| 78 | /* Ok, the stack is pinned */ |
| 79 | p->sp_count++; |
| 80 | active_stack = p; |
| 81 | |
| 82 | rc = 0; |
| 83 | |
| 84 | out: |
| 85 | spin_unlock(&ocfs2_stack_lock); |
| 86 | return rc; |
| 87 | } |
| 88 | |
| 89 | /* |
| 90 | * This function looks up the appropriate stack and makes it active. If |
| 91 | * there is no stack, it tries to load it. It will fail if the stack still |
| 92 | * cannot be found. It will also fail if a different stack is in use. |
| 93 | */ |
| 94 | static int ocfs2_stack_driver_get(const char *name) |
| 95 | { |
| 96 | int rc; |
| 97 | |
| 98 | rc = ocfs2_stack_driver_request(name); |
| 99 | if (rc == -ENOENT) { |
| 100 | request_module("ocfs2_stack_%s", name); |
| 101 | rc = ocfs2_stack_driver_request(name); |
| 102 | } |
| 103 | |
| 104 | if (rc == -ENOENT) { |
| 105 | printk(KERN_ERR |
| 106 | "ocfs2: Cluster stack driver \"%s\" cannot be found\n", |
| 107 | name); |
| 108 | } else if (rc == -EBUSY) { |
| 109 | printk(KERN_ERR |
| 110 | "ocfs2: A different cluster stack driver is in use\n"); |
| 111 | } |
| 112 | |
| 113 | return rc; |
| 114 | } |
| 115 | |
| 116 | static void ocfs2_stack_driver_put(void) |
| 117 | { |
| 118 | spin_lock(&ocfs2_stack_lock); |
| 119 | BUG_ON(active_stack == NULL); |
| 120 | BUG_ON(active_stack->sp_count == 0); |
| 121 | |
| 122 | active_stack->sp_count--; |
| 123 | if (!active_stack->sp_count) { |
| 124 | module_put(active_stack->sp_owner); |
| 125 | active_stack = NULL; |
| 126 | } |
| 127 | spin_unlock(&ocfs2_stack_lock); |
| 128 | } |
| 129 | |
| 130 | int ocfs2_stack_glue_register(struct ocfs2_stack_plugin *plugin) |
| 131 | { |
| 132 | int rc; |
| 133 | |
| 134 | spin_lock(&ocfs2_stack_lock); |
| 135 | if (!ocfs2_stack_lookup(plugin->sp_name)) { |
| 136 | plugin->sp_count = 0; |
| 137 | plugin->sp_proto = lproto; |
| 138 | list_add(&plugin->sp_list, &ocfs2_stack_list); |
| 139 | printk(KERN_INFO "ocfs2: Registered cluster interface %s\n", |
| 140 | plugin->sp_name); |
| 141 | rc = 0; |
| 142 | } else { |
| 143 | printk(KERN_ERR "ocfs2: Stack \"%s\" already registered\n", |
| 144 | plugin->sp_name); |
| 145 | rc = -EEXIST; |
| 146 | } |
| 147 | spin_unlock(&ocfs2_stack_lock); |
| 148 | |
| 149 | return rc; |
| 150 | } |
| 151 | EXPORT_SYMBOL_GPL(ocfs2_stack_glue_register); |
| 152 | |
| 153 | void ocfs2_stack_glue_unregister(struct ocfs2_stack_plugin *plugin) |
| 154 | { |
| 155 | struct ocfs2_stack_plugin *p; |
| 156 | |
| 157 | spin_lock(&ocfs2_stack_lock); |
| 158 | p = ocfs2_stack_lookup(plugin->sp_name); |
| 159 | if (p) { |
| 160 | BUG_ON(p != plugin); |
| 161 | BUG_ON(plugin == active_stack); |
| 162 | BUG_ON(plugin->sp_count != 0); |
| 163 | list_del_init(&plugin->sp_list); |
| 164 | printk(KERN_INFO "ocfs2: Unregistered cluster interface %s\n", |
| 165 | plugin->sp_name); |
| 166 | } else { |
| 167 | printk(KERN_ERR "Stack \"%s\" is not registered\n", |
| 168 | plugin->sp_name); |
| 169 | } |
| 170 | spin_unlock(&ocfs2_stack_lock); |
| 171 | } |
| 172 | EXPORT_SYMBOL_GPL(ocfs2_stack_glue_unregister); |
| 173 | |
| 174 | void ocfs2_stack_glue_set_locking_protocol(struct ocfs2_locking_protocol *proto) |
| 175 | { |
| 176 | struct ocfs2_stack_plugin *p; |
| 177 | |
| 178 | BUG_ON(proto == NULL); |
| 179 | |
| 180 | spin_lock(&ocfs2_stack_lock); |
| 181 | BUG_ON(active_stack != NULL); |
| 182 | |
| 183 | lproto = proto; |
| 184 | list_for_each_entry(p, &ocfs2_stack_list, sp_list) { |
| 185 | p->sp_proto = lproto; |
| 186 | } |
| 187 | |
| 188 | spin_unlock(&ocfs2_stack_lock); |
| 189 | } |
| 190 | EXPORT_SYMBOL_GPL(ocfs2_stack_glue_set_locking_protocol); |
Joel Becker | 24ef181 | 2008-01-29 17:37:32 -0800 | [diff] [blame] | 191 | |
Joel Becker | 24ef181 | 2008-01-29 17:37:32 -0800 | [diff] [blame] | 192 | |
Joel Becker | 553aa7e | 2008-02-01 14:51:03 -0800 | [diff] [blame] | 193 | int ocfs2_dlm_lock(struct ocfs2_cluster_connection *conn, |
| 194 | int mode, |
| 195 | union ocfs2_dlm_lksb *lksb, |
| 196 | u32 flags, |
| 197 | void *name, |
| 198 | unsigned int namelen, |
| 199 | void *astarg) |
| 200 | { |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame^] | 201 | BUG_ON(lproto == NULL); |
Joel Becker | 553aa7e | 2008-02-01 14:51:03 -0800 | [diff] [blame] | 202 | |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame^] | 203 | return active_stack->sp_ops->dlm_lock(conn, mode, lksb, flags, |
| 204 | name, namelen, astarg); |
Joel Becker | 24ef181 | 2008-01-29 17:37:32 -0800 | [diff] [blame] | 205 | } |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame^] | 206 | EXPORT_SYMBOL_GPL(ocfs2_dlm_lock); |
Joel Becker | 24ef181 | 2008-01-29 17:37:32 -0800 | [diff] [blame] | 207 | |
Joel Becker | 553aa7e | 2008-02-01 14:51:03 -0800 | [diff] [blame] | 208 | int ocfs2_dlm_unlock(struct ocfs2_cluster_connection *conn, |
| 209 | union ocfs2_dlm_lksb *lksb, |
| 210 | u32 flags, |
| 211 | void *astarg) |
| 212 | { |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame^] | 213 | BUG_ON(lproto == NULL); |
Joel Becker | 553aa7e | 2008-02-01 14:51:03 -0800 | [diff] [blame] | 214 | |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame^] | 215 | return active_stack->sp_ops->dlm_unlock(conn, lksb, flags, astarg); |
Joel Becker | 8f2c9c1 | 2008-02-01 12:16:57 -0800 | [diff] [blame] | 216 | } |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame^] | 217 | EXPORT_SYMBOL_GPL(ocfs2_dlm_unlock); |
Joel Becker | 8f2c9c1 | 2008-02-01 12:16:57 -0800 | [diff] [blame] | 218 | |
Joel Becker | 553aa7e | 2008-02-01 14:51:03 -0800 | [diff] [blame] | 219 | int ocfs2_dlm_lock_status(union ocfs2_dlm_lksb *lksb) |
| 220 | { |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame^] | 221 | return active_stack->sp_ops->lock_status(lksb); |
Joel Becker | 553aa7e | 2008-02-01 14:51:03 -0800 | [diff] [blame] | 222 | } |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame^] | 223 | EXPORT_SYMBOL_GPL(ocfs2_dlm_lock_status); |
Joel Becker | 553aa7e | 2008-02-01 14:51:03 -0800 | [diff] [blame] | 224 | |
Joel Becker | 8f2c9c1 | 2008-02-01 12:16:57 -0800 | [diff] [blame] | 225 | /* |
| 226 | * Why don't we cast to ocfs2_meta_lvb? The "clean" answer is that we |
| 227 | * don't cast at the glue level. The real answer is that the header |
| 228 | * ordering is nigh impossible. |
| 229 | */ |
Joel Becker | 553aa7e | 2008-02-01 14:51:03 -0800 | [diff] [blame] | 230 | void *ocfs2_dlm_lvb(union ocfs2_dlm_lksb *lksb) |
| 231 | { |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame^] | 232 | return active_stack->sp_ops->lock_lvb(lksb); |
Joel Becker | cf0acdc | 2008-01-29 16:59:55 -0800 | [diff] [blame] | 233 | } |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame^] | 234 | EXPORT_SYMBOL_GPL(ocfs2_dlm_lvb); |
Joel Becker | cf0acdc | 2008-01-29 16:59:55 -0800 | [diff] [blame] | 235 | |
Joel Becker | 553aa7e | 2008-02-01 14:51:03 -0800 | [diff] [blame] | 236 | void ocfs2_dlm_dump_lksb(union ocfs2_dlm_lksb *lksb) |
| 237 | { |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame^] | 238 | active_stack->sp_ops->dump_lksb(lksb); |
Joel Becker | 553aa7e | 2008-02-01 14:51:03 -0800 | [diff] [blame] | 239 | } |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame^] | 240 | EXPORT_SYMBOL_GPL(ocfs2_dlm_dump_lksb); |
Joel Becker | 553aa7e | 2008-02-01 14:51:03 -0800 | [diff] [blame] | 241 | |
Joel Becker | 4670c46 | 2008-02-01 14:39:35 -0800 | [diff] [blame] | 242 | int ocfs2_cluster_connect(const char *group, |
| 243 | int grouplen, |
| 244 | void (*recovery_handler)(int node_num, |
| 245 | void *recovery_data), |
| 246 | void *recovery_data, |
| 247 | struct ocfs2_cluster_connection **conn) |
| 248 | { |
| 249 | int rc = 0; |
| 250 | struct ocfs2_cluster_connection *new_conn; |
Joel Becker | 4670c46 | 2008-02-01 14:39:35 -0800 | [diff] [blame] | 251 | |
| 252 | BUG_ON(group == NULL); |
| 253 | BUG_ON(conn == NULL); |
| 254 | BUG_ON(recovery_handler == NULL); |
| 255 | |
| 256 | if (grouplen > GROUP_NAME_MAX) { |
| 257 | rc = -EINVAL; |
| 258 | goto out; |
| 259 | } |
| 260 | |
| 261 | new_conn = kzalloc(sizeof(struct ocfs2_cluster_connection), |
| 262 | GFP_KERNEL); |
| 263 | if (!new_conn) { |
| 264 | rc = -ENOMEM; |
| 265 | goto out; |
| 266 | } |
| 267 | |
| 268 | memcpy(new_conn->cc_name, group, grouplen); |
| 269 | new_conn->cc_namelen = grouplen; |
| 270 | new_conn->cc_recovery_handler = recovery_handler; |
| 271 | new_conn->cc_recovery_data = recovery_data; |
| 272 | |
| 273 | /* Start the new connection at our maximum compatibility level */ |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame^] | 274 | new_conn->cc_version = lproto->lp_max_version; |
Joel Becker | 4670c46 | 2008-02-01 14:39:35 -0800 | [diff] [blame] | 275 | |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame^] | 276 | /* This will pin the stack driver if successful */ |
| 277 | rc = ocfs2_stack_driver_get("o2cb"); |
| 278 | if (rc) |
| 279 | goto out_free; |
| 280 | |
| 281 | rc = active_stack->sp_ops->connect(new_conn); |
Joel Becker | 553aa7e | 2008-02-01 14:51:03 -0800 | [diff] [blame] | 282 | if (rc) { |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame^] | 283 | ocfs2_stack_driver_put(); |
Joel Becker | 4670c46 | 2008-02-01 14:39:35 -0800 | [diff] [blame] | 284 | goto out_free; |
| 285 | } |
| 286 | |
Joel Becker | 4670c46 | 2008-02-01 14:39:35 -0800 | [diff] [blame] | 287 | *conn = new_conn; |
| 288 | |
| 289 | out_free: |
Joel Becker | 553aa7e | 2008-02-01 14:51:03 -0800 | [diff] [blame] | 290 | if (rc) |
Joel Becker | 4670c46 | 2008-02-01 14:39:35 -0800 | [diff] [blame] | 291 | kfree(new_conn); |
Joel Becker | 4670c46 | 2008-02-01 14:39:35 -0800 | [diff] [blame] | 292 | |
| 293 | out: |
| 294 | return rc; |
| 295 | } |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame^] | 296 | EXPORT_SYMBOL_GPL(ocfs2_cluster_connect); |
Joel Becker | 4670c46 | 2008-02-01 14:39:35 -0800 | [diff] [blame] | 297 | |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame^] | 298 | /* If hangup_pending is 0, the stack driver will be dropped */ |
| 299 | int ocfs2_cluster_disconnect(struct ocfs2_cluster_connection *conn, |
| 300 | int hangup_pending) |
Joel Becker | 553aa7e | 2008-02-01 14:51:03 -0800 | [diff] [blame] | 301 | { |
| 302 | int ret; |
| 303 | |
| 304 | BUG_ON(conn == NULL); |
| 305 | |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame^] | 306 | ret = active_stack->sp_ops->disconnect(conn, hangup_pending); |
Joel Becker | 553aa7e | 2008-02-01 14:51:03 -0800 | [diff] [blame] | 307 | |
| 308 | /* XXX Should we free it anyway? */ |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame^] | 309 | if (!ret) { |
Joel Becker | 553aa7e | 2008-02-01 14:51:03 -0800 | [diff] [blame] | 310 | kfree(conn); |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame^] | 311 | if (!hangup_pending) |
| 312 | ocfs2_stack_driver_put(); |
| 313 | } |
Joel Becker | 553aa7e | 2008-02-01 14:51:03 -0800 | [diff] [blame] | 314 | |
| 315 | return ret; |
| 316 | } |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame^] | 317 | EXPORT_SYMBOL_GPL(ocfs2_cluster_disconnect); |
Joel Becker | 553aa7e | 2008-02-01 14:51:03 -0800 | [diff] [blame] | 318 | |
Joel Becker | 6953b4c | 2008-01-29 16:59:56 -0800 | [diff] [blame] | 319 | void ocfs2_cluster_hangup(const char *group, int grouplen) |
| 320 | { |
| 321 | BUG_ON(group == NULL); |
| 322 | BUG_ON(group[grouplen] != '\0'); |
| 323 | |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame^] | 324 | active_stack->sp_ops->hangup(group, grouplen); |
| 325 | |
| 326 | /* cluster_disconnect() was called with hangup_pending==1 */ |
| 327 | ocfs2_stack_driver_put(); |
Joel Becker | 19fdb62 | 2008-01-30 15:38:24 -0800 | [diff] [blame] | 328 | } |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame^] | 329 | EXPORT_SYMBOL_GPL(ocfs2_cluster_hangup); |
Joel Becker | 19fdb62 | 2008-01-30 15:38:24 -0800 | [diff] [blame] | 330 | |
Joel Becker | 553aa7e | 2008-02-01 14:51:03 -0800 | [diff] [blame] | 331 | int ocfs2_cluster_this_node(unsigned int *node) |
| 332 | { |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame^] | 333 | return active_stack->sp_ops->this_node(node); |
Joel Becker | 553aa7e | 2008-02-01 14:51:03 -0800 | [diff] [blame] | 334 | } |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame^] | 335 | EXPORT_SYMBOL_GPL(ocfs2_cluster_this_node); |
Joel Becker | 553aa7e | 2008-02-01 14:51:03 -0800 | [diff] [blame] | 336 | |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame^] | 337 | |
| 338 | static int __init ocfs2_stack_glue_init(void) |
Joel Becker | 24ef181 | 2008-01-29 17:37:32 -0800 | [diff] [blame] | 339 | { |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame^] | 340 | return 0; |
Joel Becker | 24ef181 | 2008-01-29 17:37:32 -0800 | [diff] [blame] | 341 | } |
| 342 | |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame^] | 343 | static void __exit ocfs2_stack_glue_exit(void) |
| 344 | { |
| 345 | lproto = NULL; |
| 346 | } |
| 347 | |
| 348 | MODULE_AUTHOR("Oracle"); |
| 349 | MODULE_DESCRIPTION("ocfs2 cluter stack glue layer"); |
| 350 | MODULE_LICENSE("GPL"); |
| 351 | module_init(ocfs2_stack_glue_init); |
| 352 | module_exit(ocfs2_stack_glue_exit); |