blob: e35dde6217f501bce9d8639509663604730f9b16 [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
Joel Beckerde551242008-02-01 14:45:08 -0800107 * DLM_CANCELGRANT: -EBUSY
108 * DLM_CANCEL: -DLM_ECANCEL
Joel Becker7431cd72008-02-01 12:15:37 -0800109 */
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,
Joel Beckerde551242008-02-01 14:45:08 -0800116 [DLM_WORKING] = -EACCES,
Joel Becker7431cd72008-02-01 12:15:37 -0800117 [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,
Joel Beckerde551242008-02-01 14:45:08 -0800122 [DLM_CANCELGRANT] = -EBUSY, /* Cancel after grant */
Joel Becker7431cd72008-02-01 12:15:37 -0800123 [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,
Joel Beckerde551242008-02-01 14:45:08 -0800140 [DLM_CANCEL] = -DLM_ECANCEL, /* Successful cancel */
Joel Becker7431cd72008-02-01 12:15:37 -0800141 [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};
Joel Beckerde551242008-02-01 14:45:08 -0800155
Joel Becker7431cd72008-02-01 12:15:37 -0800156static int dlm_status_to_errno(enum dlm_status status)
157{
158 BUG_ON(status > (sizeof(status_map) / sizeof(status_map[0])));
159
160 return status_map[status];
161}
162
163static void o2dlm_lock_ast_wrapper(void *astarg)
164{
165 BUG_ON(lproto == NULL);
166
167 lproto->lp_lock_ast(astarg);
168}
169
170static void o2dlm_blocking_ast_wrapper(void *astarg, int level)
171{
172 BUG_ON(lproto == NULL);
173
174 lproto->lp_blocking_ast(astarg, level);
175}
176
177static void o2dlm_unlock_ast_wrapper(void *astarg, enum dlm_status status)
178{
Joel Beckerde551242008-02-01 14:45:08 -0800179 int error = dlm_status_to_errno(status);
Joel Becker7431cd72008-02-01 12:15:37 -0800180
181 BUG_ON(lproto == NULL);
182
183 /*
Joel Becker7431cd72008-02-01 12:15:37 -0800184 * In o2dlm, you can get both the lock_ast() for the lock being
185 * granted and the unlock_ast() for the CANCEL failing. A
186 * successful cancel sends DLM_NORMAL here. If the
187 * lock grant happened before the cancel arrived, you get
Joel Beckerde551242008-02-01 14:45:08 -0800188 * DLM_CANCELGRANT.
Joel Becker7431cd72008-02-01 12:15:37 -0800189 *
Joel Beckerde551242008-02-01 14:45:08 -0800190 * There's no need for the double-ast. If we see DLM_CANCELGRANT,
191 * we just ignore it. We expect the lock_ast() to handle the
192 * granted lock.
Joel Becker7431cd72008-02-01 12:15:37 -0800193 */
Joel Beckerde551242008-02-01 14:45:08 -0800194 if (status == DLM_CANCELGRANT)
195 return;
Joel Becker7431cd72008-02-01 12:15:37 -0800196
197 lproto->lp_unlock_ast(astarg, error);
198}
199
Joel Becker553aa7e2008-02-01 14:51:03 -0800200static int o2cb_dlm_lock(struct ocfs2_cluster_connection *conn,
201 int mode,
202 union ocfs2_dlm_lksb *lksb,
203 u32 flags,
204 void *name,
205 unsigned int namelen,
206 void *astarg)
Joel Becker24ef1812008-01-29 17:37:32 -0800207{
Joel Becker7431cd72008-02-01 12:15:37 -0800208 enum dlm_status status;
Joel Beckerbd3e7612008-02-01 12:14:57 -0800209 int o2dlm_mode = mode_to_o2dlm(mode);
210 int o2dlm_flags = flags_to_o2dlm(flags);
Joel Becker7431cd72008-02-01 12:15:37 -0800211 int ret;
Joel Beckerbd3e7612008-02-01 12:14:57 -0800212
Joel Becker4670c462008-02-01 14:39:35 -0800213 status = dlmlock(conn->cc_lockspace, o2dlm_mode, &lksb->lksb_o2dlm,
214 o2dlm_flags, name, namelen,
Joel Becker8f2c9c12008-02-01 12:16:57 -0800215 o2dlm_lock_ast_wrapper, astarg,
216 o2dlm_blocking_ast_wrapper);
Joel Becker7431cd72008-02-01 12:15:37 -0800217 ret = dlm_status_to_errno(status);
218 return ret;
Joel Becker24ef1812008-01-29 17:37:32 -0800219}
220
Joel Becker553aa7e2008-02-01 14:51:03 -0800221int ocfs2_dlm_lock(struct ocfs2_cluster_connection *conn,
222 int mode,
223 union ocfs2_dlm_lksb *lksb,
224 u32 flags,
225 void *name,
226 unsigned int namelen,
227 void *astarg)
228{
229 BUG_ON(lproto == NULL);
230
231 return o2cb_dlm_lock(conn, mode, lksb, flags,
232 name, namelen, astarg);
233}
234
235static int o2cb_dlm_unlock(struct ocfs2_cluster_connection *conn,
236 union ocfs2_dlm_lksb *lksb,
237 u32 flags,
238 void *astarg)
Joel Becker24ef1812008-01-29 17:37:32 -0800239{
Joel Becker7431cd72008-02-01 12:15:37 -0800240 enum dlm_status status;
Joel Beckerbd3e7612008-02-01 12:14:57 -0800241 int o2dlm_flags = flags_to_o2dlm(flags);
Joel Becker7431cd72008-02-01 12:15:37 -0800242 int ret;
Joel Beckerbd3e7612008-02-01 12:14:57 -0800243
Joel Becker4670c462008-02-01 14:39:35 -0800244 status = dlmunlock(conn->cc_lockspace, &lksb->lksb_o2dlm,
245 o2dlm_flags, o2dlm_unlock_ast_wrapper, astarg);
Joel Becker7431cd72008-02-01 12:15:37 -0800246 ret = dlm_status_to_errno(status);
247 return ret;
Joel Becker24ef1812008-01-29 17:37:32 -0800248}
249
Joel Becker553aa7e2008-02-01 14:51:03 -0800250int ocfs2_dlm_unlock(struct ocfs2_cluster_connection *conn,
251 union ocfs2_dlm_lksb *lksb,
252 u32 flags,
253 void *astarg)
254{
255 BUG_ON(lproto == NULL);
256
257 return o2cb_dlm_unlock(conn, lksb, flags, astarg);
258}
259
260static int o2cb_dlm_lock_status(union ocfs2_dlm_lksb *lksb)
Joel Becker8f2c9c12008-02-01 12:16:57 -0800261{
262 return dlm_status_to_errno(lksb->lksb_o2dlm.status);
263}
264
Joel Becker553aa7e2008-02-01 14:51:03 -0800265int ocfs2_dlm_lock_status(union ocfs2_dlm_lksb *lksb)
266{
267 return o2cb_dlm_lock_status(lksb);
268}
269
Joel Becker8f2c9c12008-02-01 12:16:57 -0800270/*
271 * Why don't we cast to ocfs2_meta_lvb? The "clean" answer is that we
272 * don't cast at the glue level. The real answer is that the header
273 * ordering is nigh impossible.
274 */
Joel Becker553aa7e2008-02-01 14:51:03 -0800275static void *o2cb_dlm_lvb(union ocfs2_dlm_lksb *lksb)
Joel Becker8f2c9c12008-02-01 12:16:57 -0800276{
277 return (void *)(lksb->lksb_o2dlm.lvb);
278}
Joel Becker24ef1812008-01-29 17:37:32 -0800279
Joel Becker553aa7e2008-02-01 14:51:03 -0800280void *ocfs2_dlm_lvb(union ocfs2_dlm_lksb *lksb)
281{
282 return o2cb_dlm_lvb(lksb);
283}
284
285static void o2cb_dlm_dump_lksb(union ocfs2_dlm_lksb *lksb)
Joel Beckercf0acdc2008-01-29 16:59:55 -0800286{
287 dlm_print_one_lock(lksb->lksb_o2dlm.lockid);
288}
289
Joel Becker553aa7e2008-02-01 14:51:03 -0800290void ocfs2_dlm_dump_lksb(union ocfs2_dlm_lksb *lksb)
291{
292 o2cb_dlm_dump_lksb(lksb);
293}
294
Joel Becker4670c462008-02-01 14:39:35 -0800295/*
296 * Called from the dlm when it's about to evict a node. This is how the
297 * classic stack signals node death.
298 */
299static void o2dlm_eviction_cb(int node_num, void *data)
300{
301 struct ocfs2_cluster_connection *conn = data;
302
303 mlog(ML_NOTICE, "o2dlm has evicted node %d from group %.*s\n",
304 node_num, conn->cc_namelen, conn->cc_name);
305
306 conn->cc_recovery_handler(node_num, conn->cc_recovery_data);
307}
308
Joel Becker553aa7e2008-02-01 14:51:03 -0800309static int o2cb_cluster_connect(struct ocfs2_cluster_connection *conn)
310{
311 int rc = 0;
312 u32 dlm_key;
313 struct dlm_ctxt *dlm;
314 struct o2dlm_private *priv;
315 struct dlm_protocol_version dlm_version;
316
317 BUG_ON(conn == NULL);
318
319 /* for now we only have one cluster/node, make sure we see it
320 * in the heartbeat universe */
321 if (!o2hb_check_local_node_heartbeating()) {
322 rc = -EINVAL;
323 goto out;
324 }
325
326 priv = kzalloc(sizeof(struct o2dlm_private), GFP_KERNEL);
327 if (!priv) {
328 rc = -ENOMEM;
329 goto out_free;
330 }
331
332 /* This just fills the structure in. It is safe to pass conn. */
333 dlm_setup_eviction_cb(&priv->op_eviction_cb, o2dlm_eviction_cb,
334 conn);
335
336 conn->cc_private = priv;
337
338 /* used by the dlm code to make message headers unique, each
339 * node in this domain must agree on this. */
340 dlm_key = crc32_le(0, conn->cc_name, conn->cc_namelen);
341 dlm_version.pv_major = conn->cc_version.pv_major;
342 dlm_version.pv_minor = conn->cc_version.pv_minor;
343
344 dlm = dlm_register_domain(conn->cc_name, dlm_key, &dlm_version);
345 if (IS_ERR(dlm)) {
346 rc = PTR_ERR(dlm);
347 mlog_errno(rc);
348 goto out_free;
349 }
350
351 conn->cc_version.pv_major = dlm_version.pv_major;
352 conn->cc_version.pv_minor = dlm_version.pv_minor;
353 conn->cc_lockspace = dlm;
354
355 dlm_register_eviction_cb(dlm, &priv->op_eviction_cb);
356
357out_free:
358 if (rc && conn->cc_private)
359 kfree(conn->cc_private);
360
361out:
362 return rc;
363}
364
Joel Becker4670c462008-02-01 14:39:35 -0800365int ocfs2_cluster_connect(const char *group,
366 int grouplen,
367 void (*recovery_handler)(int node_num,
368 void *recovery_data),
369 void *recovery_data,
370 struct ocfs2_cluster_connection **conn)
371{
372 int rc = 0;
373 struct ocfs2_cluster_connection *new_conn;
Joel Becker4670c462008-02-01 14:39:35 -0800374
375 BUG_ON(group == NULL);
376 BUG_ON(conn == NULL);
377 BUG_ON(recovery_handler == NULL);
378
379 if (grouplen > GROUP_NAME_MAX) {
380 rc = -EINVAL;
381 goto out;
382 }
383
384 new_conn = kzalloc(sizeof(struct ocfs2_cluster_connection),
385 GFP_KERNEL);
386 if (!new_conn) {
387 rc = -ENOMEM;
388 goto out;
389 }
390
391 memcpy(new_conn->cc_name, group, grouplen);
392 new_conn->cc_namelen = grouplen;
393 new_conn->cc_recovery_handler = recovery_handler;
394 new_conn->cc_recovery_data = recovery_data;
395
396 /* Start the new connection at our maximum compatibility level */
397 new_conn->cc_version = lproto->lp_max_version;
398
Joel Becker553aa7e2008-02-01 14:51:03 -0800399 rc = o2cb_cluster_connect(new_conn);
400 if (rc) {
Joel Becker4670c462008-02-01 14:39:35 -0800401 mlog_errno(rc);
402 goto out_free;
403 }
404
Joel Becker4670c462008-02-01 14:39:35 -0800405 *conn = new_conn;
406
407out_free:
Joel Becker553aa7e2008-02-01 14:51:03 -0800408 if (rc)
Joel Becker4670c462008-02-01 14:39:35 -0800409 kfree(new_conn);
Joel Becker4670c462008-02-01 14:39:35 -0800410
411out:
412 return rc;
413}
414
Joel Becker6953b4c2008-01-29 16:59:56 -0800415
Joel Becker553aa7e2008-02-01 14:51:03 -0800416static int o2cb_cluster_disconnect(struct ocfs2_cluster_connection *conn)
Joel Becker4670c462008-02-01 14:39:35 -0800417{
418 struct dlm_ctxt *dlm = conn->cc_lockspace;
419 struct o2dlm_private *priv = conn->cc_private;
420
421 dlm_unregister_eviction_cb(&priv->op_eviction_cb);
Joel Becker553aa7e2008-02-01 14:51:03 -0800422 conn->cc_private = NULL;
Joel Becker4670c462008-02-01 14:39:35 -0800423 kfree(priv);
Joel Becker553aa7e2008-02-01 14:51:03 -0800424
425 dlm_unregister_domain(dlm);
426 conn->cc_lockspace = NULL;
Joel Becker4670c462008-02-01 14:39:35 -0800427
428 return 0;
429}
430
Joel Becker553aa7e2008-02-01 14:51:03 -0800431int ocfs2_cluster_disconnect(struct ocfs2_cluster_connection *conn)
432{
433 int ret;
434
435 BUG_ON(conn == NULL);
436
437 ret = o2cb_cluster_disconnect(conn);
438
439 /* XXX Should we free it anyway? */
440 if (!ret)
441 kfree(conn);
442
443 return ret;
444}
445
Joel Becker6953b4c2008-01-29 16:59:56 -0800446static void o2hb_stop(const char *group)
447{
448 int ret;
449 char *argv[5], *envp[3];
450
451 argv[0] = (char *)o2nm_get_hb_ctl_path();
452 argv[1] = "-K";
453 argv[2] = "-u";
454 argv[3] = (char *)group;
455 argv[4] = NULL;
456
457 mlog(0, "Run: %s %s %s %s\n", argv[0], argv[1], argv[2], argv[3]);
458
459 /* minimal command environment taken from cpu_run_sbin_hotplug */
460 envp[0] = "HOME=/";
461 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
462 envp[2] = NULL;
463
464 ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
465 if (ret < 0)
466 mlog_errno(ret);
467}
468
469/*
470 * Hangup is a hack for tools compatibility. Older ocfs2-tools software
471 * expects the filesystem to call "ocfs2_hb_ctl" during unmount. This
472 * happens regardless of whether the DLM got started, so we can't do it
473 * in ocfs2_cluster_disconnect(). We bring the o2hb_stop() function into
474 * the glue and provide a "hangup" API for super.c to call.
475 *
476 * Other stacks will eventually provide a NULL ->hangup() pointer.
477 */
Joel Becker553aa7e2008-02-01 14:51:03 -0800478static void o2cb_cluster_hangup(const char *group, int grouplen)
479{
480 o2hb_stop(group);
481}
482
Joel Becker6953b4c2008-01-29 16:59:56 -0800483void ocfs2_cluster_hangup(const char *group, int grouplen)
484{
485 BUG_ON(group == NULL);
486 BUG_ON(group[grouplen] != '\0');
487
Joel Becker553aa7e2008-02-01 14:51:03 -0800488 o2cb_cluster_hangup(group, grouplen);
Joel Becker6953b4c2008-01-29 16:59:56 -0800489}
490
Joel Becker553aa7e2008-02-01 14:51:03 -0800491static int o2cb_cluster_this_node(unsigned int *node)
Joel Becker19fdb622008-01-30 15:38:24 -0800492{
493 int node_num;
494
495 node_num = o2nm_this_node();
496 if (node_num == O2NM_INVALID_NODE_NUM)
497 return -ENOENT;
498
499 if (node_num >= O2NM_MAX_NODES)
500 return -EOVERFLOW;
501
502 *node = node_num;
503 return 0;
504}
505
Joel Becker553aa7e2008-02-01 14:51:03 -0800506int ocfs2_cluster_this_node(unsigned int *node)
507{
508 return o2cb_cluster_this_node(node);
509}
510
Joel Becker63e0c482008-01-30 16:58:36 -0800511void ocfs2_stack_glue_set_locking_protocol(struct ocfs2_locking_protocol *proto)
Joel Becker24ef1812008-01-29 17:37:32 -0800512{
Joel Becker63e0c482008-01-30 16:58:36 -0800513 BUG_ON(proto != NULL);
Joel Becker24ef1812008-01-29 17:37:32 -0800514
515 lproto = proto;
516}
517