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 | 4670c46 | 2008-02-01 14:39:35 -0800 | [diff] [blame^] | 21 | #include <linux/slab.h> |
| 22 | #include <linux/crc32.h> |
| 23 | |
| 24 | /* Needed for AOP_TRUNCATED_PAGE in mlog_errno() */ |
| 25 | #include <linux/fs.h> |
| 26 | |
Joel Becker | 7431cd7 | 2008-02-01 12:15:37 -0800 | [diff] [blame] | 27 | #include "cluster/masklog.h" |
Joel Becker | 24ef181 | 2008-01-29 17:37:32 -0800 | [diff] [blame] | 28 | #include "stackglue.h" |
| 29 | |
| 30 | static struct ocfs2_locking_protocol *lproto; |
| 31 | |
Joel Becker | 4670c46 | 2008-02-01 14:39:35 -0800 | [diff] [blame^] | 32 | struct o2dlm_private { |
| 33 | struct dlm_eviction_cb op_eviction_cb; |
| 34 | }; |
| 35 | |
Joel Becker | bd3e761 | 2008-02-01 12:14:57 -0800 | [diff] [blame] | 36 | /* These should be identical */ |
| 37 | #if (DLM_LOCK_IV != LKM_IVMODE) |
| 38 | # error Lock modes do not match |
| 39 | #endif |
| 40 | #if (DLM_LOCK_NL != LKM_NLMODE) |
| 41 | # error Lock modes do not match |
| 42 | #endif |
| 43 | #if (DLM_LOCK_CR != LKM_CRMODE) |
| 44 | # error Lock modes do not match |
| 45 | #endif |
| 46 | #if (DLM_LOCK_CW != LKM_CWMODE) |
| 47 | # error Lock modes do not match |
| 48 | #endif |
| 49 | #if (DLM_LOCK_PR != LKM_PRMODE) |
| 50 | # error Lock modes do not match |
| 51 | #endif |
| 52 | #if (DLM_LOCK_PW != LKM_PWMODE) |
| 53 | # error Lock modes do not match |
| 54 | #endif |
| 55 | #if (DLM_LOCK_EX != LKM_EXMODE) |
| 56 | # error Lock modes do not match |
| 57 | #endif |
| 58 | static inline int mode_to_o2dlm(int mode) |
| 59 | { |
| 60 | BUG_ON(mode > LKM_MAXMODE); |
| 61 | |
| 62 | return mode; |
| 63 | } |
| 64 | |
| 65 | #define map_flag(_generic, _o2dlm) \ |
| 66 | if (flags & (_generic)) { \ |
| 67 | flags &= ~(_generic); \ |
| 68 | o2dlm_flags |= (_o2dlm); \ |
| 69 | } |
| 70 | static int flags_to_o2dlm(u32 flags) |
| 71 | { |
| 72 | int o2dlm_flags = 0; |
| 73 | |
| 74 | map_flag(DLM_LKF_NOQUEUE, LKM_NOQUEUE); |
| 75 | map_flag(DLM_LKF_CANCEL, LKM_CANCEL); |
| 76 | map_flag(DLM_LKF_CONVERT, LKM_CONVERT); |
| 77 | map_flag(DLM_LKF_VALBLK, LKM_VALBLK); |
| 78 | map_flag(DLM_LKF_IVVALBLK, LKM_INVVALBLK); |
| 79 | map_flag(DLM_LKF_ORPHAN, LKM_ORPHAN); |
| 80 | map_flag(DLM_LKF_FORCEUNLOCK, LKM_FORCE); |
| 81 | map_flag(DLM_LKF_TIMEOUT, LKM_TIMEOUT); |
| 82 | map_flag(DLM_LKF_LOCAL, LKM_LOCAL); |
| 83 | |
| 84 | /* map_flag() should have cleared every flag passed in */ |
| 85 | BUG_ON(flags != 0); |
| 86 | |
| 87 | return o2dlm_flags; |
| 88 | } |
| 89 | #undef map_flag |
| 90 | |
Joel Becker | 7431cd7 | 2008-02-01 12:15:37 -0800 | [diff] [blame] | 91 | /* |
| 92 | * Map an o2dlm status to standard errno values. |
| 93 | * |
| 94 | * o2dlm only uses a handful of these, and returns even fewer to the |
| 95 | * caller. Still, we try to assign sane values to each error. |
| 96 | * |
| 97 | * The following value pairs have special meanings to dlmglue, thus |
| 98 | * the right hand side needs to stay unique - never duplicate the |
| 99 | * mapping elsewhere in the table! |
| 100 | * |
| 101 | * DLM_NORMAL: 0 |
| 102 | * DLM_NOTQUEUED: -EAGAIN |
| 103 | * DLM_CANCELGRANT: -DLM_ECANCEL |
| 104 | * DLM_CANCEL: -DLM_EUNLOCK |
| 105 | */ |
| 106 | /* Keep in sync with dlmapi.h */ |
| 107 | static int status_map[] = { |
| 108 | [DLM_NORMAL] = 0, /* Success */ |
| 109 | [DLM_GRANTED] = -EINVAL, |
| 110 | [DLM_DENIED] = -EACCES, |
| 111 | [DLM_DENIED_NOLOCKS] = -EACCES, |
| 112 | [DLM_WORKING] = -EBUSY, |
| 113 | [DLM_BLOCKED] = -EINVAL, |
| 114 | [DLM_BLOCKED_ORPHAN] = -EINVAL, |
| 115 | [DLM_DENIED_GRACE_PERIOD] = -EACCES, |
| 116 | [DLM_SYSERR] = -ENOMEM, /* It is what it is */ |
| 117 | [DLM_NOSUPPORT] = -EPROTO, |
| 118 | [DLM_CANCELGRANT] = -DLM_ECANCEL, /* Cancel after grant */ |
| 119 | [DLM_IVLOCKID] = -EINVAL, |
| 120 | [DLM_SYNC] = -EINVAL, |
| 121 | [DLM_BADTYPE] = -EINVAL, |
| 122 | [DLM_BADRESOURCE] = -EINVAL, |
| 123 | [DLM_MAXHANDLES] = -ENOMEM, |
| 124 | [DLM_NOCLINFO] = -EINVAL, |
| 125 | [DLM_NOLOCKMGR] = -EINVAL, |
| 126 | [DLM_NOPURGED] = -EINVAL, |
| 127 | [DLM_BADARGS] = -EINVAL, |
| 128 | [DLM_VOID] = -EINVAL, |
| 129 | [DLM_NOTQUEUED] = -EAGAIN, /* Trylock failed */ |
| 130 | [DLM_IVBUFLEN] = -EINVAL, |
| 131 | [DLM_CVTUNGRANT] = -EPERM, |
| 132 | [DLM_BADPARAM] = -EINVAL, |
| 133 | [DLM_VALNOTVALID] = -EINVAL, |
| 134 | [DLM_REJECTED] = -EPERM, |
| 135 | [DLM_ABORT] = -EINVAL, |
| 136 | [DLM_CANCEL] = -DLM_EUNLOCK, /* Successful cancel */ |
| 137 | [DLM_IVRESHANDLE] = -EINVAL, |
| 138 | [DLM_DEADLOCK] = -EDEADLK, |
| 139 | [DLM_DENIED_NOASTS] = -EINVAL, |
| 140 | [DLM_FORWARD] = -EINVAL, |
| 141 | [DLM_TIMEOUT] = -ETIMEDOUT, |
| 142 | [DLM_IVGROUPID] = -EINVAL, |
| 143 | [DLM_VERS_CONFLICT] = -EOPNOTSUPP, |
| 144 | [DLM_BAD_DEVICE_PATH] = -ENOENT, |
| 145 | [DLM_NO_DEVICE_PERMISSION] = -EPERM, |
| 146 | [DLM_NO_CONTROL_DEVICE] = -ENOENT, |
| 147 | [DLM_RECOVERING] = -ENOTCONN, |
| 148 | [DLM_MIGRATING] = -ERESTART, |
| 149 | [DLM_MAXSTATS] = -EINVAL, |
| 150 | }; |
| 151 | static int dlm_status_to_errno(enum dlm_status status) |
| 152 | { |
| 153 | BUG_ON(status > (sizeof(status_map) / sizeof(status_map[0]))); |
| 154 | |
| 155 | return status_map[status]; |
| 156 | } |
| 157 | |
| 158 | static void o2dlm_lock_ast_wrapper(void *astarg) |
| 159 | { |
| 160 | BUG_ON(lproto == NULL); |
| 161 | |
| 162 | lproto->lp_lock_ast(astarg); |
| 163 | } |
| 164 | |
| 165 | static void o2dlm_blocking_ast_wrapper(void *astarg, int level) |
| 166 | { |
| 167 | BUG_ON(lproto == NULL); |
| 168 | |
| 169 | lproto->lp_blocking_ast(astarg, level); |
| 170 | } |
| 171 | |
| 172 | static void o2dlm_unlock_ast_wrapper(void *astarg, enum dlm_status status) |
| 173 | { |
| 174 | int error; |
| 175 | |
| 176 | BUG_ON(lproto == NULL); |
| 177 | |
| 178 | /* |
| 179 | * XXX: CANCEL values are sketchy. |
| 180 | * |
| 181 | * Currently we have preserved the o2dlm paradigm. You can get |
| 182 | * unlock_ast() whether the cancel succeded or not. |
| 183 | * |
| 184 | * First, we're going to pass DLM_EUNLOCK just like fs/dlm does for |
| 185 | * successful unlocks. That is a clean behavior. |
| 186 | * |
| 187 | * In o2dlm, you can get both the lock_ast() for the lock being |
| 188 | * granted and the unlock_ast() for the CANCEL failing. A |
| 189 | * successful cancel sends DLM_NORMAL here. If the |
| 190 | * lock grant happened before the cancel arrived, you get |
| 191 | * DLM_CANCELGRANT. For now, we'll use DLM_ECANCEL to signify |
| 192 | * CANCELGRANT - the CANCEL was supposed to happen but didn't. We |
| 193 | * can then use DLM_EUNLOCK to signify a successful CANCEL - |
| 194 | * effectively, the CANCEL caused the lock to roll back. |
| 195 | * |
| 196 | * In the future, we will likely move the o2dlm to send only one |
| 197 | * ast - either unlock_ast() for a successful CANCEL or lock_ast() |
| 198 | * when the grant succeeds. At that point, we'll send DLM_ECANCEL |
| 199 | * for all cancel results (CANCELGRANT will no longer exist). |
| 200 | */ |
| 201 | error = dlm_status_to_errno(status); |
| 202 | |
| 203 | /* Successful unlock is DLM_EUNLOCK */ |
| 204 | if (!error) |
| 205 | error = -DLM_EUNLOCK; |
| 206 | |
| 207 | lproto->lp_unlock_ast(astarg, error); |
| 208 | } |
| 209 | |
Joel Becker | 4670c46 | 2008-02-01 14:39:35 -0800 | [diff] [blame^] | 210 | int ocfs2_dlm_lock(struct ocfs2_cluster_connection *conn, |
Joel Becker | 24ef181 | 2008-01-29 17:37:32 -0800 | [diff] [blame] | 211 | int mode, |
Joel Becker | 8f2c9c1 | 2008-02-01 12:16:57 -0800 | [diff] [blame] | 212 | union ocfs2_dlm_lksb *lksb, |
Joel Becker | 24ef181 | 2008-01-29 17:37:32 -0800 | [diff] [blame] | 213 | u32 flags, |
| 214 | void *name, |
| 215 | unsigned int namelen, |
| 216 | void *astarg) |
| 217 | { |
Joel Becker | 7431cd7 | 2008-02-01 12:15:37 -0800 | [diff] [blame] | 218 | enum dlm_status status; |
Joel Becker | bd3e761 | 2008-02-01 12:14:57 -0800 | [diff] [blame] | 219 | int o2dlm_mode = mode_to_o2dlm(mode); |
| 220 | int o2dlm_flags = flags_to_o2dlm(flags); |
Joel Becker | 7431cd7 | 2008-02-01 12:15:37 -0800 | [diff] [blame] | 221 | int ret; |
Joel Becker | bd3e761 | 2008-02-01 12:14:57 -0800 | [diff] [blame] | 222 | |
Joel Becker | 24ef181 | 2008-01-29 17:37:32 -0800 | [diff] [blame] | 223 | BUG_ON(lproto == NULL); |
Joel Becker | bd3e761 | 2008-02-01 12:14:57 -0800 | [diff] [blame] | 224 | |
Joel Becker | 4670c46 | 2008-02-01 14:39:35 -0800 | [diff] [blame^] | 225 | status = dlmlock(conn->cc_lockspace, o2dlm_mode, &lksb->lksb_o2dlm, |
| 226 | o2dlm_flags, name, namelen, |
Joel Becker | 8f2c9c1 | 2008-02-01 12:16:57 -0800 | [diff] [blame] | 227 | o2dlm_lock_ast_wrapper, astarg, |
| 228 | o2dlm_blocking_ast_wrapper); |
Joel Becker | 7431cd7 | 2008-02-01 12:15:37 -0800 | [diff] [blame] | 229 | ret = dlm_status_to_errno(status); |
| 230 | return ret; |
Joel Becker | 24ef181 | 2008-01-29 17:37:32 -0800 | [diff] [blame] | 231 | } |
| 232 | |
Joel Becker | 4670c46 | 2008-02-01 14:39:35 -0800 | [diff] [blame^] | 233 | int ocfs2_dlm_unlock(struct ocfs2_cluster_connection *conn, |
Joel Becker | 8f2c9c1 | 2008-02-01 12:16:57 -0800 | [diff] [blame] | 234 | union ocfs2_dlm_lksb *lksb, |
Joel Becker | 24ef181 | 2008-01-29 17:37:32 -0800 | [diff] [blame] | 235 | u32 flags, |
| 236 | void *astarg) |
| 237 | { |
Joel Becker | 7431cd7 | 2008-02-01 12:15:37 -0800 | [diff] [blame] | 238 | enum dlm_status status; |
Joel Becker | bd3e761 | 2008-02-01 12:14:57 -0800 | [diff] [blame] | 239 | int o2dlm_flags = flags_to_o2dlm(flags); |
Joel Becker | 7431cd7 | 2008-02-01 12:15:37 -0800 | [diff] [blame] | 240 | int ret; |
Joel Becker | bd3e761 | 2008-02-01 12:14:57 -0800 | [diff] [blame] | 241 | |
Joel Becker | 24ef181 | 2008-01-29 17:37:32 -0800 | [diff] [blame] | 242 | BUG_ON(lproto == NULL); |
| 243 | |
Joel Becker | 4670c46 | 2008-02-01 14:39:35 -0800 | [diff] [blame^] | 244 | status = dlmunlock(conn->cc_lockspace, &lksb->lksb_o2dlm, |
| 245 | o2dlm_flags, o2dlm_unlock_ast_wrapper, astarg); |
Joel Becker | 7431cd7 | 2008-02-01 12:15:37 -0800 | [diff] [blame] | 246 | ret = dlm_status_to_errno(status); |
| 247 | return ret; |
Joel Becker | 24ef181 | 2008-01-29 17:37:32 -0800 | [diff] [blame] | 248 | } |
| 249 | |
Joel Becker | 8f2c9c1 | 2008-02-01 12:16:57 -0800 | [diff] [blame] | 250 | int ocfs2_dlm_lock_status(union ocfs2_dlm_lksb *lksb) |
| 251 | { |
| 252 | return dlm_status_to_errno(lksb->lksb_o2dlm.status); |
| 253 | } |
| 254 | |
| 255 | /* |
| 256 | * Why don't we cast to ocfs2_meta_lvb? The "clean" answer is that we |
| 257 | * don't cast at the glue level. The real answer is that the header |
| 258 | * ordering is nigh impossible. |
| 259 | */ |
| 260 | void *ocfs2_dlm_lvb(union ocfs2_dlm_lksb *lksb) |
| 261 | { |
| 262 | return (void *)(lksb->lksb_o2dlm.lvb); |
| 263 | } |
Joel Becker | 24ef181 | 2008-01-29 17:37:32 -0800 | [diff] [blame] | 264 | |
Joel Becker | 4670c46 | 2008-02-01 14:39:35 -0800 | [diff] [blame^] | 265 | /* |
| 266 | * Called from the dlm when it's about to evict a node. This is how the |
| 267 | * classic stack signals node death. |
| 268 | */ |
| 269 | static void o2dlm_eviction_cb(int node_num, void *data) |
| 270 | { |
| 271 | struct ocfs2_cluster_connection *conn = data; |
| 272 | |
| 273 | mlog(ML_NOTICE, "o2dlm has evicted node %d from group %.*s\n", |
| 274 | node_num, conn->cc_namelen, conn->cc_name); |
| 275 | |
| 276 | conn->cc_recovery_handler(node_num, conn->cc_recovery_data); |
| 277 | } |
| 278 | |
| 279 | int ocfs2_cluster_connect(const char *group, |
| 280 | int grouplen, |
| 281 | void (*recovery_handler)(int node_num, |
| 282 | void *recovery_data), |
| 283 | void *recovery_data, |
| 284 | struct ocfs2_cluster_connection **conn) |
| 285 | { |
| 286 | int rc = 0; |
| 287 | struct ocfs2_cluster_connection *new_conn; |
| 288 | u32 dlm_key; |
| 289 | struct dlm_ctxt *dlm; |
| 290 | struct o2dlm_private *priv; |
| 291 | struct dlm_protocol_version dlm_version; |
| 292 | |
| 293 | BUG_ON(group == NULL); |
| 294 | BUG_ON(conn == NULL); |
| 295 | BUG_ON(recovery_handler == NULL); |
| 296 | |
| 297 | if (grouplen > GROUP_NAME_MAX) { |
| 298 | rc = -EINVAL; |
| 299 | goto out; |
| 300 | } |
| 301 | |
| 302 | new_conn = kzalloc(sizeof(struct ocfs2_cluster_connection), |
| 303 | GFP_KERNEL); |
| 304 | if (!new_conn) { |
| 305 | rc = -ENOMEM; |
| 306 | goto out; |
| 307 | } |
| 308 | |
| 309 | memcpy(new_conn->cc_name, group, grouplen); |
| 310 | new_conn->cc_namelen = grouplen; |
| 311 | new_conn->cc_recovery_handler = recovery_handler; |
| 312 | new_conn->cc_recovery_data = recovery_data; |
| 313 | |
| 314 | /* Start the new connection at our maximum compatibility level */ |
| 315 | new_conn->cc_version = lproto->lp_max_version; |
| 316 | |
| 317 | priv = kzalloc(sizeof(struct o2dlm_private), GFP_KERNEL); |
| 318 | if (!priv) { |
| 319 | rc = -ENOMEM; |
| 320 | goto out_free; |
| 321 | } |
| 322 | |
| 323 | /* This just fills the structure in. It is safe to use new_conn. */ |
| 324 | dlm_setup_eviction_cb(&priv->op_eviction_cb, o2dlm_eviction_cb, |
| 325 | new_conn); |
| 326 | |
| 327 | new_conn->cc_private = priv; |
| 328 | |
| 329 | /* used by the dlm code to make message headers unique, each |
| 330 | * node in this domain must agree on this. */ |
| 331 | dlm_key = crc32_le(0, group, grouplen); |
| 332 | dlm_version.pv_major = new_conn->cc_version.pv_major; |
| 333 | dlm_version.pv_minor = new_conn->cc_version.pv_minor; |
| 334 | |
| 335 | dlm = dlm_register_domain(group, dlm_key, &dlm_version); |
| 336 | if (IS_ERR(dlm)) { |
| 337 | rc = PTR_ERR(dlm); |
| 338 | mlog_errno(rc); |
| 339 | goto out_free; |
| 340 | } |
| 341 | |
| 342 | new_conn->cc_version.pv_major = dlm_version.pv_major; |
| 343 | new_conn->cc_version.pv_minor = dlm_version.pv_minor; |
| 344 | new_conn->cc_lockspace = dlm; |
| 345 | |
| 346 | dlm_register_eviction_cb(dlm, &priv->op_eviction_cb); |
| 347 | |
| 348 | *conn = new_conn; |
| 349 | |
| 350 | out_free: |
| 351 | if (rc) { |
| 352 | kfree(new_conn->cc_private); |
| 353 | kfree(new_conn); |
| 354 | } |
| 355 | |
| 356 | out: |
| 357 | return rc; |
| 358 | } |
| 359 | |
| 360 | int ocfs2_cluster_disconnect(struct ocfs2_cluster_connection *conn) |
| 361 | { |
| 362 | struct dlm_ctxt *dlm = conn->cc_lockspace; |
| 363 | struct o2dlm_private *priv = conn->cc_private; |
| 364 | |
| 365 | dlm_unregister_eviction_cb(&priv->op_eviction_cb); |
| 366 | dlm_unregister_domain(dlm); |
| 367 | |
| 368 | kfree(priv); |
| 369 | kfree(conn); |
| 370 | |
| 371 | return 0; |
| 372 | } |
| 373 | |
Joel Becker | 24ef181 | 2008-01-29 17:37:32 -0800 | [diff] [blame] | 374 | void o2cb_get_stack(struct ocfs2_locking_protocol *proto) |
| 375 | { |
| 376 | BUG_ON(proto == NULL); |
| 377 | |
| 378 | lproto = proto; |
| 379 | } |
| 380 | |
| 381 | void o2cb_put_stack(void) |
| 382 | { |
| 383 | lproto = NULL; |
| 384 | } |