1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Code which implements an OCFS2 specific interface to underlying
9 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
21 #include "cluster/masklog.h"
22 #include "stackglue.h"
24 static struct ocfs2_locking_protocol
*lproto
;
26 /* These should be identical */
27 #if (DLM_LOCK_IV != LKM_IVMODE)
28 # error Lock modes do not match
30 #if (DLM_LOCK_NL != LKM_NLMODE)
31 # error Lock modes do not match
33 #if (DLM_LOCK_CR != LKM_CRMODE)
34 # error Lock modes do not match
36 #if (DLM_LOCK_CW != LKM_CWMODE)
37 # error Lock modes do not match
39 #if (DLM_LOCK_PR != LKM_PRMODE)
40 # error Lock modes do not match
42 #if (DLM_LOCK_PW != LKM_PWMODE)
43 # error Lock modes do not match
45 #if (DLM_LOCK_EX != LKM_EXMODE)
46 # error Lock modes do not match
48 static inline int mode_to_o2dlm(int mode
)
50 BUG_ON(mode
> LKM_MAXMODE
);
55 #define map_flag(_generic, _o2dlm) \
56 if (flags & (_generic)) { \
57 flags &= ~(_generic); \
58 o2dlm_flags |= (_o2dlm); \
60 static int flags_to_o2dlm(u32 flags
)
64 map_flag(DLM_LKF_NOQUEUE
, LKM_NOQUEUE
);
65 map_flag(DLM_LKF_CANCEL
, LKM_CANCEL
);
66 map_flag(DLM_LKF_CONVERT
, LKM_CONVERT
);
67 map_flag(DLM_LKF_VALBLK
, LKM_VALBLK
);
68 map_flag(DLM_LKF_IVVALBLK
, LKM_INVVALBLK
);
69 map_flag(DLM_LKF_ORPHAN
, LKM_ORPHAN
);
70 map_flag(DLM_LKF_FORCEUNLOCK
, LKM_FORCE
);
71 map_flag(DLM_LKF_TIMEOUT
, LKM_TIMEOUT
);
72 map_flag(DLM_LKF_LOCAL
, LKM_LOCAL
);
74 /* map_flag() should have cleared every flag passed in */
82 * Map an o2dlm status to standard errno values.
84 * o2dlm only uses a handful of these, and returns even fewer to the
85 * caller. Still, we try to assign sane values to each error.
87 * The following value pairs have special meanings to dlmglue, thus
88 * the right hand side needs to stay unique - never duplicate the
89 * mapping elsewhere in the table!
92 * DLM_NOTQUEUED: -EAGAIN
93 * DLM_CANCELGRANT: -DLM_ECANCEL
94 * DLM_CANCEL: -DLM_EUNLOCK
96 /* Keep in sync with dlmapi.h */
97 static int status_map
[] = {
98 [DLM_NORMAL
] = 0, /* Success */
99 [DLM_GRANTED
] = -EINVAL
,
100 [DLM_DENIED
] = -EACCES
,
101 [DLM_DENIED_NOLOCKS
] = -EACCES
,
102 [DLM_WORKING
] = -EBUSY
,
103 [DLM_BLOCKED
] = -EINVAL
,
104 [DLM_BLOCKED_ORPHAN
] = -EINVAL
,
105 [DLM_DENIED_GRACE_PERIOD
] = -EACCES
,
106 [DLM_SYSERR
] = -ENOMEM
, /* It is what it is */
107 [DLM_NOSUPPORT
] = -EPROTO
,
108 [DLM_CANCELGRANT
] = -DLM_ECANCEL
, /* Cancel after grant */
109 [DLM_IVLOCKID
] = -EINVAL
,
110 [DLM_SYNC
] = -EINVAL
,
111 [DLM_BADTYPE
] = -EINVAL
,
112 [DLM_BADRESOURCE
] = -EINVAL
,
113 [DLM_MAXHANDLES
] = -ENOMEM
,
114 [DLM_NOCLINFO
] = -EINVAL
,
115 [DLM_NOLOCKMGR
] = -EINVAL
,
116 [DLM_NOPURGED
] = -EINVAL
,
117 [DLM_BADARGS
] = -EINVAL
,
118 [DLM_VOID
] = -EINVAL
,
119 [DLM_NOTQUEUED
] = -EAGAIN
, /* Trylock failed */
120 [DLM_IVBUFLEN
] = -EINVAL
,
121 [DLM_CVTUNGRANT
] = -EPERM
,
122 [DLM_BADPARAM
] = -EINVAL
,
123 [DLM_VALNOTVALID
] = -EINVAL
,
124 [DLM_REJECTED
] = -EPERM
,
125 [DLM_ABORT
] = -EINVAL
,
126 [DLM_CANCEL
] = -DLM_EUNLOCK
, /* Successful cancel */
127 [DLM_IVRESHANDLE
] = -EINVAL
,
128 [DLM_DEADLOCK
] = -EDEADLK
,
129 [DLM_DENIED_NOASTS
] = -EINVAL
,
130 [DLM_FORWARD
] = -EINVAL
,
131 [DLM_TIMEOUT
] = -ETIMEDOUT
,
132 [DLM_IVGROUPID
] = -EINVAL
,
133 [DLM_VERS_CONFLICT
] = -EOPNOTSUPP
,
134 [DLM_BAD_DEVICE_PATH
] = -ENOENT
,
135 [DLM_NO_DEVICE_PERMISSION
] = -EPERM
,
136 [DLM_NO_CONTROL_DEVICE
] = -ENOENT
,
137 [DLM_RECOVERING
] = -ENOTCONN
,
138 [DLM_MIGRATING
] = -ERESTART
,
139 [DLM_MAXSTATS
] = -EINVAL
,
141 static int dlm_status_to_errno(enum dlm_status status
)
143 BUG_ON(status
> (sizeof(status_map
) / sizeof(status_map
[0])));
145 return status_map
[status
];
148 static void o2dlm_lock_ast_wrapper(void *astarg
)
150 BUG_ON(lproto
== NULL
);
152 lproto
->lp_lock_ast(astarg
);
155 static void o2dlm_blocking_ast_wrapper(void *astarg
, int level
)
157 BUG_ON(lproto
== NULL
);
159 lproto
->lp_blocking_ast(astarg
, level
);
162 static void o2dlm_unlock_ast_wrapper(void *astarg
, enum dlm_status status
)
166 BUG_ON(lproto
== NULL
);
169 * XXX: CANCEL values are sketchy.
171 * Currently we have preserved the o2dlm paradigm. You can get
172 * unlock_ast() whether the cancel succeded or not.
174 * First, we're going to pass DLM_EUNLOCK just like fs/dlm does for
175 * successful unlocks. That is a clean behavior.
177 * In o2dlm, you can get both the lock_ast() for the lock being
178 * granted and the unlock_ast() for the CANCEL failing. A
179 * successful cancel sends DLM_NORMAL here. If the
180 * lock grant happened before the cancel arrived, you get
181 * DLM_CANCELGRANT. For now, we'll use DLM_ECANCEL to signify
182 * CANCELGRANT - the CANCEL was supposed to happen but didn't. We
183 * can then use DLM_EUNLOCK to signify a successful CANCEL -
184 * effectively, the CANCEL caused the lock to roll back.
186 * In the future, we will likely move the o2dlm to send only one
187 * ast - either unlock_ast() for a successful CANCEL or lock_ast()
188 * when the grant succeeds. At that point, we'll send DLM_ECANCEL
189 * for all cancel results (CANCELGRANT will no longer exist).
191 error
= dlm_status_to_errno(status
);
193 /* Successful unlock is DLM_EUNLOCK */
195 error
= -DLM_EUNLOCK
;
197 lproto
->lp_unlock_ast(astarg
, error
);
200 int ocfs2_dlm_lock(struct dlm_ctxt
*dlm
,
202 union ocfs2_dlm_lksb
*lksb
,
205 unsigned int namelen
,
208 enum dlm_status status
;
209 int o2dlm_mode
= mode_to_o2dlm(mode
);
210 int o2dlm_flags
= flags_to_o2dlm(flags
);
213 BUG_ON(lproto
== NULL
);
215 status
= dlmlock(dlm
, o2dlm_mode
, &lksb
->lksb_o2dlm
, o2dlm_flags
,
217 o2dlm_lock_ast_wrapper
, astarg
,
218 o2dlm_blocking_ast_wrapper
);
219 ret
= dlm_status_to_errno(status
);
223 int ocfs2_dlm_unlock(struct dlm_ctxt
*dlm
,
224 union ocfs2_dlm_lksb
*lksb
,
228 enum dlm_status status
;
229 int o2dlm_flags
= flags_to_o2dlm(flags
);
232 BUG_ON(lproto
== NULL
);
234 status
= dlmunlock(dlm
, &lksb
->lksb_o2dlm
, o2dlm_flags
,
235 o2dlm_unlock_ast_wrapper
, astarg
);
236 ret
= dlm_status_to_errno(status
);
240 int ocfs2_dlm_lock_status(union ocfs2_dlm_lksb
*lksb
)
242 return dlm_status_to_errno(lksb
->lksb_o2dlm
.status
);
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.
250 void *ocfs2_dlm_lvb(union ocfs2_dlm_lksb
*lksb
)
252 return (void *)(lksb
->lksb_o2dlm
.lvb
);
255 void o2cb_get_stack(struct ocfs2_locking_protocol
*proto
)
257 BUG_ON(proto
== NULL
);
262 void o2cb_put_stack(void)