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> |
| 21 | #include <linux/kmod.h> |
| 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 | |
| 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 | |
| 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: -EBUSY |
| 104 | * DLM_CANCEL: -DLM_ECANCEL |
| 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] = -EACCES, |
| 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] = -EBUSY, /* 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_ECANCEL, /* 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 | |
| 152 | static int dlm_status_to_errno(enum dlm_status status) |
| 153 | { |
| 154 | BUG_ON(status > (sizeof(status_map) / sizeof(status_map[0]))); |
| 155 | |
| 156 | return status_map[status]; |
| 157 | } |
| 158 | |
| 159 | static void o2dlm_lock_ast_wrapper(void *astarg) |
| 160 | { |
| 161 | BUG_ON(stack_glue_lproto == NULL); |
| 162 | |
| 163 | stack_glue_lproto->lp_lock_ast(astarg); |
| 164 | } |
| 165 | |
| 166 | static void o2dlm_blocking_ast_wrapper(void *astarg, int level) |
| 167 | { |
| 168 | BUG_ON(stack_glue_lproto == NULL); |
| 169 | |
| 170 | stack_glue_lproto->lp_blocking_ast(astarg, level); |
| 171 | } |
| 172 | |
| 173 | static void o2dlm_unlock_ast_wrapper(void *astarg, enum dlm_status status) |
| 174 | { |
| 175 | int error = dlm_status_to_errno(status); |
| 176 | |
| 177 | BUG_ON(stack_glue_lproto == NULL); |
| 178 | |
| 179 | /* |
| 180 | * In o2dlm, you can get both the lock_ast() for the lock being |
| 181 | * granted and the unlock_ast() for the CANCEL failing. A |
| 182 | * successful cancel sends DLM_NORMAL here. If the |
| 183 | * lock grant happened before the cancel arrived, you get |
| 184 | * DLM_CANCELGRANT. |
| 185 | * |
| 186 | * There's no need for the double-ast. If we see DLM_CANCELGRANT, |
| 187 | * we just ignore it. We expect the lock_ast() to handle the |
| 188 | * granted lock. |
| 189 | */ |
| 190 | if (status == DLM_CANCELGRANT) |
| 191 | return; |
| 192 | |
| 193 | stack_glue_lproto->lp_unlock_ast(astarg, error); |
| 194 | } |
| 195 | |
| 196 | static int o2cb_dlm_lock(struct ocfs2_cluster_connection *conn, |
| 197 | int mode, |
| 198 | union ocfs2_dlm_lksb *lksb, |
| 199 | u32 flags, |
| 200 | void *name, |
| 201 | unsigned int namelen, |
| 202 | void *astarg) |
| 203 | { |
| 204 | enum dlm_status status; |
| 205 | int o2dlm_mode = mode_to_o2dlm(mode); |
| 206 | int o2dlm_flags = flags_to_o2dlm(flags); |
| 207 | int ret; |
| 208 | |
| 209 | status = dlmlock(conn->cc_lockspace, o2dlm_mode, &lksb->lksb_o2dlm, |
| 210 | o2dlm_flags, name, namelen, |
| 211 | o2dlm_lock_ast_wrapper, astarg, |
| 212 | o2dlm_blocking_ast_wrapper); |
| 213 | ret = dlm_status_to_errno(status); |
| 214 | return ret; |
| 215 | } |
| 216 | |
| 217 | static int o2cb_dlm_unlock(struct ocfs2_cluster_connection *conn, |
| 218 | union ocfs2_dlm_lksb *lksb, |
| 219 | u32 flags, |
| 220 | void *astarg) |
| 221 | { |
| 222 | enum dlm_status status; |
| 223 | int o2dlm_flags = flags_to_o2dlm(flags); |
| 224 | int ret; |
| 225 | |
| 226 | status = dlmunlock(conn->cc_lockspace, &lksb->lksb_o2dlm, |
| 227 | o2dlm_flags, o2dlm_unlock_ast_wrapper, astarg); |
| 228 | ret = dlm_status_to_errno(status); |
| 229 | return ret; |
| 230 | } |
| 231 | |
| 232 | static int o2cb_dlm_lock_status(union ocfs2_dlm_lksb *lksb) |
| 233 | { |
| 234 | return dlm_status_to_errno(lksb->lksb_o2dlm.status); |
| 235 | } |
| 236 | |
| 237 | static void *o2cb_dlm_lvb(union ocfs2_dlm_lksb *lksb) |
| 238 | { |
| 239 | return (void *)(lksb->lksb_o2dlm.lvb); |
| 240 | } |
| 241 | |
| 242 | static void o2cb_dump_lksb(union ocfs2_dlm_lksb *lksb) |
| 243 | { |
| 244 | dlm_print_one_lock(lksb->lksb_o2dlm.lockid); |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * Called from the dlm when it's about to evict a node. This is how the |
| 249 | * classic stack signals node death. |
| 250 | */ |
| 251 | static void o2dlm_eviction_cb(int node_num, void *data) |
| 252 | { |
| 253 | struct ocfs2_cluster_connection *conn = data; |
| 254 | |
| 255 | mlog(ML_NOTICE, "o2dlm has evicted node %d from group %.*s\n", |
| 256 | node_num, conn->cc_namelen, conn->cc_name); |
| 257 | |
| 258 | conn->cc_recovery_handler(node_num, conn->cc_recovery_data); |
| 259 | } |
| 260 | |
| 261 | static int o2cb_cluster_connect(struct ocfs2_cluster_connection *conn) |
| 262 | { |
| 263 | int rc = 0; |
| 264 | u32 dlm_key; |
| 265 | struct dlm_ctxt *dlm; |
| 266 | struct o2dlm_private *priv; |
| 267 | struct dlm_protocol_version dlm_version; |
| 268 | |
| 269 | BUG_ON(conn == NULL); |
| 270 | |
| 271 | /* for now we only have one cluster/node, make sure we see it |
| 272 | * in the heartbeat universe */ |
| 273 | if (!o2hb_check_local_node_heartbeating()) { |
| 274 | rc = -EINVAL; |
| 275 | goto out; |
| 276 | } |
| 277 | |
| 278 | priv = kzalloc(sizeof(struct o2dlm_private), GFP_KERNEL); |
| 279 | if (!priv) { |
| 280 | rc = -ENOMEM; |
| 281 | goto out_free; |
| 282 | } |
| 283 | |
| 284 | /* This just fills the structure in. It is safe to pass conn. */ |
| 285 | dlm_setup_eviction_cb(&priv->op_eviction_cb, o2dlm_eviction_cb, |
| 286 | conn); |
| 287 | |
| 288 | conn->cc_private = priv; |
| 289 | |
| 290 | /* used by the dlm code to make message headers unique, each |
| 291 | * node in this domain must agree on this. */ |
| 292 | dlm_key = crc32_le(0, conn->cc_name, conn->cc_namelen); |
| 293 | dlm_version.pv_major = conn->cc_version.pv_major; |
| 294 | dlm_version.pv_minor = conn->cc_version.pv_minor; |
| 295 | |
| 296 | dlm = dlm_register_domain(conn->cc_name, dlm_key, &dlm_version); |
| 297 | if (IS_ERR(dlm)) { |
| 298 | rc = PTR_ERR(dlm); |
| 299 | mlog_errno(rc); |
| 300 | goto out_free; |
| 301 | } |
| 302 | |
| 303 | conn->cc_version.pv_major = dlm_version.pv_major; |
| 304 | conn->cc_version.pv_minor = dlm_version.pv_minor; |
| 305 | conn->cc_lockspace = dlm; |
| 306 | |
| 307 | dlm_register_eviction_cb(dlm, &priv->op_eviction_cb); |
| 308 | |
| 309 | out_free: |
| 310 | if (rc && conn->cc_private) |
| 311 | kfree(conn->cc_private); |
| 312 | |
| 313 | out: |
| 314 | return rc; |
| 315 | } |
| 316 | |
| 317 | static int o2cb_cluster_disconnect(struct ocfs2_cluster_connection *conn) |
| 318 | { |
| 319 | struct dlm_ctxt *dlm = conn->cc_lockspace; |
| 320 | struct o2dlm_private *priv = conn->cc_private; |
| 321 | |
| 322 | dlm_unregister_eviction_cb(&priv->op_eviction_cb); |
| 323 | conn->cc_private = NULL; |
| 324 | kfree(priv); |
| 325 | |
| 326 | dlm_unregister_domain(dlm); |
| 327 | conn->cc_lockspace = NULL; |
| 328 | |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | static void o2hb_stop(const char *group) |
| 333 | { |
| 334 | int ret; |
| 335 | char *argv[5], *envp[3]; |
| 336 | |
| 337 | argv[0] = (char *)o2nm_get_hb_ctl_path(); |
| 338 | argv[1] = "-K"; |
| 339 | argv[2] = "-u"; |
| 340 | argv[3] = (char *)group; |
| 341 | argv[4] = NULL; |
| 342 | |
| 343 | mlog(0, "Run: %s %s %s %s\n", argv[0], argv[1], argv[2], argv[3]); |
| 344 | |
| 345 | /* minimal command environment taken from cpu_run_sbin_hotplug */ |
| 346 | envp[0] = "HOME=/"; |
| 347 | envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; |
| 348 | envp[2] = NULL; |
| 349 | |
| 350 | ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC); |
| 351 | if (ret < 0) |
| 352 | mlog_errno(ret); |
| 353 | } |
| 354 | |
| 355 | /* |
| 356 | * Hangup is a hack for tools compatibility. Older ocfs2-tools software |
| 357 | * expects the filesystem to call "ocfs2_hb_ctl" during unmount. This |
| 358 | * happens regardless of whether the DLM got started, so we can't do it |
| 359 | * in ocfs2_cluster_disconnect(). We bring the o2hb_stop() function into |
| 360 | * the glue and provide a "hangup" API for super.c to call. |
| 361 | * |
| 362 | * Other stacks will eventually provide a NULL ->hangup() pointer. |
| 363 | */ |
| 364 | static void o2cb_cluster_hangup(const char *group, int grouplen) |
| 365 | { |
| 366 | o2hb_stop(group); |
| 367 | } |
| 368 | |
| 369 | static int o2cb_cluster_this_node(unsigned int *node) |
| 370 | { |
| 371 | int node_num; |
| 372 | |
| 373 | node_num = o2nm_this_node(); |
| 374 | if (node_num == O2NM_INVALID_NODE_NUM) |
| 375 | return -ENOENT; |
| 376 | |
| 377 | if (node_num >= O2NM_MAX_NODES) |
| 378 | return -EOVERFLOW; |
| 379 | |
| 380 | *node = node_num; |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | struct ocfs2_stack_operations o2cb_stack_ops = { |
| 385 | .connect = o2cb_cluster_connect, |
| 386 | .disconnect = o2cb_cluster_disconnect, |
| 387 | .hangup = o2cb_cluster_hangup, |
| 388 | .this_node = o2cb_cluster_this_node, |
| 389 | .dlm_lock = o2cb_dlm_lock, |
| 390 | .dlm_unlock = o2cb_dlm_unlock, |
| 391 | .lock_status = o2cb_dlm_lock_status, |
| 392 | .lock_lvb = o2cb_dlm_lvb, |
| 393 | .dump_lksb = o2cb_dump_lksb, |
| 394 | }; |
| 395 | |