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
);
240 * o2dlm aways has a "valid" LVB. If the dlm loses track of the LVB
241 * contents, it will zero out the LVB. Thus the caller can always trust
244 static int o2cb_dlm_lvb_valid(union ocfs2_dlm_lksb
*lksb
)
249 static void *o2cb_dlm_lvb(union ocfs2_dlm_lksb
*lksb
)
251 return (void *)(lksb
->lksb_o2dlm
.lvb
);
254 static void o2cb_dump_lksb(union ocfs2_dlm_lksb
*lksb
)
256 dlm_print_one_lock(lksb
->lksb_o2dlm
.lockid
);
260 * Called from the dlm when it's about to evict a node. This is how the
261 * classic stack signals node death.
263 static void o2dlm_eviction_cb(int node_num
, void *data
)
265 struct ocfs2_cluster_connection
*conn
= data
;
267 mlog(ML_NOTICE
, "o2dlm has evicted node %d from group %.*s\n",
268 node_num
, conn
->cc_namelen
, conn
->cc_name
);
270 conn
->cc_recovery_handler(node_num
, conn
->cc_recovery_data
);
273 static int o2cb_cluster_connect(struct ocfs2_cluster_connection
*conn
)
277 struct dlm_ctxt
*dlm
;
278 struct o2dlm_private
*priv
;
279 struct dlm_protocol_version dlm_version
;
281 BUG_ON(conn
== NULL
);
282 BUG_ON(o2cb_stack
.sp_proto
== NULL
);
284 /* for now we only have one cluster/node, make sure we see it
285 * in the heartbeat universe */
286 if (!o2hb_check_local_node_heartbeating()) {
291 priv
= kzalloc(sizeof(struct o2dlm_private
), GFP_KERNEL
);
297 /* This just fills the structure in. It is safe to pass conn. */
298 dlm_setup_eviction_cb(&priv
->op_eviction_cb
, o2dlm_eviction_cb
,
301 conn
->cc_private
= priv
;
303 /* used by the dlm code to make message headers unique, each
304 * node in this domain must agree on this. */
305 dlm_key
= crc32_le(0, conn
->cc_name
, conn
->cc_namelen
);
306 dlm_version
.pv_major
= conn
->cc_version
.pv_major
;
307 dlm_version
.pv_minor
= conn
->cc_version
.pv_minor
;
309 dlm
= dlm_register_domain(conn
->cc_name
, dlm_key
, &dlm_version
);
316 conn
->cc_version
.pv_major
= dlm_version
.pv_major
;
317 conn
->cc_version
.pv_minor
= dlm_version
.pv_minor
;
318 conn
->cc_lockspace
= dlm
;
320 dlm_register_eviction_cb(dlm
, &priv
->op_eviction_cb
);
323 if (rc
&& conn
->cc_private
)
324 kfree(conn
->cc_private
);
330 static int o2cb_cluster_disconnect(struct ocfs2_cluster_connection
*conn
)
332 struct dlm_ctxt
*dlm
= conn
->cc_lockspace
;
333 struct o2dlm_private
*priv
= conn
->cc_private
;
335 dlm_unregister_eviction_cb(&priv
->op_eviction_cb
);
336 conn
->cc_private
= NULL
;
339 dlm_unregister_domain(dlm
);
340 conn
->cc_lockspace
= NULL
;
345 static int o2cb_cluster_this_node(unsigned int *node
)
349 node_num
= o2nm_this_node();
350 if (node_num
== O2NM_INVALID_NODE_NUM
)
353 if (node_num
>= O2NM_MAX_NODES
)
360 static struct ocfs2_stack_operations o2cb_stack_ops
= {
361 .connect
= o2cb_cluster_connect
,
362 .disconnect
= o2cb_cluster_disconnect
,
363 .this_node
= o2cb_cluster_this_node
,
364 .dlm_lock
= o2cb_dlm_lock
,
365 .dlm_unlock
= o2cb_dlm_unlock
,
366 .lock_status
= o2cb_dlm_lock_status
,
367 .lvb_valid
= o2cb_dlm_lvb_valid
,
368 .lock_lvb
= o2cb_dlm_lvb
,
369 .dump_lksb
= o2cb_dump_lksb
,
372 static struct ocfs2_stack_plugin o2cb_stack
= {
374 .sp_ops
= &o2cb_stack_ops
,
375 .sp_owner
= THIS_MODULE
,
378 static int __init
o2cb_stack_init(void)
380 return ocfs2_stack_glue_register(&o2cb_stack
);
383 static void __exit
o2cb_stack_exit(void)
385 ocfs2_stack_glue_unregister(&o2cb_stack
);
388 MODULE_AUTHOR("Oracle");
389 MODULE_DESCRIPTION("ocfs2 driver for the classic o2cb stack");
390 MODULE_LICENSE("GPL");
391 module_init(o2cb_stack_init
);
392 module_exit(o2cb_stack_exit
);