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>
24 /* Needed for AOP_TRUNCATED_PAGE in mlog_errno() */
27 #include "cluster/masklog.h"
28 #include "cluster/nodemanager.h"
30 #include "stackglue.h"
32 static struct ocfs2_locking_protocol *lproto;
34 struct o2dlm_private {
35 struct dlm_eviction_cb op_eviction_cb;
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: -DLM_ECANCEL
106 * DLM_CANCEL: -DLM_EUNLOCK
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] = -EBUSY,
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] = -DLM_ECANCEL, /* 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_EUNLOCK, /* 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,
153 static int dlm_status_to_errno(enum dlm_status status)
155 BUG_ON(status > (sizeof(status_map) / sizeof(status_map[0])));
157 return status_map[status];
160 static void o2dlm_lock_ast_wrapper(void *astarg)
162 BUG_ON(lproto == NULL);
164 lproto->lp_lock_ast(astarg);
167 static void o2dlm_blocking_ast_wrapper(void *astarg, int level)
169 BUG_ON(lproto == NULL);
171 lproto->lp_blocking_ast(astarg, level);
174 static void o2dlm_unlock_ast_wrapper(void *astarg, enum dlm_status status)
178 BUG_ON(lproto == NULL);
181 * XXX: CANCEL values are sketchy.
183 * Currently we have preserved the o2dlm paradigm. You can get
184 * unlock_ast() whether the cancel succeded or not.
186 * First, we're going to pass DLM_EUNLOCK just like fs/dlm does for
187 * successful unlocks. That is a clean behavior.
189 * In o2dlm, you can get both the lock_ast() for the lock being
190 * granted and the unlock_ast() for the CANCEL failing. A
191 * successful cancel sends DLM_NORMAL here. If the
192 * lock grant happened before the cancel arrived, you get
193 * DLM_CANCELGRANT. For now, we'll use DLM_ECANCEL to signify
194 * CANCELGRANT - the CANCEL was supposed to happen but didn't. We
195 * can then use DLM_EUNLOCK to signify a successful CANCEL -
196 * effectively, the CANCEL caused the lock to roll back.
198 * In the future, we will likely move the o2dlm to send only one
199 * ast - either unlock_ast() for a successful CANCEL or lock_ast()
200 * when the grant succeeds. At that point, we'll send DLM_ECANCEL
201 * for all cancel results (CANCELGRANT will no longer exist).
203 error = dlm_status_to_errno(status);
205 /* Successful unlock is DLM_EUNLOCK */
207 error = -DLM_EUNLOCK;
209 lproto->lp_unlock_ast(astarg, error);
212 int ocfs2_dlm_lock(struct ocfs2_cluster_connection *conn,
214 union ocfs2_dlm_lksb *lksb,
217 unsigned int namelen,
220 enum dlm_status status;
221 int o2dlm_mode = mode_to_o2dlm(mode);
222 int o2dlm_flags = flags_to_o2dlm(flags);
225 BUG_ON(lproto == NULL);
227 status = dlmlock(conn->cc_lockspace, o2dlm_mode, &lksb->lksb_o2dlm,
228 o2dlm_flags, name, namelen,
229 o2dlm_lock_ast_wrapper, astarg,
230 o2dlm_blocking_ast_wrapper);
231 ret = dlm_status_to_errno(status);
235 int ocfs2_dlm_unlock(struct ocfs2_cluster_connection *conn,
236 union ocfs2_dlm_lksb *lksb,
240 enum dlm_status status;
241 int o2dlm_flags = flags_to_o2dlm(flags);
244 BUG_ON(lproto == NULL);
246 status = dlmunlock(conn->cc_lockspace, &lksb->lksb_o2dlm,
247 o2dlm_flags, o2dlm_unlock_ast_wrapper, astarg);
248 ret = dlm_status_to_errno(status);
252 int ocfs2_dlm_lock_status(union ocfs2_dlm_lksb *lksb)
254 return dlm_status_to_errno(lksb->lksb_o2dlm.status);
258 * Why don't we cast to ocfs2_meta_lvb? The "clean" answer is that we
259 * don't cast at the glue level. The real answer is that the header
260 * ordering is nigh impossible.
262 void *ocfs2_dlm_lvb(union ocfs2_dlm_lksb *lksb)
264 return (void *)(lksb->lksb_o2dlm.lvb);
268 * Called from the dlm when it's about to evict a node. This is how the
269 * classic stack signals node death.
271 static void o2dlm_eviction_cb(int node_num, void *data)
273 struct ocfs2_cluster_connection *conn = data;
275 mlog(ML_NOTICE, "o2dlm has evicted node %d from group %.*s\n",
276 node_num, conn->cc_namelen, conn->cc_name);
278 conn->cc_recovery_handler(node_num, conn->cc_recovery_data);
281 int ocfs2_cluster_connect(const char *group,
283 void (*recovery_handler)(int node_num,
284 void *recovery_data),
286 struct ocfs2_cluster_connection **conn)
289 struct ocfs2_cluster_connection *new_conn;
291 struct dlm_ctxt *dlm;
292 struct o2dlm_private *priv;
293 struct dlm_protocol_version dlm_version;
295 BUG_ON(group == NULL);
296 BUG_ON(conn == NULL);
297 BUG_ON(recovery_handler == NULL);
299 if (grouplen > GROUP_NAME_MAX) {
304 new_conn = kzalloc(sizeof(struct ocfs2_cluster_connection),
311 memcpy(new_conn->cc_name, group, grouplen);
312 new_conn->cc_namelen = grouplen;
313 new_conn->cc_recovery_handler = recovery_handler;
314 new_conn->cc_recovery_data = recovery_data;
316 /* Start the new connection at our maximum compatibility level */
317 new_conn->cc_version = lproto->lp_max_version;
319 priv = kzalloc(sizeof(struct o2dlm_private), GFP_KERNEL);
325 /* This just fills the structure in. It is safe to use new_conn. */
326 dlm_setup_eviction_cb(&priv->op_eviction_cb, o2dlm_eviction_cb,
329 new_conn->cc_private = priv;
331 /* used by the dlm code to make message headers unique, each
332 * node in this domain must agree on this. */
333 dlm_key = crc32_le(0, group, grouplen);
334 dlm_version.pv_major = new_conn->cc_version.pv_major;
335 dlm_version.pv_minor = new_conn->cc_version.pv_minor;
337 dlm = dlm_register_domain(group, dlm_key, &dlm_version);
344 new_conn->cc_version.pv_major = dlm_version.pv_major;
345 new_conn->cc_version.pv_minor = dlm_version.pv_minor;
346 new_conn->cc_lockspace = dlm;
348 dlm_register_eviction_cb(dlm, &priv->op_eviction_cb);
354 kfree(new_conn->cc_private);
362 int ocfs2_cluster_disconnect(struct ocfs2_cluster_connection *conn)
364 struct dlm_ctxt *dlm = conn->cc_lockspace;
365 struct o2dlm_private *priv = conn->cc_private;
367 dlm_unregister_eviction_cb(&priv->op_eviction_cb);
368 dlm_unregister_domain(dlm);
376 int ocfs2_cluster_this_node(unsigned int *node)
380 node_num = o2nm_this_node();
381 if (node_num == O2NM_INVALID_NODE_NUM)
384 if (node_num >= O2NM_MAX_NODES)
391 void o2cb_get_stack(struct ocfs2_locking_protocol *proto)
393 BUG_ON(proto == NULL);
398 void o2cb_put_stack(void)