]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - fs/ocfs2/stackglue.c
ocfs2: Abstract out node number queries.
[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
24 /* Needed for AOP_TRUNCATED_PAGE in mlog_errno() */
25 #include <linux/fs.h>
26
27 #include "cluster/masklog.h"
28 #include "cluster/nodemanager.h"
29
30 #include "stackglue.h"
31
32 static struct ocfs2_locking_protocol *lproto;
33
34 struct o2dlm_private {
35         struct dlm_eviction_cb op_eviction_cb;
36 };
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:     -DLM_ECANCEL
106  * DLM_CANCEL:          -DLM_EUNLOCK
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]                   = -EBUSY,
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]               = -DLM_ECANCEL, /* 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_EUNLOCK, /* 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 static int dlm_status_to_errno(enum dlm_status status)
154 {
155         BUG_ON(status > (sizeof(status_map) / sizeof(status_map[0])));
156
157         return status_map[status];
158 }
159
160 static void o2dlm_lock_ast_wrapper(void *astarg)
161 {
162         BUG_ON(lproto == NULL);
163
164         lproto->lp_lock_ast(astarg);
165 }
166
167 static void o2dlm_blocking_ast_wrapper(void *astarg, int level)
168 {
169         BUG_ON(lproto == NULL);
170
171         lproto->lp_blocking_ast(astarg, level);
172 }
173
174 static void o2dlm_unlock_ast_wrapper(void *astarg, enum dlm_status status)
175 {
176         int error;
177
178         BUG_ON(lproto == NULL);
179
180         /*
181          * XXX: CANCEL values are sketchy.
182          *
183          * Currently we have preserved the o2dlm paradigm.  You can get
184          * unlock_ast() whether the cancel succeded or not.
185          *
186          * First, we're going to pass DLM_EUNLOCK just like fs/dlm does for
187          * successful unlocks.  That is a clean behavior.
188          *
189          * In o2dlm, you can get both the lock_ast() for the lock being
190          * granted and the unlock_ast() for the CANCEL failing.  A
191          * successful cancel sends DLM_NORMAL here.  If the
192          * lock grant happened before the cancel arrived, you get
193          * DLM_CANCELGRANT.  For now, we'll use DLM_ECANCEL to signify
194          * CANCELGRANT - the CANCEL was supposed to happen but didn't.  We
195          * can then use DLM_EUNLOCK to signify a successful CANCEL -
196          * effectively, the CANCEL caused the lock to roll back.
197          *
198          * In the future, we will likely move the o2dlm to send only one
199          * ast - either unlock_ast() for a successful CANCEL or lock_ast()
200          * when the grant succeeds.  At that point, we'll send DLM_ECANCEL
201          * for all cancel results (CANCELGRANT will no longer exist).
202          */
203         error = dlm_status_to_errno(status);
204
205         /* Successful unlock is DLM_EUNLOCK */
206         if (!error)
207                 error = -DLM_EUNLOCK;
208
209         lproto->lp_unlock_ast(astarg, error);
210 }
211
212 int ocfs2_dlm_lock(struct ocfs2_cluster_connection *conn,
213                    int mode,
214                    union ocfs2_dlm_lksb *lksb,
215                    u32 flags,
216                    void *name,
217                    unsigned int namelen,
218                    void *astarg)
219 {
220         enum dlm_status status;
221         int o2dlm_mode = mode_to_o2dlm(mode);
222         int o2dlm_flags = flags_to_o2dlm(flags);
223         int ret;
224
225         BUG_ON(lproto == NULL);
226
227         status = dlmlock(conn->cc_lockspace, o2dlm_mode, &lksb->lksb_o2dlm,
228                          o2dlm_flags, name, namelen,
229                          o2dlm_lock_ast_wrapper, astarg,
230                          o2dlm_blocking_ast_wrapper);
231         ret = dlm_status_to_errno(status);
232         return ret;
233 }
234
235 int ocfs2_dlm_unlock(struct ocfs2_cluster_connection *conn,
236                      union ocfs2_dlm_lksb *lksb,
237                      u32 flags,
238                      void *astarg)
239 {
240         enum dlm_status status;
241         int o2dlm_flags = flags_to_o2dlm(flags);
242         int ret;
243
244         BUG_ON(lproto == NULL);
245
246         status = dlmunlock(conn->cc_lockspace, &lksb->lksb_o2dlm,
247                            o2dlm_flags, o2dlm_unlock_ast_wrapper, astarg);
248         ret = dlm_status_to_errno(status);
249         return ret;
250 }
251
252 int ocfs2_dlm_lock_status(union ocfs2_dlm_lksb *lksb)
253 {
254         return dlm_status_to_errno(lksb->lksb_o2dlm.status);
255 }
256
257 /*
258  * Why don't we cast to ocfs2_meta_lvb?  The "clean" answer is that we
259  * don't cast at the glue level.  The real answer is that the header
260  * ordering is nigh impossible.
261  */
262 void *ocfs2_dlm_lvb(union ocfs2_dlm_lksb *lksb)
263 {
264         return (void *)(lksb->lksb_o2dlm.lvb);
265 }
266
267 /*
268  * Called from the dlm when it's about to evict a node. This is how the
269  * classic stack signals node death.
270  */
271 static void o2dlm_eviction_cb(int node_num, void *data)
272 {
273         struct ocfs2_cluster_connection *conn = data;
274
275         mlog(ML_NOTICE, "o2dlm has evicted node %d from group %.*s\n",
276              node_num, conn->cc_namelen, conn->cc_name);
277
278         conn->cc_recovery_handler(node_num, conn->cc_recovery_data);
279 }
280
281 int ocfs2_cluster_connect(const char *group,
282                           int grouplen,
283                           void (*recovery_handler)(int node_num,
284                                                    void *recovery_data),
285                           void *recovery_data,
286                           struct ocfs2_cluster_connection **conn)
287 {
288         int rc = 0;
289         struct ocfs2_cluster_connection *new_conn;
290         u32 dlm_key;
291         struct dlm_ctxt *dlm;
292         struct o2dlm_private *priv;
293         struct dlm_protocol_version dlm_version;
294
295         BUG_ON(group == NULL);
296         BUG_ON(conn == NULL);
297         BUG_ON(recovery_handler == NULL);
298
299         if (grouplen > GROUP_NAME_MAX) {
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 int ocfs2_cluster_disconnect(struct ocfs2_cluster_connection *conn)
363 {
364         struct dlm_ctxt *dlm = conn->cc_lockspace;
365         struct o2dlm_private *priv = conn->cc_private;
366
367         dlm_unregister_eviction_cb(&priv->op_eviction_cb);
368         dlm_unregister_domain(dlm);
369
370         kfree(priv);
371         kfree(conn);
372
373         return 0;
374 }
375
376 int ocfs2_cluster_this_node(unsigned int *node)
377 {
378         int node_num;
379
380         node_num = o2nm_this_node();
381         if (node_num == O2NM_INVALID_NODE_NUM)
382                 return -ENOENT;
383
384         if (node_num >= O2NM_MAX_NODES)
385                 return -EOVERFLOW;
386
387         *node = node_num;
388         return 0;
389 }
390
391 void o2cb_get_stack(struct ocfs2_locking_protocol *proto)
392 {
393         BUG_ON(proto == NULL);
394
395         lproto = proto;
396 }
397
398 void o2cb_put_stack(void)
399 {
400         lproto = NULL;
401 }