1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Code which interfaces ocfs2 with the o2cb stack.
8 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
20 #include <linux/crc32.h>
21 #include <linux/module.h>
23 /* Needed for AOP_TRUNCATED_PAGE in mlog_errno() */
26 #include "cluster/masklog.h"
27 #include "cluster/nodemanager.h"
28 #include "cluster/heartbeat.h"
30 #include "stackglue.h"
32 struct o2dlm_private {
33 struct dlm_eviction_cb op_eviction_cb;
36 static struct ocfs2_stack_plugin o2cb_stack;
38 /* These should be identical */
39 #if (DLM_LOCK_IV != LKM_IVMODE)
40 # error Lock modes do not match
42 #if (DLM_LOCK_NL != LKM_NLMODE)
43 # error Lock modes do not match
45 #if (DLM_LOCK_CR != LKM_CRMODE)
46 # error Lock modes do not match
48 #if (DLM_LOCK_CW != LKM_CWMODE)
49 # error Lock modes do not match
51 #if (DLM_LOCK_PR != LKM_PRMODE)
52 # error Lock modes do not match
54 #if (DLM_LOCK_PW != LKM_PWMODE)
55 # error Lock modes do not match
57 #if (DLM_LOCK_EX != LKM_EXMODE)
58 # error Lock modes do not match
60 static inline int mode_to_o2dlm(int mode)
62 BUG_ON(mode > LKM_MAXMODE);
67 #define map_flag(_generic, _o2dlm) \
68 if (flags & (_generic)) { \
69 flags &= ~(_generic); \
70 o2dlm_flags |= (_o2dlm); \
72 static int flags_to_o2dlm(u32 flags)
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);
86 /* map_flag() should have cleared every flag passed in */
94 * Map an o2dlm status to standard errno values.
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.
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!
104 * DLM_NOTQUEUED: -EAGAIN
105 * DLM_CANCELGRANT: -EBUSY
106 * DLM_CANCEL: -DLM_ECANCEL
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,
154 static int dlm_status_to_errno(enum dlm_status status)
156 BUG_ON(status > (sizeof(status_map) / sizeof(status_map[0])));
158 return status_map[status];
161 static void o2dlm_lock_ast_wrapper(void *astarg)
163 BUG_ON(o2cb_stack.sp_proto == NULL);
165 o2cb_stack.sp_proto->lp_lock_ast(astarg);
168 static void o2dlm_blocking_ast_wrapper(void *astarg, int level)
170 BUG_ON(o2cb_stack.sp_proto == NULL);
172 o2cb_stack.sp_proto->lp_blocking_ast(astarg, level);
175 static void o2dlm_unlock_ast_wrapper(void *astarg, enum dlm_status status)
177 int error = dlm_status_to_errno(status);
179 BUG_ON(o2cb_stack.sp_proto == NULL);
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
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
192 if (status == DLM_CANCELGRANT)
195 o2cb_stack.sp_proto->lp_unlock_ast(astarg, error);
198 static int o2cb_dlm_lock(struct ocfs2_cluster_connection *conn,
200 union ocfs2_dlm_lksb *lksb,
203 unsigned int namelen,
206 enum dlm_status status;
207 int o2dlm_mode = mode_to_o2dlm(mode);
208 int o2dlm_flags = flags_to_o2dlm(flags);
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);
219 static int o2cb_dlm_unlock(struct ocfs2_cluster_connection *conn,
220 union ocfs2_dlm_lksb *lksb,
224 enum dlm_status status;
225 int o2dlm_flags = flags_to_o2dlm(flags);
228 status = dlmunlock(conn->cc_lockspace, &lksb->lksb_o2dlm,
229 o2dlm_flags, o2dlm_unlock_ast_wrapper, astarg);
230 ret = dlm_status_to_errno(status);
234 static int o2cb_dlm_lock_status(union ocfs2_dlm_lksb *lksb)
236 return dlm_status_to_errno(lksb->lksb_o2dlm.status);
239 static void *o2cb_dlm_lvb(union ocfs2_dlm_lksb *lksb)
241 return (void *)(lksb->lksb_o2dlm.lvb);
244 static void o2cb_dump_lksb(union ocfs2_dlm_lksb *lksb)
246 dlm_print_one_lock(lksb->lksb_o2dlm.lockid);
250 * Called from the dlm when it's about to evict a node. This is how the
251 * classic stack signals node death.
253 static void o2dlm_eviction_cb(int node_num, void *data)
255 struct ocfs2_cluster_connection *conn = data;
257 mlog(ML_NOTICE, "o2dlm has evicted node %d from group %.*s\n",
258 node_num, conn->cc_namelen, conn->cc_name);
260 conn->cc_recovery_handler(node_num, conn->cc_recovery_data);
263 static int o2cb_cluster_connect(struct ocfs2_cluster_connection *conn)
267 struct dlm_ctxt *dlm;
268 struct o2dlm_private *priv;
269 struct dlm_protocol_version dlm_version;
271 BUG_ON(conn == NULL);
272 BUG_ON(o2cb_stack.sp_proto == NULL);
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()) {
281 priv = kzalloc(sizeof(struct o2dlm_private), GFP_KERNEL);
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,
291 conn->cc_private = priv;
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;
299 dlm = dlm_register_domain(conn->cc_name, dlm_key, &dlm_version);
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;
310 dlm_register_eviction_cb(dlm, &priv->op_eviction_cb);
313 if (rc && conn->cc_private)
314 kfree(conn->cc_private);
320 static int o2cb_cluster_disconnect(struct ocfs2_cluster_connection *conn,
323 struct dlm_ctxt *dlm = conn->cc_lockspace;
324 struct o2dlm_private *priv = conn->cc_private;
326 dlm_unregister_eviction_cb(&priv->op_eviction_cb);
327 conn->cc_private = NULL;
330 dlm_unregister_domain(dlm);
331 conn->cc_lockspace = NULL;
336 static void o2hb_stop(const char *group)
339 char *argv[5], *envp[3];
341 argv[0] = (char *)o2nm_get_hb_ctl_path();
344 argv[3] = (char *)group;
347 mlog(0, "Run: %s %s %s %s\n", argv[0], argv[1], argv[2], argv[3]);
349 /* minimal command environment taken from cpu_run_sbin_hotplug */
351 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
354 ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
360 * Hangup is a hack for tools compatibility. Older ocfs2-tools software
361 * expects the filesystem to call "ocfs2_hb_ctl" during unmount. This
362 * happens regardless of whether the DLM got started, so we can't do it
363 * in ocfs2_cluster_disconnect(). We bring the o2hb_stop() function into
364 * the glue and provide a "hangup" API for super.c to call.
366 * Other stacks will eventually provide a NULL ->hangup() pointer.
368 static void o2cb_cluster_hangup(const char *group, int grouplen)
373 static int o2cb_cluster_this_node(unsigned int *node)
377 node_num = o2nm_this_node();
378 if (node_num == O2NM_INVALID_NODE_NUM)
381 if (node_num >= O2NM_MAX_NODES)
388 static struct ocfs2_stack_operations o2cb_stack_ops = {
389 .connect = o2cb_cluster_connect,
390 .disconnect = o2cb_cluster_disconnect,
391 .hangup = o2cb_cluster_hangup,
392 .this_node = o2cb_cluster_this_node,
393 .dlm_lock = o2cb_dlm_lock,
394 .dlm_unlock = o2cb_dlm_unlock,
395 .lock_status = o2cb_dlm_lock_status,
396 .lock_lvb = o2cb_dlm_lvb,
397 .dump_lksb = o2cb_dump_lksb,
400 static struct ocfs2_stack_plugin o2cb_stack = {
402 .sp_ops = &o2cb_stack_ops,
403 .sp_owner = THIS_MODULE,
406 static int __init o2cb_stack_init(void)
408 return ocfs2_stack_glue_register(&o2cb_stack);
411 static void __exit o2cb_stack_exit(void)
413 ocfs2_stack_glue_unregister(&o2cb_stack);
416 MODULE_AUTHOR("Oracle");
417 MODULE_DESCRIPTION("ocfs2 driver for the classic o2cb stack");
418 MODULE_LICENSE("GPL");
419 module_init(o2cb_stack_init);
420 module_exit(o2cb_stack_exit);