staging: hv: Fix error checking in channel.c
[linux/fpc-iii.git] / fs / ocfs2 / stack_o2cb.c
blob0d3049f696c5418448173ed3538020bd5d51b9d0
1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
4 * stack_o2cb.c
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/kernel.h>
21 #include <linux/crc32.h>
22 #include <linux/slab.h>
23 #include <linux/module.h>
25 /* Needed for AOP_TRUNCATED_PAGE in mlog_errno() */
26 #include <linux/fs.h>
28 #include "cluster/masklog.h"
29 #include "cluster/nodemanager.h"
30 #include "cluster/heartbeat.h"
32 #include "stackglue.h"
34 struct o2dlm_private {
35 struct dlm_eviction_cb op_eviction_cb;
38 static struct ocfs2_stack_plugin o2cb_stack;
40 /* These should be identical */
41 #if (DLM_LOCK_IV != LKM_IVMODE)
42 # error Lock modes do not match
43 #endif
44 #if (DLM_LOCK_NL != LKM_NLMODE)
45 # error Lock modes do not match
46 #endif
47 #if (DLM_LOCK_CR != LKM_CRMODE)
48 # error Lock modes do not match
49 #endif
50 #if (DLM_LOCK_CW != LKM_CWMODE)
51 # error Lock modes do not match
52 #endif
53 #if (DLM_LOCK_PR != LKM_PRMODE)
54 # error Lock modes do not match
55 #endif
56 #if (DLM_LOCK_PW != LKM_PWMODE)
57 # error Lock modes do not match
58 #endif
59 #if (DLM_LOCK_EX != LKM_EXMODE)
60 # error Lock modes do not match
61 #endif
62 static inline int mode_to_o2dlm(int mode)
64 BUG_ON(mode > LKM_MAXMODE);
66 return mode;
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)
76 int o2dlm_flags = 0;
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 */
89 BUG_ON(flags != 0);
91 return o2dlm_flags;
93 #undef map_flag
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!
105 * DLM_NORMAL: 0
106 * DLM_NOTQUEUED: -EAGAIN
107 * DLM_CANCELGRANT: -EBUSY
108 * DLM_CANCEL: -DLM_ECANCEL
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] = -EACCES,
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] = -EBUSY, /* 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_ECANCEL, /* 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,
156 static int dlm_status_to_errno(enum dlm_status status)
158 BUG_ON(status < 0 || status >= ARRAY_SIZE(status_map));
160 return status_map[status];
163 static void o2dlm_lock_ast_wrapper(void *astarg)
165 struct ocfs2_dlm_lksb *lksb = astarg;
167 lksb->lksb_conn->cc_proto->lp_lock_ast(lksb);
170 static void o2dlm_blocking_ast_wrapper(void *astarg, int level)
172 struct ocfs2_dlm_lksb *lksb = astarg;
174 lksb->lksb_conn->cc_proto->lp_blocking_ast(lksb, level);
177 static void o2dlm_unlock_ast_wrapper(void *astarg, enum dlm_status status)
179 struct ocfs2_dlm_lksb *lksb = astarg;
180 int error = dlm_status_to_errno(status);
183 * In o2dlm, you can get both the lock_ast() for the lock being
184 * granted and the unlock_ast() for the CANCEL failing. A
185 * successful cancel sends DLM_NORMAL here. If the
186 * lock grant happened before the cancel arrived, you get
187 * DLM_CANCELGRANT.
189 * There's no need for the double-ast. If we see DLM_CANCELGRANT,
190 * we just ignore it. We expect the lock_ast() to handle the
191 * granted lock.
193 if (status == DLM_CANCELGRANT)
194 return;
196 lksb->lksb_conn->cc_proto->lp_unlock_ast(lksb, error);
199 static int o2cb_dlm_lock(struct ocfs2_cluster_connection *conn,
200 int mode,
201 struct ocfs2_dlm_lksb *lksb,
202 u32 flags,
203 void *name,
204 unsigned int namelen)
206 enum dlm_status status;
207 int o2dlm_mode = mode_to_o2dlm(mode);
208 int o2dlm_flags = flags_to_o2dlm(flags);
209 int ret;
211 status = dlmlock(conn->cc_lockspace, o2dlm_mode, &lksb->lksb_o2dlm,
212 o2dlm_flags, name, namelen,
213 o2dlm_lock_ast_wrapper, lksb,
214 o2dlm_blocking_ast_wrapper);
215 ret = dlm_status_to_errno(status);
216 return ret;
219 static int o2cb_dlm_unlock(struct ocfs2_cluster_connection *conn,
220 struct ocfs2_dlm_lksb *lksb,
221 u32 flags)
223 enum dlm_status status;
224 int o2dlm_flags = flags_to_o2dlm(flags);
225 int ret;
227 status = dlmunlock(conn->cc_lockspace, &lksb->lksb_o2dlm,
228 o2dlm_flags, o2dlm_unlock_ast_wrapper, lksb);
229 ret = dlm_status_to_errno(status);
230 return ret;
233 static int o2cb_dlm_lock_status(struct ocfs2_dlm_lksb *lksb)
235 return dlm_status_to_errno(lksb->lksb_o2dlm.status);
239 * o2dlm aways has a "valid" LVB. If the dlm loses track of the LVB
240 * contents, it will zero out the LVB. Thus the caller can always trust
241 * the contents.
243 static int o2cb_dlm_lvb_valid(struct ocfs2_dlm_lksb *lksb)
245 return 1;
248 static void *o2cb_dlm_lvb(struct ocfs2_dlm_lksb *lksb)
250 return (void *)(lksb->lksb_o2dlm.lvb);
253 static void o2cb_dump_lksb(struct ocfs2_dlm_lksb *lksb)
255 dlm_print_one_lock(lksb->lksb_o2dlm.lockid);
259 * Called from the dlm when it's about to evict a node. This is how the
260 * classic stack signals node death.
262 static void o2dlm_eviction_cb(int node_num, void *data)
264 struct ocfs2_cluster_connection *conn = data;
266 mlog(ML_NOTICE, "o2dlm has evicted node %d from group %.*s\n",
267 node_num, conn->cc_namelen, conn->cc_name);
269 conn->cc_recovery_handler(node_num, conn->cc_recovery_data);
272 static int o2cb_cluster_connect(struct ocfs2_cluster_connection *conn)
274 int rc = 0;
275 u32 dlm_key;
276 struct dlm_ctxt *dlm;
277 struct o2dlm_private *priv;
278 struct dlm_protocol_version fs_version;
280 BUG_ON(conn == NULL);
281 BUG_ON(conn->cc_proto == NULL);
283 /* for now we only have one cluster/node, make sure we see it
284 * in the heartbeat universe */
285 if (!o2hb_check_local_node_heartbeating()) {
286 rc = -EINVAL;
287 goto out;
290 priv = kzalloc(sizeof(struct o2dlm_private), GFP_KERNEL);
291 if (!priv) {
292 rc = -ENOMEM;
293 goto out_free;
296 /* This just fills the structure in. It is safe to pass conn. */
297 dlm_setup_eviction_cb(&priv->op_eviction_cb, o2dlm_eviction_cb,
298 conn);
300 conn->cc_private = priv;
302 /* used by the dlm code to make message headers unique, each
303 * node in this domain must agree on this. */
304 dlm_key = crc32_le(0, conn->cc_name, conn->cc_namelen);
305 fs_version.pv_major = conn->cc_version.pv_major;
306 fs_version.pv_minor = conn->cc_version.pv_minor;
308 dlm = dlm_register_domain(conn->cc_name, dlm_key, &fs_version);
309 if (IS_ERR(dlm)) {
310 rc = PTR_ERR(dlm);
311 mlog_errno(rc);
312 goto out_free;
315 conn->cc_version.pv_major = fs_version.pv_major;
316 conn->cc_version.pv_minor = fs_version.pv_minor;
317 conn->cc_lockspace = dlm;
319 dlm_register_eviction_cb(dlm, &priv->op_eviction_cb);
321 out_free:
322 if (rc && conn->cc_private)
323 kfree(conn->cc_private);
325 out:
326 return rc;
329 static int o2cb_cluster_disconnect(struct ocfs2_cluster_connection *conn)
331 struct dlm_ctxt *dlm = conn->cc_lockspace;
332 struct o2dlm_private *priv = conn->cc_private;
334 dlm_unregister_eviction_cb(&priv->op_eviction_cb);
335 conn->cc_private = NULL;
336 kfree(priv);
338 dlm_unregister_domain(dlm);
339 conn->cc_lockspace = NULL;
341 return 0;
344 static int o2cb_cluster_this_node(unsigned int *node)
346 int node_num;
348 node_num = o2nm_this_node();
349 if (node_num == O2NM_INVALID_NODE_NUM)
350 return -ENOENT;
352 if (node_num >= O2NM_MAX_NODES)
353 return -EOVERFLOW;
355 *node = node_num;
356 return 0;
359 static struct ocfs2_stack_operations o2cb_stack_ops = {
360 .connect = o2cb_cluster_connect,
361 .disconnect = o2cb_cluster_disconnect,
362 .this_node = o2cb_cluster_this_node,
363 .dlm_lock = o2cb_dlm_lock,
364 .dlm_unlock = o2cb_dlm_unlock,
365 .lock_status = o2cb_dlm_lock_status,
366 .lvb_valid = o2cb_dlm_lvb_valid,
367 .lock_lvb = o2cb_dlm_lvb,
368 .dump_lksb = o2cb_dump_lksb,
371 static struct ocfs2_stack_plugin o2cb_stack = {
372 .sp_name = "o2cb",
373 .sp_ops = &o2cb_stack_ops,
374 .sp_owner = THIS_MODULE,
377 static int __init o2cb_stack_init(void)
379 return ocfs2_stack_glue_register(&o2cb_stack);
382 static void __exit o2cb_stack_exit(void)
384 ocfs2_stack_glue_unregister(&o2cb_stack);
387 MODULE_AUTHOR("Oracle");
388 MODULE_DESCRIPTION("ocfs2 driver for the classic o2cb stack");
389 MODULE_LICENSE("GPL");
390 module_init(o2cb_stack_init);
391 module_exit(o2cb_stack_exit);