blob: 670fa945c212bd872ba224a545f6b21c9d0eda48 [file] [log] [blame]
Joel Becker24ef1812008-01-29 17:37:32 -08001/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * stackglue.c
5 *
6 * Code which implements an OCFS2 specific interface to underlying
7 * cluster stacks.
8 *
9 * Copyright (C) 2007 Oracle. All rights reserved.
10 *
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.
14 *
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.
19 */
20
Joel Becker4670c462008-02-01 14:39:35 -080021#include <linux/slab.h>
22#include <linux/crc32.h>
Joel Becker6953b4c2008-01-29 16:59:56 -080023#include <linux/kmod.h>
Joel Becker4670c462008-02-01 14:39:35 -080024
25/* Needed for AOP_TRUNCATED_PAGE in mlog_errno() */
26#include <linux/fs.h>
27
Joel Becker7431cd72008-02-01 12:15:37 -080028#include "cluster/masklog.h"
Joel Becker19fdb622008-01-30 15:38:24 -080029#include "cluster/nodemanager.h"
Joel Becker6953b4c2008-01-29 16:59:56 -080030#include "cluster/heartbeat.h"
Joel Becker19fdb622008-01-30 15:38:24 -080031
Joel Becker24ef1812008-01-29 17:37:32 -080032#include "stackglue.h"
33
34static struct ocfs2_locking_protocol *lproto;
35
Joel Becker4670c462008-02-01 14:39:35 -080036struct o2dlm_private {
37 struct dlm_eviction_cb op_eviction_cb;
38};
39
Joel Beckerbd3e7612008-02-01 12:14:57 -080040/* 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
62static inline int mode_to_o2dlm(int mode)
63{
64 BUG_ON(mode > LKM_MAXMODE);
65
66 return mode;
67}
68
69#define map_flag(_generic, _o2dlm) \
70 if (flags & (_generic)) { \
71 flags &= ~(_generic); \
72 o2dlm_flags |= (_o2dlm); \
73 }
74static int flags_to_o2dlm(u32 flags)
75{
76 int o2dlm_flags = 0;
77
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);
87
88 /* map_flag() should have cleared every flag passed in */
89 BUG_ON(flags != 0);
90
91 return o2dlm_flags;
92}
93#undef map_flag
94
Joel Becker7431cd72008-02-01 12:15:37 -080095/*
96 * Map an o2dlm status to standard errno values.
97 *
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.
100 *
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!
104 *
105 * DLM_NORMAL: 0
106 * DLM_NOTQUEUED: -EAGAIN
107 * DLM_CANCELGRANT: -DLM_ECANCEL
108 * DLM_CANCEL: -DLM_EUNLOCK
109 */
110/* Keep in sync with dlmapi.h */
111static int status_map[] = {
112 [DLM_NORMAL] = 0, /* Success */
113 [DLM_GRANTED] = -EINVAL,
114 [DLM_DENIED] = -EACCES,
115 [DLM_DENIED_NOLOCKS] = -EACCES,
116 [DLM_WORKING] = -EBUSY,
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] = -DLM_ECANCEL, /* 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_EUNLOCK, /* 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,
154};
155static int dlm_status_to_errno(enum dlm_status status)
156{
157 BUG_ON(status > (sizeof(status_map) / sizeof(status_map[0])));
158
159 return status_map[status];
160}
161
162static void o2dlm_lock_ast_wrapper(void *astarg)
163{
164 BUG_ON(lproto == NULL);
165
166 lproto->lp_lock_ast(astarg);
167}
168
169static void o2dlm_blocking_ast_wrapper(void *astarg, int level)
170{
171 BUG_ON(lproto == NULL);
172
173 lproto->lp_blocking_ast(astarg, level);
174}
175
176static void o2dlm_unlock_ast_wrapper(void *astarg, enum dlm_status status)
177{
178 int error;
179
180 BUG_ON(lproto == NULL);
181
182 /*
183 * XXX: CANCEL values are sketchy.
184 *
185 * Currently we have preserved the o2dlm paradigm. You can get
186 * unlock_ast() whether the cancel succeded or not.
187 *
188 * First, we're going to pass DLM_EUNLOCK just like fs/dlm does for
189 * successful unlocks. That is a clean behavior.
190 *
191 * In o2dlm, you can get both the lock_ast() for the lock being
192 * granted and the unlock_ast() for the CANCEL failing. A
193 * successful cancel sends DLM_NORMAL here. If the
194 * lock grant happened before the cancel arrived, you get
195 * DLM_CANCELGRANT. For now, we'll use DLM_ECANCEL to signify
196 * CANCELGRANT - the CANCEL was supposed to happen but didn't. We
197 * can then use DLM_EUNLOCK to signify a successful CANCEL -
198 * effectively, the CANCEL caused the lock to roll back.
199 *
200 * In the future, we will likely move the o2dlm to send only one
201 * ast - either unlock_ast() for a successful CANCEL or lock_ast()
202 * when the grant succeeds. At that point, we'll send DLM_ECANCEL
203 * for all cancel results (CANCELGRANT will no longer exist).
204 */
205 error = dlm_status_to_errno(status);
206
207 /* Successful unlock is DLM_EUNLOCK */
208 if (!error)
209 error = -DLM_EUNLOCK;
210
211 lproto->lp_unlock_ast(astarg, error);
212}
213
Joel Becker4670c462008-02-01 14:39:35 -0800214int ocfs2_dlm_lock(struct ocfs2_cluster_connection *conn,
Joel Becker24ef1812008-01-29 17:37:32 -0800215 int mode,
Joel Becker8f2c9c12008-02-01 12:16:57 -0800216 union ocfs2_dlm_lksb *lksb,
Joel Becker24ef1812008-01-29 17:37:32 -0800217 u32 flags,
218 void *name,
219 unsigned int namelen,
220 void *astarg)
221{
Joel Becker7431cd72008-02-01 12:15:37 -0800222 enum dlm_status status;
Joel Beckerbd3e7612008-02-01 12:14:57 -0800223 int o2dlm_mode = mode_to_o2dlm(mode);
224 int o2dlm_flags = flags_to_o2dlm(flags);
Joel Becker7431cd72008-02-01 12:15:37 -0800225 int ret;
Joel Beckerbd3e7612008-02-01 12:14:57 -0800226
Joel Becker24ef1812008-01-29 17:37:32 -0800227 BUG_ON(lproto == NULL);
Joel Beckerbd3e7612008-02-01 12:14:57 -0800228
Joel Becker4670c462008-02-01 14:39:35 -0800229 status = dlmlock(conn->cc_lockspace, o2dlm_mode, &lksb->lksb_o2dlm,
230 o2dlm_flags, name, namelen,
Joel Becker8f2c9c12008-02-01 12:16:57 -0800231 o2dlm_lock_ast_wrapper, astarg,
232 o2dlm_blocking_ast_wrapper);
Joel Becker7431cd72008-02-01 12:15:37 -0800233 ret = dlm_status_to_errno(status);
234 return ret;
Joel Becker24ef1812008-01-29 17:37:32 -0800235}
236
Joel Becker4670c462008-02-01 14:39:35 -0800237int ocfs2_dlm_unlock(struct ocfs2_cluster_connection *conn,
Joel Becker8f2c9c12008-02-01 12:16:57 -0800238 union ocfs2_dlm_lksb *lksb,
Joel Becker24ef1812008-01-29 17:37:32 -0800239 u32 flags,
240 void *astarg)
241{
Joel Becker7431cd72008-02-01 12:15:37 -0800242 enum dlm_status status;
Joel Beckerbd3e7612008-02-01 12:14:57 -0800243 int o2dlm_flags = flags_to_o2dlm(flags);
Joel Becker7431cd72008-02-01 12:15:37 -0800244 int ret;
Joel Beckerbd3e7612008-02-01 12:14:57 -0800245
Joel Becker24ef1812008-01-29 17:37:32 -0800246 BUG_ON(lproto == NULL);
247
Joel Becker4670c462008-02-01 14:39:35 -0800248 status = dlmunlock(conn->cc_lockspace, &lksb->lksb_o2dlm,
249 o2dlm_flags, o2dlm_unlock_ast_wrapper, astarg);
Joel Becker7431cd72008-02-01 12:15:37 -0800250 ret = dlm_status_to_errno(status);
251 return ret;
Joel Becker24ef1812008-01-29 17:37:32 -0800252}
253
Joel Becker8f2c9c12008-02-01 12:16:57 -0800254int ocfs2_dlm_lock_status(union ocfs2_dlm_lksb *lksb)
255{
256 return dlm_status_to_errno(lksb->lksb_o2dlm.status);
257}
258
259/*
260 * Why don't we cast to ocfs2_meta_lvb? The "clean" answer is that we
261 * don't cast at the glue level. The real answer is that the header
262 * ordering is nigh impossible.
263 */
264void *ocfs2_dlm_lvb(union ocfs2_dlm_lksb *lksb)
265{
266 return (void *)(lksb->lksb_o2dlm.lvb);
267}
Joel Becker24ef1812008-01-29 17:37:32 -0800268
Joel Becker4670c462008-02-01 14:39:35 -0800269/*
270 * Called from the dlm when it's about to evict a node. This is how the
271 * classic stack signals node death.
272 */
273static void o2dlm_eviction_cb(int node_num, void *data)
274{
275 struct ocfs2_cluster_connection *conn = data;
276
277 mlog(ML_NOTICE, "o2dlm has evicted node %d from group %.*s\n",
278 node_num, conn->cc_namelen, conn->cc_name);
279
280 conn->cc_recovery_handler(node_num, conn->cc_recovery_data);
281}
282
283int ocfs2_cluster_connect(const char *group,
284 int grouplen,
285 void (*recovery_handler)(int node_num,
286 void *recovery_data),
287 void *recovery_data,
288 struct ocfs2_cluster_connection **conn)
289{
290 int rc = 0;
291 struct ocfs2_cluster_connection *new_conn;
292 u32 dlm_key;
293 struct dlm_ctxt *dlm;
294 struct o2dlm_private *priv;
295 struct dlm_protocol_version dlm_version;
296
297 BUG_ON(group == NULL);
298 BUG_ON(conn == NULL);
299 BUG_ON(recovery_handler == NULL);
300
301 if (grouplen > GROUP_NAME_MAX) {
302 rc = -EINVAL;
303 goto out;
304 }
305
Joel Becker6953b4c2008-01-29 16:59:56 -0800306 /* for now we only have one cluster/node, make sure we see it
307 * in the heartbeat universe */
308 if (!o2hb_check_local_node_heartbeating()) {
309 rc = -EINVAL;
310 goto out;
311 }
312
Joel Becker4670c462008-02-01 14:39:35 -0800313 new_conn = kzalloc(sizeof(struct ocfs2_cluster_connection),
314 GFP_KERNEL);
315 if (!new_conn) {
316 rc = -ENOMEM;
317 goto out;
318 }
319
320 memcpy(new_conn->cc_name, group, grouplen);
321 new_conn->cc_namelen = grouplen;
322 new_conn->cc_recovery_handler = recovery_handler;
323 new_conn->cc_recovery_data = recovery_data;
324
325 /* Start the new connection at our maximum compatibility level */
326 new_conn->cc_version = lproto->lp_max_version;
327
328 priv = kzalloc(sizeof(struct o2dlm_private), GFP_KERNEL);
329 if (!priv) {
330 rc = -ENOMEM;
331 goto out_free;
332 }
333
334 /* This just fills the structure in. It is safe to use new_conn. */
335 dlm_setup_eviction_cb(&priv->op_eviction_cb, o2dlm_eviction_cb,
336 new_conn);
337
338 new_conn->cc_private = priv;
339
340 /* used by the dlm code to make message headers unique, each
341 * node in this domain must agree on this. */
342 dlm_key = crc32_le(0, group, grouplen);
343 dlm_version.pv_major = new_conn->cc_version.pv_major;
344 dlm_version.pv_minor = new_conn->cc_version.pv_minor;
345
346 dlm = dlm_register_domain(group, dlm_key, &dlm_version);
347 if (IS_ERR(dlm)) {
348 rc = PTR_ERR(dlm);
349 mlog_errno(rc);
350 goto out_free;
351 }
352
353 new_conn->cc_version.pv_major = dlm_version.pv_major;
354 new_conn->cc_version.pv_minor = dlm_version.pv_minor;
355 new_conn->cc_lockspace = dlm;
356
357 dlm_register_eviction_cb(dlm, &priv->op_eviction_cb);
358
359 *conn = new_conn;
360
361out_free:
362 if (rc) {
363 kfree(new_conn->cc_private);
364 kfree(new_conn);
365 }
366
367out:
368 return rc;
369}
370
Joel Becker6953b4c2008-01-29 16:59:56 -0800371
Joel Becker4670c462008-02-01 14:39:35 -0800372int ocfs2_cluster_disconnect(struct ocfs2_cluster_connection *conn)
373{
374 struct dlm_ctxt *dlm = conn->cc_lockspace;
375 struct o2dlm_private *priv = conn->cc_private;
376
377 dlm_unregister_eviction_cb(&priv->op_eviction_cb);
378 dlm_unregister_domain(dlm);
379
380 kfree(priv);
381 kfree(conn);
382
383 return 0;
384}
385
Joel Becker6953b4c2008-01-29 16:59:56 -0800386static void o2hb_stop(const char *group)
387{
388 int ret;
389 char *argv[5], *envp[3];
390
391 argv[0] = (char *)o2nm_get_hb_ctl_path();
392 argv[1] = "-K";
393 argv[2] = "-u";
394 argv[3] = (char *)group;
395 argv[4] = NULL;
396
397 mlog(0, "Run: %s %s %s %s\n", argv[0], argv[1], argv[2], argv[3]);
398
399 /* minimal command environment taken from cpu_run_sbin_hotplug */
400 envp[0] = "HOME=/";
401 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
402 envp[2] = NULL;
403
404 ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
405 if (ret < 0)
406 mlog_errno(ret);
407}
408
409/*
410 * Hangup is a hack for tools compatibility. Older ocfs2-tools software
411 * expects the filesystem to call "ocfs2_hb_ctl" during unmount. This
412 * happens regardless of whether the DLM got started, so we can't do it
413 * in ocfs2_cluster_disconnect(). We bring the o2hb_stop() function into
414 * the glue and provide a "hangup" API for super.c to call.
415 *
416 * Other stacks will eventually provide a NULL ->hangup() pointer.
417 */
418void ocfs2_cluster_hangup(const char *group, int grouplen)
419{
420 BUG_ON(group == NULL);
421 BUG_ON(group[grouplen] != '\0');
422
423 o2hb_stop(group);
424}
425
Joel Becker19fdb622008-01-30 15:38:24 -0800426int ocfs2_cluster_this_node(unsigned int *node)
427{
428 int node_num;
429
430 node_num = o2nm_this_node();
431 if (node_num == O2NM_INVALID_NODE_NUM)
432 return -ENOENT;
433
434 if (node_num >= O2NM_MAX_NODES)
435 return -EOVERFLOW;
436
437 *node = node_num;
438 return 0;
439}
440
Joel Becker24ef1812008-01-29 17:37:32 -0800441void o2cb_get_stack(struct ocfs2_locking_protocol *proto)
442{
443 BUG_ON(proto == NULL);
444
445 lproto = proto;
446}
447
448void o2cb_put_stack(void)
449{
450 lproto = NULL;
451}