1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Code which implements an OCFS2 specific interface to underlying
9 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
21 #include <linux/slab.h>
22 #include <linux/crc32.h>
23 #include <linux/kmod.h>
25 /* Needed for AOP_TRUNCATED_PAGE in mlog_errno() */
28 #include "cluster/masklog.h"
29 #include "cluster/nodemanager.h"
30 #include "cluster/heartbeat.h"
32 #include "stackglue.h"
34 static struct ocfs2_locking_protocol *lproto;
36 struct o2dlm_private {
37 struct dlm_eviction_cb op_eviction_cb;
40 /* These should be identical */
41 #if (DLM_LOCK_IV != LKM_IVMODE)
42 # error Lock modes do not match
44 #if (DLM_LOCK_NL != LKM_NLMODE)
45 # error Lock modes do not match
47 #if (DLM_LOCK_CR != LKM_CRMODE)
48 # error Lock modes do not match
50 #if (DLM_LOCK_CW != LKM_CWMODE)
51 # error Lock modes do not match
53 #if (DLM_LOCK_PR != LKM_PRMODE)
54 # error Lock modes do not match
56 #if (DLM_LOCK_PW != LKM_PWMODE)
57 # error Lock modes do not match
59 #if (DLM_LOCK_EX != LKM_EXMODE)
60 # error Lock modes do not match
62 static inline int mode_to_o2dlm(int mode)
64 BUG_ON(mode > LKM_MAXMODE);
69 #define map_flag(_generic, _o2dlm) \
70 if (flags & (_generic)) { \
71 flags &= ~(_generic); \
72 o2dlm_flags |= (_o2dlm); \
74 static int flags_to_o2dlm(u32 flags)
78 map_flag(DLM_LKF_NOQUEUE, LKM_NOQUEUE);
79 map_flag(DLM_LKF_CANCEL, LKM_CANCEL);
80 map_flag(DLM_LKF_CONVERT, LKM_CONVERT);
81 map_flag(DLM_LKF_VALBLK, LKM_VALBLK);
82 map_flag(DLM_LKF_IVVALBLK, LKM_INVVALBLK);
83 map_flag(DLM_LKF_ORPHAN, LKM_ORPHAN);
84 map_flag(DLM_LKF_FORCEUNLOCK, LKM_FORCE);
85 map_flag(DLM_LKF_TIMEOUT, LKM_TIMEOUT);
86 map_flag(DLM_LKF_LOCAL, LKM_LOCAL);
88 /* map_flag() should have cleared every flag passed in */
96 * Map an o2dlm status to standard errno values.
98 * o2dlm only uses a handful of these, and returns even fewer to the
99 * caller. Still, we try to assign sane values to each error.
101 * The following value pairs have special meanings to dlmglue, thus
102 * the right hand side needs to stay unique - never duplicate the
103 * mapping elsewhere in the table!
106 * DLM_NOTQUEUED: -EAGAIN
107 * DLM_CANCELGRANT: -DLM_ECANCEL
108 * DLM_CANCEL: -DLM_EUNLOCK
110 /* Keep in sync with dlmapi.h */
111 static int status_map[] = {
112 [DLM_NORMAL] = 0, /* Success */
113 [DLM_GRANTED] = -EINVAL,
114 [DLM_DENIED] = -EACCES,
115 [DLM_DENIED_NOLOCKS] = -EACCES,
116 [DLM_WORKING] = -EBUSY,
117 [DLM_BLOCKED] = -EINVAL,
118 [DLM_BLOCKED_ORPHAN] = -EINVAL,
119 [DLM_DENIED_GRACE_PERIOD] = -EACCES,
120 [DLM_SYSERR] = -ENOMEM, /* It is what it is */
121 [DLM_NOSUPPORT] = -EPROTO,
122 [DLM_CANCELGRANT] = -DLM_ECANCEL, /* Cancel after grant */
123 [DLM_IVLOCKID] = -EINVAL,
124 [DLM_SYNC] = -EINVAL,
125 [DLM_BADTYPE] = -EINVAL,
126 [DLM_BADRESOURCE] = -EINVAL,
127 [DLM_MAXHANDLES] = -ENOMEM,
128 [DLM_NOCLINFO] = -EINVAL,
129 [DLM_NOLOCKMGR] = -EINVAL,
130 [DLM_NOPURGED] = -EINVAL,
131 [DLM_BADARGS] = -EINVAL,
132 [DLM_VOID] = -EINVAL,
133 [DLM_NOTQUEUED] = -EAGAIN, /* Trylock failed */
134 [DLM_IVBUFLEN] = -EINVAL,
135 [DLM_CVTUNGRANT] = -EPERM,
136 [DLM_BADPARAM] = -EINVAL,
137 [DLM_VALNOTVALID] = -EINVAL,
138 [DLM_REJECTED] = -EPERM,
139 [DLM_ABORT] = -EINVAL,
140 [DLM_CANCEL] = -DLM_EUNLOCK, /* Successful cancel */
141 [DLM_IVRESHANDLE] = -EINVAL,
142 [DLM_DEADLOCK] = -EDEADLK,
143 [DLM_DENIED_NOASTS] = -EINVAL,
144 [DLM_FORWARD] = -EINVAL,
145 [DLM_TIMEOUT] = -ETIMEDOUT,
146 [DLM_IVGROUPID] = -EINVAL,
147 [DLM_VERS_CONFLICT] = -EOPNOTSUPP,
148 [DLM_BAD_DEVICE_PATH] = -ENOENT,
149 [DLM_NO_DEVICE_PERMISSION] = -EPERM,
150 [DLM_NO_CONTROL_DEVICE] = -ENOENT,
151 [DLM_RECOVERING] = -ENOTCONN,
152 [DLM_MIGRATING] = -ERESTART,
153 [DLM_MAXSTATS] = -EINVAL,
155 static int dlm_status_to_errno(enum dlm_status status)
157 BUG_ON(status > (sizeof(status_map) / sizeof(status_map[0])));
159 return status_map[status];
162 static void o2dlm_lock_ast_wrapper(void *astarg)
164 BUG_ON(lproto == NULL);
166 lproto->lp_lock_ast(astarg);
169 static void o2dlm_blocking_ast_wrapper(void *astarg, int level)
171 BUG_ON(lproto == NULL);
173 lproto->lp_blocking_ast(astarg, level);
176 static void o2dlm_unlock_ast_wrapper(void *astarg, enum dlm_status status)
180 BUG_ON(lproto == NULL);
183 * XXX: CANCEL values are sketchy.
185 * Currently we have preserved the o2dlm paradigm. You can get
186 * unlock_ast() whether the cancel succeded or not.
188 * First, we're going to pass DLM_EUNLOCK just like fs/dlm does for
189 * successful unlocks. That is a clean behavior.
191 * In o2dlm, you can get both the lock_ast() for the lock being
192 * granted and the unlock_ast() for the CANCEL failing. A
193 * successful cancel sends DLM_NORMAL here. If the
194 * lock grant happened before the cancel arrived, you get
195 * DLM_CANCELGRANT. For now, we'll use DLM_ECANCEL to signify
196 * CANCELGRANT - the CANCEL was supposed to happen but didn't. We
197 * can then use DLM_EUNLOCK to signify a successful CANCEL -
198 * effectively, the CANCEL caused the lock to roll back.
200 * In the future, we will likely move the o2dlm to send only one
201 * ast - either unlock_ast() for a successful CANCEL or lock_ast()
202 * when the grant succeeds. At that point, we'll send DLM_ECANCEL
203 * for all cancel results (CANCELGRANT will no longer exist).
205 error = dlm_status_to_errno(status);
207 /* Successful unlock is DLM_EUNLOCK */
209 error = -DLM_EUNLOCK;
211 lproto->lp_unlock_ast(astarg, error);
214 int ocfs2_dlm_lock(struct ocfs2_cluster_connection *conn,
216 union ocfs2_dlm_lksb *lksb,
219 unsigned int namelen,
222 enum dlm_status status;
223 int o2dlm_mode = mode_to_o2dlm(mode);
224 int o2dlm_flags = flags_to_o2dlm(flags);
227 BUG_ON(lproto == NULL);
229 status = dlmlock(conn->cc_lockspace, o2dlm_mode, &lksb->lksb_o2dlm,
230 o2dlm_flags, name, namelen,
231 o2dlm_lock_ast_wrapper, astarg,
232 o2dlm_blocking_ast_wrapper);
233 ret = dlm_status_to_errno(status);
237 int ocfs2_dlm_unlock(struct ocfs2_cluster_connection *conn,
238 union ocfs2_dlm_lksb *lksb,
242 enum dlm_status status;
243 int o2dlm_flags = flags_to_o2dlm(flags);
246 BUG_ON(lproto == NULL);
248 status = dlmunlock(conn->cc_lockspace, &lksb->lksb_o2dlm,
249 o2dlm_flags, o2dlm_unlock_ast_wrapper, astarg);
250 ret = dlm_status_to_errno(status);
254 int ocfs2_dlm_lock_status(union ocfs2_dlm_lksb *lksb)
256 return dlm_status_to_errno(lksb->lksb_o2dlm.status);
260 * Why don't we cast to ocfs2_meta_lvb? The "clean" answer is that we
261 * don't cast at the glue level. The real answer is that the header
262 * ordering is nigh impossible.
264 void *ocfs2_dlm_lvb(union ocfs2_dlm_lksb *lksb)
266 return (void *)(lksb->lksb_o2dlm.lvb);
270 * Called from the dlm when it's about to evict a node. This is how the
271 * classic stack signals node death.
273 static void o2dlm_eviction_cb(int node_num, void *data)
275 struct ocfs2_cluster_connection *conn = data;
277 mlog(ML_NOTICE, "o2dlm has evicted node %d from group %.*s\n",
278 node_num, conn->cc_namelen, conn->cc_name);
280 conn->cc_recovery_handler(node_num, conn->cc_recovery_data);
283 int ocfs2_cluster_connect(const char *group,
285 void (*recovery_handler)(int node_num,
286 void *recovery_data),
288 struct ocfs2_cluster_connection **conn)
291 struct ocfs2_cluster_connection *new_conn;
293 struct dlm_ctxt *dlm;
294 struct o2dlm_private *priv;
295 struct dlm_protocol_version dlm_version;
297 BUG_ON(group == NULL);
298 BUG_ON(conn == NULL);
299 BUG_ON(recovery_handler == NULL);
301 if (grouplen > GROUP_NAME_MAX) {
306 /* for now we only have one cluster/node, make sure we see it
307 * in the heartbeat universe */
308 if (!o2hb_check_local_node_heartbeating()) {
313 new_conn = kzalloc(sizeof(struct ocfs2_cluster_connection),
320 memcpy(new_conn->cc_name, group, grouplen);
321 new_conn->cc_namelen = grouplen;
322 new_conn->cc_recovery_handler = recovery_handler;
323 new_conn->cc_recovery_data = recovery_data;
325 /* Start the new connection at our maximum compatibility level */
326 new_conn->cc_version = lproto->lp_max_version;
328 priv = kzalloc(sizeof(struct o2dlm_private), GFP_KERNEL);
334 /* This just fills the structure in. It is safe to use new_conn. */
335 dlm_setup_eviction_cb(&priv->op_eviction_cb, o2dlm_eviction_cb,
338 new_conn->cc_private = priv;
340 /* used by the dlm code to make message headers unique, each
341 * node in this domain must agree on this. */
342 dlm_key = crc32_le(0, group, grouplen);
343 dlm_version.pv_major = new_conn->cc_version.pv_major;
344 dlm_version.pv_minor = new_conn->cc_version.pv_minor;
346 dlm = dlm_register_domain(group, dlm_key, &dlm_version);
353 new_conn->cc_version.pv_major = dlm_version.pv_major;
354 new_conn->cc_version.pv_minor = dlm_version.pv_minor;
355 new_conn->cc_lockspace = dlm;
357 dlm_register_eviction_cb(dlm, &priv->op_eviction_cb);
363 kfree(new_conn->cc_private);
372 int ocfs2_cluster_disconnect(struct ocfs2_cluster_connection *conn)
374 struct dlm_ctxt *dlm = conn->cc_lockspace;
375 struct o2dlm_private *priv = conn->cc_private;
377 dlm_unregister_eviction_cb(&priv->op_eviction_cb);
378 dlm_unregister_domain(dlm);
386 static void o2hb_stop(const char *group)
389 char *argv[5], *envp[3];
391 argv[0] = (char *)o2nm_get_hb_ctl_path();
394 argv[3] = (char *)group;
397 mlog(0, "Run: %s %s %s %s\n", argv[0], argv[1], argv[2], argv[3]);
399 /* minimal command environment taken from cpu_run_sbin_hotplug */
401 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
404 ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
410 * Hangup is a hack for tools compatibility. Older ocfs2-tools software
411 * expects the filesystem to call "ocfs2_hb_ctl" during unmount. This
412 * happens regardless of whether the DLM got started, so we can't do it
413 * in ocfs2_cluster_disconnect(). We bring the o2hb_stop() function into
414 * the glue and provide a "hangup" API for super.c to call.
416 * Other stacks will eventually provide a NULL ->hangup() pointer.
418 void ocfs2_cluster_hangup(const char *group, int grouplen)
420 BUG_ON(group == NULL);
421 BUG_ON(group[grouplen] != '\0');
426 int ocfs2_cluster_this_node(unsigned int *node)
430 node_num = o2nm_this_node();
431 if (node_num == O2NM_INVALID_NODE_NUM)
434 if (node_num >= O2NM_MAX_NODES)
441 void o2cb_get_stack(struct ocfs2_locking_protocol *proto)
443 BUG_ON(proto == NULL);
448 void o2cb_put_stack(void)