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 "stackglue.h"
30 static struct ocfs2_locking_protocol *lproto;
32 struct o2dlm_private {
33 struct dlm_eviction_cb op_eviction_cb;
36 /* These should be identical */
37 #if (DLM_LOCK_IV != LKM_IVMODE)
38 # error Lock modes do not match
40 #if (DLM_LOCK_NL != LKM_NLMODE)
41 # error Lock modes do not match
43 #if (DLM_LOCK_CR != LKM_CRMODE)
44 # error Lock modes do not match
46 #if (DLM_LOCK_CW != LKM_CWMODE)
47 # error Lock modes do not match
49 #if (DLM_LOCK_PR != LKM_PRMODE)
50 # error Lock modes do not match
52 #if (DLM_LOCK_PW != LKM_PWMODE)
53 # error Lock modes do not match
55 #if (DLM_LOCK_EX != LKM_EXMODE)
56 # error Lock modes do not match
58 static inline int mode_to_o2dlm(int mode)
60 BUG_ON(mode > LKM_MAXMODE);
65 #define map_flag(_generic, _o2dlm) \
66 if (flags & (_generic)) { \
67 flags &= ~(_generic); \
68 o2dlm_flags |= (_o2dlm); \
70 static int flags_to_o2dlm(u32 flags)
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);
84 /* map_flag() should have cleared every flag passed in */
92 * Map an o2dlm status to standard errno values.
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.
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!
102 * DLM_NOTQUEUED: -EAGAIN
103 * DLM_CANCELGRANT: -DLM_ECANCEL
104 * DLM_CANCEL: -DLM_EUNLOCK
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,
151 static int dlm_status_to_errno(enum dlm_status status)
153 BUG_ON(status > (sizeof(status_map) / sizeof(status_map[0])));
155 return status_map[status];
158 static void o2dlm_lock_ast_wrapper(void *astarg)
160 BUG_ON(lproto == NULL);
162 lproto->lp_lock_ast(astarg);
165 static void o2dlm_blocking_ast_wrapper(void *astarg, int level)
167 BUG_ON(lproto == NULL);
169 lproto->lp_blocking_ast(astarg, level);
172 static void o2dlm_unlock_ast_wrapper(void *astarg, enum dlm_status status)
176 BUG_ON(lproto == NULL);
179 * XXX: CANCEL values are sketchy.
181 * Currently we have preserved the o2dlm paradigm. You can get
182 * unlock_ast() whether the cancel succeded or not.
184 * First, we're going to pass DLM_EUNLOCK just like fs/dlm does for
185 * successful unlocks. That is a clean behavior.
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.
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).
201 error = dlm_status_to_errno(status);
203 /* Successful unlock is DLM_EUNLOCK */
205 error = -DLM_EUNLOCK;
207 lproto->lp_unlock_ast(astarg, error);
210 int ocfs2_dlm_lock(struct ocfs2_cluster_connection *conn,
212 union ocfs2_dlm_lksb *lksb,
215 unsigned int namelen,
218 enum dlm_status status;
219 int o2dlm_mode = mode_to_o2dlm(mode);
220 int o2dlm_flags = flags_to_o2dlm(flags);
223 BUG_ON(lproto == NULL);
225 status = dlmlock(conn->cc_lockspace, o2dlm_mode, &lksb->lksb_o2dlm,
226 o2dlm_flags, name, namelen,
227 o2dlm_lock_ast_wrapper, astarg,
228 o2dlm_blocking_ast_wrapper);
229 ret = dlm_status_to_errno(status);
233 int ocfs2_dlm_unlock(struct ocfs2_cluster_connection *conn,
234 union ocfs2_dlm_lksb *lksb,
238 enum dlm_status status;
239 int o2dlm_flags = flags_to_o2dlm(flags);
242 BUG_ON(lproto == NULL);
244 status = dlmunlock(conn->cc_lockspace, &lksb->lksb_o2dlm,
245 o2dlm_flags, o2dlm_unlock_ast_wrapper, astarg);
246 ret = dlm_status_to_errno(status);
250 int ocfs2_dlm_lock_status(union ocfs2_dlm_lksb *lksb)
252 return dlm_status_to_errno(lksb->lksb_o2dlm.status);
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.
260 void *ocfs2_dlm_lvb(union ocfs2_dlm_lksb *lksb)
262 return (void *)(lksb->lksb_o2dlm.lvb);
266 * Called from the dlm when it's about to evict a node. This is how the
267 * classic stack signals node death.
269 static void o2dlm_eviction_cb(int node_num, void *data)
271 struct ocfs2_cluster_connection *conn = data;
273 mlog(ML_NOTICE, "o2dlm has evicted node %d from group %.*s\n",
274 node_num, conn->cc_namelen, conn->cc_name);
276 conn->cc_recovery_handler(node_num, conn->cc_recovery_data);
279 int ocfs2_cluster_connect(const char *group,
281 void (*recovery_handler)(int node_num,
282 void *recovery_data),
284 struct ocfs2_cluster_connection **conn)
287 struct ocfs2_cluster_connection *new_conn;
289 struct dlm_ctxt *dlm;
290 struct o2dlm_private *priv;
291 struct dlm_protocol_version dlm_version;
293 BUG_ON(group == NULL);
294 BUG_ON(conn == NULL);
295 BUG_ON(recovery_handler == NULL);
297 if (grouplen > GROUP_NAME_MAX) {
302 new_conn = kzalloc(sizeof(struct ocfs2_cluster_connection),
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;
314 /* Start the new connection at our maximum compatibility level */
315 new_conn->cc_version = lproto->lp_max_version;
317 priv = kzalloc(sizeof(struct o2dlm_private), GFP_KERNEL);
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,
327 new_conn->cc_private = priv;
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;
335 dlm = dlm_register_domain(group, dlm_key, &dlm_version);
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;
346 dlm_register_eviction_cb(dlm, &priv->op_eviction_cb);
352 kfree(new_conn->cc_private);
360 int ocfs2_cluster_disconnect(struct ocfs2_cluster_connection *conn)
362 struct dlm_ctxt *dlm = conn->cc_lockspace;
363 struct o2dlm_private *priv = conn->cc_private;
365 dlm_unregister_eviction_cb(&priv->op_eviction_cb);
366 dlm_unregister_domain(dlm);
374 void o2cb_get_stack(struct ocfs2_locking_protocol *proto)
376 BUG_ON(proto == NULL);
381 void o2cb_put_stack(void)