ocfs2: Provide the ocfs2_dlm_lvb_valid() stack API.
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / ocfs2 / stack_o2cb.c
1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * stack_o2cb.c
5 *
6 * Code which interfaces ocfs2 with the o2cb stack.
7 *
8 * Copyright (C) 2007 Oracle. All rights reserved.
9 *
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.
13 *
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.
18 */
19
20 #include <linux/crc32.h>
21 #include <linux/module.h>
22
23 /* Needed for AOP_TRUNCATED_PAGE in mlog_errno() */
24 #include <linux/fs.h>
25
26 #include "cluster/masklog.h"
27 #include "cluster/nodemanager.h"
28 #include "cluster/heartbeat.h"
29
30 #include "stackglue.h"
31
32 struct o2dlm_private {
33 struct dlm_eviction_cb op_eviction_cb;
34 };
35
36 static struct ocfs2_stack_plugin o2cb_stack;
37
38 /* These should be identical */
39 #if (DLM_LOCK_IV != LKM_IVMODE)
40 # error Lock modes do not match
41 #endif
42 #if (DLM_LOCK_NL != LKM_NLMODE)
43 # error Lock modes do not match
44 #endif
45 #if (DLM_LOCK_CR != LKM_CRMODE)
46 # error Lock modes do not match
47 #endif
48 #if (DLM_LOCK_CW != LKM_CWMODE)
49 # error Lock modes do not match
50 #endif
51 #if (DLM_LOCK_PR != LKM_PRMODE)
52 # error Lock modes do not match
53 #endif
54 #if (DLM_LOCK_PW != LKM_PWMODE)
55 # error Lock modes do not match
56 #endif
57 #if (DLM_LOCK_EX != LKM_EXMODE)
58 # error Lock modes do not match
59 #endif
60 static inline int mode_to_o2dlm(int mode)
61 {
62 BUG_ON(mode > LKM_MAXMODE);
63
64 return mode;
65 }
66
67 #define map_flag(_generic, _o2dlm) \
68 if (flags & (_generic)) { \
69 flags &= ~(_generic); \
70 o2dlm_flags |= (_o2dlm); \
71 }
72 static int flags_to_o2dlm(u32 flags)
73 {
74 int o2dlm_flags = 0;
75
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);
85
86 /* map_flag() should have cleared every flag passed in */
87 BUG_ON(flags != 0);
88
89 return o2dlm_flags;
90 }
91 #undef map_flag
92
93 /*
94 * Map an o2dlm status to standard errno values.
95 *
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.
98 *
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!
102 *
103 * DLM_NORMAL: 0
104 * DLM_NOTQUEUED: -EAGAIN
105 * DLM_CANCELGRANT: -EBUSY
106 * DLM_CANCEL: -DLM_ECANCEL
107 */
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,
152 };
153
154 static int dlm_status_to_errno(enum dlm_status status)
155 {
156 BUG_ON(status > (sizeof(status_map) / sizeof(status_map[0])));
157
158 return status_map[status];
159 }
160
161 static void o2dlm_lock_ast_wrapper(void *astarg)
162 {
163 BUG_ON(o2cb_stack.sp_proto == NULL);
164
165 o2cb_stack.sp_proto->lp_lock_ast(astarg);
166 }
167
168 static void o2dlm_blocking_ast_wrapper(void *astarg, int level)
169 {
170 BUG_ON(o2cb_stack.sp_proto == NULL);
171
172 o2cb_stack.sp_proto->lp_blocking_ast(astarg, level);
173 }
174
175 static void o2dlm_unlock_ast_wrapper(void *astarg, enum dlm_status status)
176 {
177 int error = dlm_status_to_errno(status);
178
179 BUG_ON(o2cb_stack.sp_proto == NULL);
180
181 /*
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
186 * DLM_CANCELGRANT.
187 *
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
190 * granted lock.
191 */
192 if (status == DLM_CANCELGRANT)
193 return;
194
195 o2cb_stack.sp_proto->lp_unlock_ast(astarg, error);
196 }
197
198 static int o2cb_dlm_lock(struct ocfs2_cluster_connection *conn,
199 int mode,
200 union ocfs2_dlm_lksb *lksb,
201 u32 flags,
202 void *name,
203 unsigned int namelen,
204 void *astarg)
205 {
206 enum dlm_status status;
207 int o2dlm_mode = mode_to_o2dlm(mode);
208 int o2dlm_flags = flags_to_o2dlm(flags);
209 int ret;
210
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);
216 return ret;
217 }
218
219 static int o2cb_dlm_unlock(struct ocfs2_cluster_connection *conn,
220 union ocfs2_dlm_lksb *lksb,
221 u32 flags,
222 void *astarg)
223 {
224 enum dlm_status status;
225 int o2dlm_flags = flags_to_o2dlm(flags);
226 int ret;
227
228 status = dlmunlock(conn->cc_lockspace, &lksb->lksb_o2dlm,
229 o2dlm_flags, o2dlm_unlock_ast_wrapper, astarg);
230 ret = dlm_status_to_errno(status);
231 return ret;
232 }
233
234 static int o2cb_dlm_lock_status(union ocfs2_dlm_lksb *lksb)
235 {
236 return dlm_status_to_errno(lksb->lksb_o2dlm.status);
237 }
238
239 /*
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
242 * the contents.
243 */
244 static int o2cb_dlm_lvb_valid(union ocfs2_dlm_lksb *lksb)
245 {
246 return 1;
247 }
248
249 static void *o2cb_dlm_lvb(union ocfs2_dlm_lksb *lksb)
250 {
251 return (void *)(lksb->lksb_o2dlm.lvb);
252 }
253
254 static void o2cb_dump_lksb(union ocfs2_dlm_lksb *lksb)
255 {
256 dlm_print_one_lock(lksb->lksb_o2dlm.lockid);
257 }
258
259 /*
260 * Called from the dlm when it's about to evict a node. This is how the
261 * classic stack signals node death.
262 */
263 static void o2dlm_eviction_cb(int node_num, void *data)
264 {
265 struct ocfs2_cluster_connection *conn = data;
266
267 mlog(ML_NOTICE, "o2dlm has evicted node %d from group %.*s\n",
268 node_num, conn->cc_namelen, conn->cc_name);
269
270 conn->cc_recovery_handler(node_num, conn->cc_recovery_data);
271 }
272
273 static int o2cb_cluster_connect(struct ocfs2_cluster_connection *conn)
274 {
275 int rc = 0;
276 u32 dlm_key;
277 struct dlm_ctxt *dlm;
278 struct o2dlm_private *priv;
279 struct dlm_protocol_version dlm_version;
280
281 BUG_ON(conn == NULL);
282 BUG_ON(o2cb_stack.sp_proto == NULL);
283
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()) {
287 rc = -EINVAL;
288 goto out;
289 }
290
291 priv = kzalloc(sizeof(struct o2dlm_private), GFP_KERNEL);
292 if (!priv) {
293 rc = -ENOMEM;
294 goto out_free;
295 }
296
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,
299 conn);
300
301 conn->cc_private = priv;
302
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;
308
309 dlm = dlm_register_domain(conn->cc_name, dlm_key, &dlm_version);
310 if (IS_ERR(dlm)) {
311 rc = PTR_ERR(dlm);
312 mlog_errno(rc);
313 goto out_free;
314 }
315
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;
319
320 dlm_register_eviction_cb(dlm, &priv->op_eviction_cb);
321
322 out_free:
323 if (rc && conn->cc_private)
324 kfree(conn->cc_private);
325
326 out:
327 return rc;
328 }
329
330 static int o2cb_cluster_disconnect(struct ocfs2_cluster_connection *conn)
331 {
332 struct dlm_ctxt *dlm = conn->cc_lockspace;
333 struct o2dlm_private *priv = conn->cc_private;
334
335 dlm_unregister_eviction_cb(&priv->op_eviction_cb);
336 conn->cc_private = NULL;
337 kfree(priv);
338
339 dlm_unregister_domain(dlm);
340 conn->cc_lockspace = NULL;
341
342 return 0;
343 }
344
345 static int o2cb_cluster_this_node(unsigned int *node)
346 {
347 int node_num;
348
349 node_num = o2nm_this_node();
350 if (node_num == O2NM_INVALID_NODE_NUM)
351 return -ENOENT;
352
353 if (node_num >= O2NM_MAX_NODES)
354 return -EOVERFLOW;
355
356 *node = node_num;
357 return 0;
358 }
359
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,
370 };
371
372 static struct ocfs2_stack_plugin o2cb_stack = {
373 .sp_name = "o2cb",
374 .sp_ops = &o2cb_stack_ops,
375 .sp_owner = THIS_MODULE,
376 };
377
378 static int __init o2cb_stack_init(void)
379 {
380 return ocfs2_stack_glue_register(&o2cb_stack);
381 }
382
383 static void __exit o2cb_stack_exit(void)
384 {
385 ocfs2_stack_glue_unregister(&o2cb_stack);
386 }
387
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);