Joel Becker | e3dad42 | 2008-02-01 15:02:36 -0800 | [diff] [blame] | 1 | /* -*- mode: c; c-basic-offset: 8; -*- |
| 2 | * vim: noexpandtab sw=8 ts=8 sts=0: |
| 3 | * |
| 4 | * stack_o2cb.c |
| 5 | * |
| 6 | * Code which interfaces ocfs2 with the o2cb stack. |
| 7 | * |
| 8 | * Copyright (C) 2007 Oracle. All rights reserved. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public |
| 12 | * License as published by the Free Software Foundation, version 2. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
| 18 | */ |
| 19 | |
| 20 | #include <linux/crc32.h> |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame] | 21 | #include <linux/module.h> |
Joel Becker | e3dad42 | 2008-02-01 15:02:36 -0800 | [diff] [blame] | 22 | |
| 23 | /* Needed for AOP_TRUNCATED_PAGE in mlog_errno() */ |
| 24 | #include <linux/fs.h> |
| 25 | |
| 26 | #include "cluster/masklog.h" |
| 27 | #include "cluster/nodemanager.h" |
| 28 | #include "cluster/heartbeat.h" |
| 29 | |
| 30 | #include "stackglue.h" |
| 31 | |
| 32 | struct o2dlm_private { |
| 33 | struct dlm_eviction_cb op_eviction_cb; |
| 34 | }; |
| 35 | |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame] | 36 | static struct ocfs2_stack_plugin o2cb_stack; |
| 37 | |
Joel Becker | e3dad42 | 2008-02-01 15:02:36 -0800 | [diff] [blame] | 38 | /* These should be identical */ |
| 39 | #if (DLM_LOCK_IV != LKM_IVMODE) |
| 40 | # error Lock modes do not match |
| 41 | #endif |
| 42 | #if (DLM_LOCK_NL != LKM_NLMODE) |
| 43 | # error Lock modes do not match |
| 44 | #endif |
| 45 | #if (DLM_LOCK_CR != LKM_CRMODE) |
| 46 | # error Lock modes do not match |
| 47 | #endif |
| 48 | #if (DLM_LOCK_CW != LKM_CWMODE) |
| 49 | # error Lock modes do not match |
| 50 | #endif |
| 51 | #if (DLM_LOCK_PR != LKM_PRMODE) |
| 52 | # error Lock modes do not match |
| 53 | #endif |
| 54 | #if (DLM_LOCK_PW != LKM_PWMODE) |
| 55 | # error Lock modes do not match |
| 56 | #endif |
| 57 | #if (DLM_LOCK_EX != LKM_EXMODE) |
| 58 | # error Lock modes do not match |
| 59 | #endif |
| 60 | static inline int mode_to_o2dlm(int mode) |
| 61 | { |
| 62 | BUG_ON(mode > LKM_MAXMODE); |
| 63 | |
| 64 | return mode; |
| 65 | } |
| 66 | |
| 67 | #define map_flag(_generic, _o2dlm) \ |
| 68 | if (flags & (_generic)) { \ |
| 69 | flags &= ~(_generic); \ |
| 70 | o2dlm_flags |= (_o2dlm); \ |
| 71 | } |
| 72 | static int flags_to_o2dlm(u32 flags) |
| 73 | { |
| 74 | int o2dlm_flags = 0; |
| 75 | |
| 76 | map_flag(DLM_LKF_NOQUEUE, LKM_NOQUEUE); |
| 77 | map_flag(DLM_LKF_CANCEL, LKM_CANCEL); |
| 78 | map_flag(DLM_LKF_CONVERT, LKM_CONVERT); |
| 79 | map_flag(DLM_LKF_VALBLK, LKM_VALBLK); |
| 80 | map_flag(DLM_LKF_IVVALBLK, LKM_INVVALBLK); |
| 81 | map_flag(DLM_LKF_ORPHAN, LKM_ORPHAN); |
| 82 | map_flag(DLM_LKF_FORCEUNLOCK, LKM_FORCE); |
| 83 | map_flag(DLM_LKF_TIMEOUT, LKM_TIMEOUT); |
| 84 | map_flag(DLM_LKF_LOCAL, LKM_LOCAL); |
| 85 | |
| 86 | /* map_flag() should have cleared every flag passed in */ |
| 87 | BUG_ON(flags != 0); |
| 88 | |
| 89 | return o2dlm_flags; |
| 90 | } |
| 91 | #undef map_flag |
| 92 | |
| 93 | /* |
| 94 | * Map an o2dlm status to standard errno values. |
| 95 | * |
| 96 | * o2dlm only uses a handful of these, and returns even fewer to the |
| 97 | * caller. Still, we try to assign sane values to each error. |
| 98 | * |
| 99 | * The following value pairs have special meanings to dlmglue, thus |
| 100 | * the right hand side needs to stay unique - never duplicate the |
| 101 | * mapping elsewhere in the table! |
| 102 | * |
| 103 | * DLM_NORMAL: 0 |
| 104 | * DLM_NOTQUEUED: -EAGAIN |
| 105 | * DLM_CANCELGRANT: -EBUSY |
| 106 | * DLM_CANCEL: -DLM_ECANCEL |
| 107 | */ |
| 108 | /* Keep in sync with dlmapi.h */ |
| 109 | static int status_map[] = { |
| 110 | [DLM_NORMAL] = 0, /* Success */ |
| 111 | [DLM_GRANTED] = -EINVAL, |
| 112 | [DLM_DENIED] = -EACCES, |
| 113 | [DLM_DENIED_NOLOCKS] = -EACCES, |
| 114 | [DLM_WORKING] = -EACCES, |
| 115 | [DLM_BLOCKED] = -EINVAL, |
| 116 | [DLM_BLOCKED_ORPHAN] = -EINVAL, |
| 117 | [DLM_DENIED_GRACE_PERIOD] = -EACCES, |
| 118 | [DLM_SYSERR] = -ENOMEM, /* It is what it is */ |
| 119 | [DLM_NOSUPPORT] = -EPROTO, |
| 120 | [DLM_CANCELGRANT] = -EBUSY, /* Cancel after grant */ |
| 121 | [DLM_IVLOCKID] = -EINVAL, |
| 122 | [DLM_SYNC] = -EINVAL, |
| 123 | [DLM_BADTYPE] = -EINVAL, |
| 124 | [DLM_BADRESOURCE] = -EINVAL, |
| 125 | [DLM_MAXHANDLES] = -ENOMEM, |
| 126 | [DLM_NOCLINFO] = -EINVAL, |
| 127 | [DLM_NOLOCKMGR] = -EINVAL, |
| 128 | [DLM_NOPURGED] = -EINVAL, |
| 129 | [DLM_BADARGS] = -EINVAL, |
| 130 | [DLM_VOID] = -EINVAL, |
| 131 | [DLM_NOTQUEUED] = -EAGAIN, /* Trylock failed */ |
| 132 | [DLM_IVBUFLEN] = -EINVAL, |
| 133 | [DLM_CVTUNGRANT] = -EPERM, |
| 134 | [DLM_BADPARAM] = -EINVAL, |
| 135 | [DLM_VALNOTVALID] = -EINVAL, |
| 136 | [DLM_REJECTED] = -EPERM, |
| 137 | [DLM_ABORT] = -EINVAL, |
| 138 | [DLM_CANCEL] = -DLM_ECANCEL, /* Successful cancel */ |
| 139 | [DLM_IVRESHANDLE] = -EINVAL, |
| 140 | [DLM_DEADLOCK] = -EDEADLK, |
| 141 | [DLM_DENIED_NOASTS] = -EINVAL, |
| 142 | [DLM_FORWARD] = -EINVAL, |
| 143 | [DLM_TIMEOUT] = -ETIMEDOUT, |
| 144 | [DLM_IVGROUPID] = -EINVAL, |
| 145 | [DLM_VERS_CONFLICT] = -EOPNOTSUPP, |
| 146 | [DLM_BAD_DEVICE_PATH] = -ENOENT, |
| 147 | [DLM_NO_DEVICE_PERMISSION] = -EPERM, |
| 148 | [DLM_NO_CONTROL_DEVICE] = -ENOENT, |
| 149 | [DLM_RECOVERING] = -ENOTCONN, |
| 150 | [DLM_MIGRATING] = -ERESTART, |
| 151 | [DLM_MAXSTATS] = -EINVAL, |
| 152 | }; |
| 153 | |
| 154 | static int dlm_status_to_errno(enum dlm_status status) |
| 155 | { |
| 156 | BUG_ON(status > (sizeof(status_map) / sizeof(status_map[0]))); |
| 157 | |
| 158 | return status_map[status]; |
| 159 | } |
| 160 | |
| 161 | static void o2dlm_lock_ast_wrapper(void *astarg) |
| 162 | { |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame] | 163 | BUG_ON(o2cb_stack.sp_proto == NULL); |
Joel Becker | e3dad42 | 2008-02-01 15:02:36 -0800 | [diff] [blame] | 164 | |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame] | 165 | o2cb_stack.sp_proto->lp_lock_ast(astarg); |
Joel Becker | e3dad42 | 2008-02-01 15:02:36 -0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | static void o2dlm_blocking_ast_wrapper(void *astarg, int level) |
| 169 | { |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame] | 170 | BUG_ON(o2cb_stack.sp_proto == NULL); |
Joel Becker | e3dad42 | 2008-02-01 15:02:36 -0800 | [diff] [blame] | 171 | |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame] | 172 | o2cb_stack.sp_proto->lp_blocking_ast(astarg, level); |
Joel Becker | e3dad42 | 2008-02-01 15:02:36 -0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | static void o2dlm_unlock_ast_wrapper(void *astarg, enum dlm_status status) |
| 176 | { |
| 177 | int error = dlm_status_to_errno(status); |
| 178 | |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame] | 179 | BUG_ON(o2cb_stack.sp_proto == NULL); |
Joel Becker | e3dad42 | 2008-02-01 15:02:36 -0800 | [diff] [blame] | 180 | |
| 181 | /* |
| 182 | * In o2dlm, you can get both the lock_ast() for the lock being |
| 183 | * granted and the unlock_ast() for the CANCEL failing. A |
| 184 | * successful cancel sends DLM_NORMAL here. If the |
| 185 | * lock grant happened before the cancel arrived, you get |
| 186 | * DLM_CANCELGRANT. |
| 187 | * |
| 188 | * There's no need for the double-ast. If we see DLM_CANCELGRANT, |
| 189 | * we just ignore it. We expect the lock_ast() to handle the |
| 190 | * granted lock. |
| 191 | */ |
| 192 | if (status == DLM_CANCELGRANT) |
| 193 | return; |
| 194 | |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame] | 195 | o2cb_stack.sp_proto->lp_unlock_ast(astarg, error); |
Joel Becker | e3dad42 | 2008-02-01 15:02:36 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | static int o2cb_dlm_lock(struct ocfs2_cluster_connection *conn, |
| 199 | int mode, |
| 200 | union ocfs2_dlm_lksb *lksb, |
| 201 | u32 flags, |
| 202 | void *name, |
| 203 | unsigned int namelen, |
| 204 | void *astarg) |
| 205 | { |
| 206 | enum dlm_status status; |
| 207 | int o2dlm_mode = mode_to_o2dlm(mode); |
| 208 | int o2dlm_flags = flags_to_o2dlm(flags); |
| 209 | int ret; |
| 210 | |
| 211 | status = dlmlock(conn->cc_lockspace, o2dlm_mode, &lksb->lksb_o2dlm, |
| 212 | o2dlm_flags, name, namelen, |
| 213 | o2dlm_lock_ast_wrapper, astarg, |
| 214 | o2dlm_blocking_ast_wrapper); |
| 215 | ret = dlm_status_to_errno(status); |
| 216 | return ret; |
| 217 | } |
| 218 | |
| 219 | static int o2cb_dlm_unlock(struct ocfs2_cluster_connection *conn, |
| 220 | union ocfs2_dlm_lksb *lksb, |
| 221 | u32 flags, |
| 222 | void *astarg) |
| 223 | { |
| 224 | enum dlm_status status; |
| 225 | int o2dlm_flags = flags_to_o2dlm(flags); |
| 226 | int ret; |
| 227 | |
| 228 | status = dlmunlock(conn->cc_lockspace, &lksb->lksb_o2dlm, |
| 229 | o2dlm_flags, o2dlm_unlock_ast_wrapper, astarg); |
| 230 | ret = dlm_status_to_errno(status); |
| 231 | return ret; |
| 232 | } |
| 233 | |
| 234 | static int o2cb_dlm_lock_status(union ocfs2_dlm_lksb *lksb) |
| 235 | { |
| 236 | return dlm_status_to_errno(lksb->lksb_o2dlm.status); |
| 237 | } |
| 238 | |
| 239 | static void *o2cb_dlm_lvb(union ocfs2_dlm_lksb *lksb) |
| 240 | { |
| 241 | return (void *)(lksb->lksb_o2dlm.lvb); |
| 242 | } |
| 243 | |
| 244 | static void o2cb_dump_lksb(union ocfs2_dlm_lksb *lksb) |
| 245 | { |
| 246 | dlm_print_one_lock(lksb->lksb_o2dlm.lockid); |
| 247 | } |
| 248 | |
| 249 | /* |
| 250 | * Called from the dlm when it's about to evict a node. This is how the |
| 251 | * classic stack signals node death. |
| 252 | */ |
| 253 | static void o2dlm_eviction_cb(int node_num, void *data) |
| 254 | { |
| 255 | struct ocfs2_cluster_connection *conn = data; |
| 256 | |
| 257 | mlog(ML_NOTICE, "o2dlm has evicted node %d from group %.*s\n", |
| 258 | node_num, conn->cc_namelen, conn->cc_name); |
| 259 | |
| 260 | conn->cc_recovery_handler(node_num, conn->cc_recovery_data); |
| 261 | } |
| 262 | |
| 263 | static int o2cb_cluster_connect(struct ocfs2_cluster_connection *conn) |
| 264 | { |
| 265 | int rc = 0; |
| 266 | u32 dlm_key; |
| 267 | struct dlm_ctxt *dlm; |
| 268 | struct o2dlm_private *priv; |
| 269 | struct dlm_protocol_version dlm_version; |
| 270 | |
| 271 | BUG_ON(conn == NULL); |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame] | 272 | BUG_ON(o2cb_stack.sp_proto == NULL); |
Joel Becker | e3dad42 | 2008-02-01 15:02:36 -0800 | [diff] [blame] | 273 | |
| 274 | /* for now we only have one cluster/node, make sure we see it |
| 275 | * in the heartbeat universe */ |
| 276 | if (!o2hb_check_local_node_heartbeating()) { |
| 277 | rc = -EINVAL; |
| 278 | goto out; |
| 279 | } |
| 280 | |
| 281 | priv = kzalloc(sizeof(struct o2dlm_private), GFP_KERNEL); |
| 282 | if (!priv) { |
| 283 | rc = -ENOMEM; |
| 284 | goto out_free; |
| 285 | } |
| 286 | |
| 287 | /* This just fills the structure in. It is safe to pass conn. */ |
| 288 | dlm_setup_eviction_cb(&priv->op_eviction_cb, o2dlm_eviction_cb, |
| 289 | conn); |
| 290 | |
| 291 | conn->cc_private = priv; |
| 292 | |
| 293 | /* used by the dlm code to make message headers unique, each |
| 294 | * node in this domain must agree on this. */ |
| 295 | dlm_key = crc32_le(0, conn->cc_name, conn->cc_namelen); |
| 296 | dlm_version.pv_major = conn->cc_version.pv_major; |
| 297 | dlm_version.pv_minor = conn->cc_version.pv_minor; |
| 298 | |
| 299 | dlm = dlm_register_domain(conn->cc_name, dlm_key, &dlm_version); |
| 300 | if (IS_ERR(dlm)) { |
| 301 | rc = PTR_ERR(dlm); |
| 302 | mlog_errno(rc); |
| 303 | goto out_free; |
| 304 | } |
| 305 | |
| 306 | conn->cc_version.pv_major = dlm_version.pv_major; |
| 307 | conn->cc_version.pv_minor = dlm_version.pv_minor; |
| 308 | conn->cc_lockspace = dlm; |
| 309 | |
| 310 | dlm_register_eviction_cb(dlm, &priv->op_eviction_cb); |
| 311 | |
| 312 | out_free: |
| 313 | if (rc && conn->cc_private) |
| 314 | kfree(conn->cc_private); |
| 315 | |
| 316 | out: |
| 317 | return rc; |
| 318 | } |
| 319 | |
Joel Becker | 2c39450 | 2008-05-30 15:58:26 -0700 | [diff] [blame] | 320 | static int o2cb_cluster_disconnect(struct ocfs2_cluster_connection *conn) |
Joel Becker | e3dad42 | 2008-02-01 15:02:36 -0800 | [diff] [blame] | 321 | { |
| 322 | struct dlm_ctxt *dlm = conn->cc_lockspace; |
| 323 | struct o2dlm_private *priv = conn->cc_private; |
| 324 | |
| 325 | dlm_unregister_eviction_cb(&priv->op_eviction_cb); |
| 326 | conn->cc_private = NULL; |
| 327 | kfree(priv); |
| 328 | |
| 329 | dlm_unregister_domain(dlm); |
| 330 | conn->cc_lockspace = NULL; |
| 331 | |
| 332 | return 0; |
| 333 | } |
| 334 | |
Joel Becker | e3dad42 | 2008-02-01 15:02:36 -0800 | [diff] [blame] | 335 | static int o2cb_cluster_this_node(unsigned int *node) |
| 336 | { |
| 337 | int node_num; |
| 338 | |
| 339 | node_num = o2nm_this_node(); |
| 340 | if (node_num == O2NM_INVALID_NODE_NUM) |
| 341 | return -ENOENT; |
| 342 | |
| 343 | if (node_num >= O2NM_MAX_NODES) |
| 344 | return -EOVERFLOW; |
| 345 | |
| 346 | *node = node_num; |
| 347 | return 0; |
| 348 | } |
| 349 | |
Adrian Bunk | 4af694e | 2008-04-21 11:49:31 +0300 | [diff] [blame] | 350 | static struct ocfs2_stack_operations o2cb_stack_ops = { |
Joel Becker | e3dad42 | 2008-02-01 15:02:36 -0800 | [diff] [blame] | 351 | .connect = o2cb_cluster_connect, |
| 352 | .disconnect = o2cb_cluster_disconnect, |
Joel Becker | e3dad42 | 2008-02-01 15:02:36 -0800 | [diff] [blame] | 353 | .this_node = o2cb_cluster_this_node, |
| 354 | .dlm_lock = o2cb_dlm_lock, |
| 355 | .dlm_unlock = o2cb_dlm_unlock, |
| 356 | .lock_status = o2cb_dlm_lock_status, |
| 357 | .lock_lvb = o2cb_dlm_lvb, |
| 358 | .dump_lksb = o2cb_dump_lksb, |
| 359 | }; |
| 360 | |
Joel Becker | 286eaa9 | 2008-02-01 15:03:57 -0800 | [diff] [blame] | 361 | static struct ocfs2_stack_plugin o2cb_stack = { |
| 362 | .sp_name = "o2cb", |
| 363 | .sp_ops = &o2cb_stack_ops, |
| 364 | .sp_owner = THIS_MODULE, |
| 365 | }; |
| 366 | |
| 367 | static int __init o2cb_stack_init(void) |
| 368 | { |
| 369 | return ocfs2_stack_glue_register(&o2cb_stack); |
| 370 | } |
| 371 | |
| 372 | static void __exit o2cb_stack_exit(void) |
| 373 | { |
| 374 | ocfs2_stack_glue_unregister(&o2cb_stack); |
| 375 | } |
| 376 | |
| 377 | MODULE_AUTHOR("Oracle"); |
| 378 | MODULE_DESCRIPTION("ocfs2 driver for the classic o2cb stack"); |
| 379 | MODULE_LICENSE("GPL"); |
| 380 | module_init(o2cb_stack_init); |
| 381 | module_exit(o2cb_stack_exit); |