ocfs2: Remove CANCELGRANT from the view of dlmglue.
[linux-3.10.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 /*
256  * Called from the dlm when it's about to evict a node. This is how the
257  * classic stack signals node death.
258  */
259 static void o2dlm_eviction_cb(int node_num, void *data)
260 {
261         struct ocfs2_cluster_connection *conn = data;
262
263         mlog(ML_NOTICE, "o2dlm has evicted node %d from group %.*s\n",
264              node_num, conn->cc_namelen, conn->cc_name);
265
266         conn->cc_recovery_handler(node_num, conn->cc_recovery_data);
267 }
268
269 int ocfs2_cluster_connect(const char *group,
270                           int grouplen,
271                           void (*recovery_handler)(int node_num,
272                                                    void *recovery_data),
273                           void *recovery_data,
274                           struct ocfs2_cluster_connection **conn)
275 {
276         int rc = 0;
277         struct ocfs2_cluster_connection *new_conn;
278         u32 dlm_key;
279         struct dlm_ctxt *dlm;
280         struct o2dlm_private *priv;
281         struct dlm_protocol_version dlm_version;
282
283         BUG_ON(group == NULL);
284         BUG_ON(conn == NULL);
285         BUG_ON(recovery_handler == NULL);
286
287         if (grouplen > GROUP_NAME_MAX) {
288                 rc = -EINVAL;
289                 goto out;
290         }
291
292         /* for now we only have one cluster/node, make sure we see it
293          * in the heartbeat universe */
294         if (!o2hb_check_local_node_heartbeating()) {
295                 rc = -EINVAL;
296                 goto out;
297         }
298
299         new_conn = kzalloc(sizeof(struct ocfs2_cluster_connection),
300                            GFP_KERNEL);
301         if (!new_conn) {
302                 rc = -ENOMEM;
303                 goto out;
304         }
305
306         memcpy(new_conn->cc_name, group, grouplen);
307         new_conn->cc_namelen = grouplen;
308         new_conn->cc_recovery_handler = recovery_handler;
309         new_conn->cc_recovery_data = recovery_data;
310
311         /* Start the new connection at our maximum compatibility level */
312         new_conn->cc_version = lproto->lp_max_version;
313
314         priv = kzalloc(sizeof(struct o2dlm_private), GFP_KERNEL);
315         if (!priv) {
316                 rc = -ENOMEM;
317                 goto out_free;
318         }
319
320         /* This just fills the structure in.  It is safe to use new_conn. */
321         dlm_setup_eviction_cb(&priv->op_eviction_cb, o2dlm_eviction_cb,
322                               new_conn);
323
324         new_conn->cc_private = priv;
325
326         /* used by the dlm code to make message headers unique, each
327          * node in this domain must agree on this. */
328         dlm_key = crc32_le(0, group, grouplen);
329         dlm_version.pv_major = new_conn->cc_version.pv_major;
330         dlm_version.pv_minor = new_conn->cc_version.pv_minor;
331
332         dlm = dlm_register_domain(group, dlm_key, &dlm_version);
333         if (IS_ERR(dlm)) {
334                 rc = PTR_ERR(dlm);
335                 mlog_errno(rc);
336                 goto out_free;
337         }
338
339         new_conn->cc_version.pv_major = dlm_version.pv_major;
340         new_conn->cc_version.pv_minor = dlm_version.pv_minor;
341         new_conn->cc_lockspace = dlm;
342
343         dlm_register_eviction_cb(dlm, &priv->op_eviction_cb);
344
345         *conn = new_conn;
346
347 out_free:
348         if (rc) {
349                 kfree(new_conn->cc_private);
350                 kfree(new_conn);
351         }
352
353 out:
354         return rc;
355 }
356
357
358 int ocfs2_cluster_disconnect(struct ocfs2_cluster_connection *conn)
359 {
360         struct dlm_ctxt *dlm = conn->cc_lockspace;
361         struct o2dlm_private *priv = conn->cc_private;
362
363         dlm_unregister_eviction_cb(&priv->op_eviction_cb);
364         dlm_unregister_domain(dlm);
365
366         kfree(priv);
367         kfree(conn);
368
369         return 0;
370 }
371
372 static void o2hb_stop(const char *group)
373 {
374         int ret;
375         char *argv[5], *envp[3];
376
377         argv[0] = (char *)o2nm_get_hb_ctl_path();
378         argv[1] = "-K";
379         argv[2] = "-u";
380         argv[3] = (char *)group;
381         argv[4] = NULL;
382
383         mlog(0, "Run: %s %s %s %s\n", argv[0], argv[1], argv[2], argv[3]);
384
385         /* minimal command environment taken from cpu_run_sbin_hotplug */
386         envp[0] = "HOME=/";
387         envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
388         envp[2] = NULL;
389
390         ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
391         if (ret < 0)
392                 mlog_errno(ret);
393 }
394
395 /*
396  * Hangup is a hack for tools compatibility.  Older ocfs2-tools software
397  * expects the filesystem to call "ocfs2_hb_ctl" during unmount.  This
398  * happens regardless of whether the DLM got started, so we can't do it
399  * in ocfs2_cluster_disconnect().  We bring the o2hb_stop() function into
400  * the glue and provide a "hangup" API for super.c to call.
401  *
402  * Other stacks will eventually provide a NULL ->hangup() pointer.
403  */
404 void ocfs2_cluster_hangup(const char *group, int grouplen)
405 {
406         BUG_ON(group == NULL);
407         BUG_ON(group[grouplen] != '\0');
408
409         o2hb_stop(group);
410 }
411
412 int ocfs2_cluster_this_node(unsigned int *node)
413 {
414         int node_num;
415
416         node_num = o2nm_this_node();
417         if (node_num == O2NM_INVALID_NODE_NUM)
418                 return -ENOENT;
419
420         if (node_num >= O2NM_MAX_NODES)
421                 return -EOVERFLOW;
422
423         *node = node_num;
424         return 0;
425 }
426
427 void o2cb_get_stack(struct ocfs2_locking_protocol *proto)
428 {
429         BUG_ON(proto == NULL);
430
431         lproto = proto;
432 }
433
434 void o2cb_put_stack(void)
435 {
436         lproto = NULL;
437 }