2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License version 2.
11 #include <linux/dlm.h>
12 #include <linux/types.h>
13 #include <linux/gfs2_ondisk.h>
20 static void gdlm_ast(void *arg
)
22 struct gfs2_glock
*gl
= arg
;
23 unsigned ret
= gl
->gl_state
;
24 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
26 BUG_ON(gl
->gl_lksb
.sb_flags
& DLM_SBF_DEMOTED
);
28 if (gl
->gl_lksb
.sb_flags
& DLM_SBF_VALNOTVALID
)
29 memset(gl
->gl_lvb
, 0, GDLM_LVB_SIZE
);
31 switch (gl
->gl_lksb
.sb_status
) {
32 case -DLM_EUNLOCK
: /* Unlocked, so glock can be freed */
33 kmem_cache_free(gfs2_glock_cachep
, gl
);
34 if (atomic_dec_and_test(&sdp
->sd_glock_disposal
))
35 wake_up(&sdp
->sd_glock_wait
);
37 case -DLM_ECANCEL
: /* Cancel while getting lock */
38 ret
|= LM_OUT_CANCELED
;
40 case -EAGAIN
: /* Try lock fails */
42 case -EINVAL
: /* Invalid */
43 case -ENOMEM
: /* Out of memory */
48 default: /* Something unexpected */
53 if (gl
->gl_lksb
.sb_flags
& DLM_SBF_ALTMODE
) {
54 if (gl
->gl_req
== LM_ST_SHARED
)
56 else if (gl
->gl_req
== LM_ST_DEFERRED
)
62 set_bit(GLF_INITIAL
, &gl
->gl_flags
);
63 gfs2_glock_complete(gl
, ret
);
66 if (!test_bit(GLF_INITIAL
, &gl
->gl_flags
))
67 gl
->gl_lksb
.sb_lkid
= 0;
68 gfs2_glock_complete(gl
, ret
);
71 static void gdlm_bast(void *arg
, int mode
)
73 struct gfs2_glock
*gl
= arg
;
77 gfs2_glock_cb(gl
, LM_ST_UNLOCKED
);
80 gfs2_glock_cb(gl
, LM_ST_DEFERRED
);
83 gfs2_glock_cb(gl
, LM_ST_SHARED
);
86 printk(KERN_ERR
"unknown bast mode %d", mode
);
91 /* convert gfs lock-state to dlm lock-mode */
93 static int make_mode(const unsigned int lmstate
)
105 printk(KERN_ERR
"unknown LM state %d", lmstate
);
110 static u32
make_flags(const u32 lkid
, const unsigned int gfs_flags
,
115 if (gfs_flags
& LM_FLAG_TRY
)
116 lkf
|= DLM_LKF_NOQUEUE
;
118 if (gfs_flags
& LM_FLAG_TRY_1CB
) {
119 lkf
|= DLM_LKF_NOQUEUE
;
120 lkf
|= DLM_LKF_NOQUEUEBAST
;
123 if (gfs_flags
& LM_FLAG_PRIORITY
) {
124 lkf
|= DLM_LKF_NOORDER
;
125 lkf
|= DLM_LKF_HEADQUE
;
128 if (gfs_flags
& LM_FLAG_ANY
) {
129 if (req
== DLM_LOCK_PR
)
130 lkf
|= DLM_LKF_ALTCW
;
131 else if (req
== DLM_LOCK_CW
)
132 lkf
|= DLM_LKF_ALTPR
;
138 lkf
|= DLM_LKF_CONVERT
;
140 lkf
|= DLM_LKF_VALBLK
;
145 static unsigned int gdlm_lock(struct gfs2_glock
*gl
,
146 unsigned int req_state
, unsigned int flags
)
148 struct lm_lockstruct
*ls
= &gl
->gl_sbd
->sd_lockstruct
;
153 gl
->gl_req
= req_state
;
154 req
= make_mode(req_state
);
155 lkf
= make_flags(gl
->gl_lksb
.sb_lkid
, flags
, req
);
158 * Submit the actual lock request.
161 error
= dlm_lock(ls
->ls_dlm
, req
, &gl
->gl_lksb
, lkf
, gl
->gl_strname
,
162 GDLM_STRNAME_BYTES
- 1, 0, gdlm_ast
, gl
, gdlm_bast
);
163 if (error
== -EAGAIN
)
170 static void gdlm_put_lock(struct kmem_cache
*cachep
, struct gfs2_glock
*gl
)
172 struct gfs2_sbd
*sdp
= gl
->gl_sbd
;
173 struct lm_lockstruct
*ls
= &sdp
->sd_lockstruct
;
176 if (gl
->gl_lksb
.sb_lkid
== 0) {
177 kmem_cache_free(cachep
, gl
);
178 if (atomic_dec_and_test(&sdp
->sd_glock_disposal
))
179 wake_up(&sdp
->sd_glock_wait
);
183 error
= dlm_unlock(ls
->ls_dlm
, gl
->gl_lksb
.sb_lkid
, DLM_LKF_VALBLK
,
186 printk(KERN_ERR
"gdlm_unlock %x,%llx err=%d\n",
188 (unsigned long long)gl
->gl_name
.ln_number
, error
);
193 static void gdlm_cancel(struct gfs2_glock
*gl
)
195 struct lm_lockstruct
*ls
= &gl
->gl_sbd
->sd_lockstruct
;
196 dlm_unlock(ls
->ls_dlm
, gl
->gl_lksb
.sb_lkid
, DLM_LKF_CANCEL
, NULL
, gl
);
199 static int gdlm_mount(struct gfs2_sbd
*sdp
, const char *fsname
)
201 struct lm_lockstruct
*ls
= &sdp
->sd_lockstruct
;
204 if (fsname
== NULL
) {
205 fs_info(sdp
, "no fsname found\n");
209 error
= dlm_new_lockspace(fsname
, strlen(fsname
), &ls
->ls_dlm
,
210 DLM_LSFL_FS
| DLM_LSFL_NEWEXCL
|
211 (ls
->ls_nodir
? DLM_LSFL_NODIR
: 0),
214 printk(KERN_ERR
"dlm_new_lockspace error %d", error
);
219 static void gdlm_unmount(struct gfs2_sbd
*sdp
)
221 struct lm_lockstruct
*ls
= &sdp
->sd_lockstruct
;
224 dlm_release_lockspace(ls
->ls_dlm
, 2);
229 static const match_table_t dlm_tokens
= {
230 { Opt_jid
, "jid=%d"},
232 { Opt_first
, "first=%d"},
233 { Opt_nodir
, "nodir=%d"},
237 const struct lm_lockops gfs2_dlm_ops
= {
238 .lm_proto_name
= "lock_dlm",
239 .lm_mount
= gdlm_mount
,
240 .lm_unmount
= gdlm_unmount
,
241 .lm_put_lock
= gdlm_put_lock
,
242 .lm_lock
= gdlm_lock
,
243 .lm_cancel
= gdlm_cancel
,
244 .lm_tokens
= &dlm_tokens
,