]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - fs/ocfs2/stackglue.c
ocfs2: Abstract out a debugging function for underlying dlms.
[linux-2.6-omap-h63xx.git] / fs / ocfs2 / stackglue.c
1 /* -*- 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
21 #include <linux/slab.h>
22 #include <linux/crc32.h>
23 #include <linux/kmod.h>
24
25 /* Needed for AOP_TRUNCATED_PAGE in mlog_errno() */
26 #include <linux/fs.h>
27
28 #include "cluster/masklog.h"
29 #include "cluster/nodemanager.h"
30 #include "cluster/heartbeat.h"
31
32 #include "stackglue.h"
33
34 static struct ocfs2_locking_protocol *lproto;
35
36 struct o2dlm_private {
37         struct dlm_eviction_cb op_eviction_cb;
38 };
39
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)
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         }
74 static 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
95 /*
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:     -EBUSY
108  * DLM_CANCEL:          -DLM_ECANCEL
109  */
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,
154 };
155
156 static 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
163 static void o2dlm_lock_ast_wrapper(void *astarg)
164 {
165         BUG_ON(lproto == NULL);
166
167         lproto->lp_lock_ast(astarg);
168 }
169
170 static 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
177 static void o2dlm_unlock_ast_wrapper(void *astarg, enum dlm_status status)
178 {
179         int error = dlm_status_to_errno(status);
180
181         BUG_ON(lproto == NULL);
182
183         /*
184          * 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
188          * DLM_CANCELGRANT.
189          *
190          * 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.
193          */
194         if (status == DLM_CANCELGRANT)
195                 return;
196
197         lproto->lp_unlock_ast(astarg, error);
198 }
199
200 int ocfs2_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)
207 {
208         enum dlm_status status;
209         int o2dlm_mode = mode_to_o2dlm(mode);
210         int o2dlm_flags = flags_to_o2dlm(flags);
211         int ret;
212
213         BUG_ON(lproto == NULL);
214
215         status = dlmlock(conn->cc_lockspace, o2dlm_mode, &lksb->lksb_o2dlm,
216                          o2dlm_flags, name, namelen,
217                          o2dlm_lock_ast_wrapper, astarg,
218                          o2dlm_blocking_ast_wrapper);
219         ret = dlm_status_to_errno(status);
220         return ret;
221 }
222
223 int ocfs2_dlm_unlock(struct ocfs2_cluster_connection *conn,
224                      union ocfs2_dlm_lksb *lksb,
225                      u32 flags,
226                      void *astarg)
227 {
228         enum dlm_status status;
229         int o2dlm_flags = flags_to_o2dlm(flags);
230         int ret;
231
232         BUG_ON(lproto == NULL);
233
234         status = dlmunlock(conn->cc_lockspace, &lksb->lksb_o2dlm,
235                            o2dlm_flags, o2dlm_unlock_ast_wrapper, astarg);
236         ret = dlm_status_to_errno(status);
237         return ret;
238 }
239
240 int ocfs2_dlm_lock_status(union ocfs2_dlm_lksb *lksb)
241 {
242         return dlm_status_to_errno(lksb->lksb_o2dlm.status);
243 }
244
245 /*
246  * Why don't we cast to ocfs2_meta_lvb?  The "clean" answer is that we
247  * don't cast at the glue level.  The real answer is that the header
248  * ordering is nigh impossible.
249  */
250 void *ocfs2_dlm_lvb(union ocfs2_dlm_lksb *lksb)
251 {
252         return (void *)(lksb->lksb_o2dlm.lvb);
253 }
254
255 void ocfs2_dlm_dump_lksb(union ocfs2_dlm_lksb *lksb)
256 {
257         dlm_print_one_lock(lksb->lksb_o2dlm.lockid);
258 }
259
260 /*
261  * Called from the dlm when it's about to evict a node. This is how the
262  * classic stack signals node death.
263  */
264 static void o2dlm_eviction_cb(int node_num, void *data)
265 {
266         struct ocfs2_cluster_connection *conn = data;
267
268         mlog(ML_NOTICE, "o2dlm has evicted node %d from group %.*s\n",
269              node_num, conn->cc_namelen, conn->cc_name);
270
271         conn->cc_recovery_handler(node_num, conn->cc_recovery_data);
272 }
273
274 int ocfs2_cluster_connect(const char *group,
275                           int grouplen,
276                           void (*recovery_handler)(int node_num,
277                                                    void *recovery_data),
278                           void *recovery_data,
279                           struct ocfs2_cluster_connection **conn)
280 {
281         int rc = 0;
282         struct ocfs2_cluster_connection *new_conn;
283         u32 dlm_key;
284         struct dlm_ctxt *dlm;
285         struct o2dlm_private *priv;
286         struct dlm_protocol_version dlm_version;
287
288         BUG_ON(group == NULL);
289         BUG_ON(conn == NULL);
290         BUG_ON(recovery_handler == NULL);
291
292         if (grouplen > GROUP_NAME_MAX) {
293                 rc = -EINVAL;
294                 goto out;
295         }
296
297         /* for now we only have one cluster/node, make sure we see it
298          * in the heartbeat universe */
299         if (!o2hb_check_local_node_heartbeating()) {
300                 rc = -EINVAL;
301                 goto out;
302         }
303
304         new_conn = kzalloc(sizeof(struct ocfs2_cluster_connection),
305                            GFP_KERNEL);
306         if (!new_conn) {
307                 rc = -ENOMEM;
308                 goto out;
309         }
310
311         memcpy(new_conn->cc_name, group, grouplen);
312         new_conn->cc_namelen = grouplen;
313         new_conn->cc_recovery_handler = recovery_handler;
314         new_conn->cc_recovery_data = recovery_data;
315
316         /* Start the new connection at our maximum compatibility level */
317         new_conn->cc_version = lproto->lp_max_version;
318
319         priv = kzalloc(sizeof(struct o2dlm_private), GFP_KERNEL);
320         if (!priv) {
321                 rc = -ENOMEM;
322                 goto out_free;
323         }
324
325         /* This just fills the structure in.  It is safe to use new_conn. */
326         dlm_setup_eviction_cb(&priv->op_eviction_cb, o2dlm_eviction_cb,
327                               new_conn);
328
329         new_conn->cc_private = priv;
330
331         /* used by the dlm code to make message headers unique, each
332          * node in this domain must agree on this. */
333         dlm_key = crc32_le(0, group, grouplen);
334         dlm_version.pv_major = new_conn->cc_version.pv_major;
335         dlm_version.pv_minor = new_conn->cc_version.pv_minor;
336
337         dlm = dlm_register_domain(group, dlm_key, &dlm_version);
338         if (IS_ERR(dlm)) {
339                 rc = PTR_ERR(dlm);
340                 mlog_errno(rc);
341                 goto out_free;
342         }
343
344         new_conn->cc_version.pv_major = dlm_version.pv_major;
345         new_conn->cc_version.pv_minor = dlm_version.pv_minor;
346         new_conn->cc_lockspace = dlm;
347
348         dlm_register_eviction_cb(dlm, &priv->op_eviction_cb);
349
350         *conn = new_conn;
351
352 out_free:
353         if (rc) {
354                 kfree(new_conn->cc_private);
355                 kfree(new_conn);
356         }
357
358 out:
359         return rc;
360 }
361
362
363 int ocfs2_cluster_disconnect(struct ocfs2_cluster_connection *conn)
364 {
365         struct dlm_ctxt *dlm = conn->cc_lockspace;
366         struct o2dlm_private *priv = conn->cc_private;
367
368         dlm_unregister_eviction_cb(&priv->op_eviction_cb);
369         dlm_unregister_domain(dlm);
370
371         kfree(priv);
372         kfree(conn);
373
374         return 0;
375 }
376
377 static void o2hb_stop(const char *group)
378 {
379         int ret;
380         char *argv[5], *envp[3];
381
382         argv[0] = (char *)o2nm_get_hb_ctl_path();
383         argv[1] = "-K";
384         argv[2] = "-u";
385         argv[3] = (char *)group;
386         argv[4] = NULL;
387
388         mlog(0, "Run: %s %s %s %s\n", argv[0], argv[1], argv[2], argv[3]);
389
390         /* minimal command environment taken from cpu_run_sbin_hotplug */
391         envp[0] = "HOME=/";
392         envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
393         envp[2] = NULL;
394
395         ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
396         if (ret < 0)
397                 mlog_errno(ret);
398 }
399
400 /*
401  * Hangup is a hack for tools compatibility.  Older ocfs2-tools software
402  * expects the filesystem to call "ocfs2_hb_ctl" during unmount.  This
403  * happens regardless of whether the DLM got started, so we can't do it
404  * in ocfs2_cluster_disconnect().  We bring the o2hb_stop() function into
405  * the glue and provide a "hangup" API for super.c to call.
406  *
407  * Other stacks will eventually provide a NULL ->hangup() pointer.
408  */
409 void ocfs2_cluster_hangup(const char *group, int grouplen)
410 {
411         BUG_ON(group == NULL);
412         BUG_ON(group[grouplen] != '\0');
413
414         o2hb_stop(group);
415 }
416
417 int ocfs2_cluster_this_node(unsigned int *node)
418 {
419         int node_num;
420
421         node_num = o2nm_this_node();
422         if (node_num == O2NM_INVALID_NODE_NUM)
423                 return -ENOENT;
424
425         if (node_num >= O2NM_MAX_NODES)
426                 return -EOVERFLOW;
427
428         *node = node_num;
429         return 0;
430 }
431
432 void o2cb_get_stack(struct ocfs2_locking_protocol *proto)
433 {
434         BUG_ON(proto == NULL);
435
436         lproto = proto;
437 }
438
439 void o2cb_put_stack(void)
440 {
441         lproto = NULL;
442 }