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/slab.h>
13 #include <linux/types.h>
14 #include <linux/gfs2_ondisk.h>
21 static void gdlm_ast(void *arg)
23 struct gfs2_glock *gl = arg;
24 unsigned ret = gl->gl_state;
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 */
35 case -DLM_ECANCEL: /* Cancel while getting lock */
36 ret |= LM_OUT_CANCELED;
38 case -EAGAIN: /* Try lock fails */
39 case -EDEADLK: /* Deadlock detected */
41 case -ETIMEDOUT: /* Canceled due to timeout */
46 default: /* Something unexpected */
51 if (gl->gl_lksb.sb_flags & DLM_SBF_ALTMODE) {
52 if (gl->gl_req == LM_ST_SHARED)
54 else if (gl->gl_req == LM_ST_DEFERRED)
60 set_bit(GLF_INITIAL, &gl->gl_flags);
61 gfs2_glock_complete(gl, ret);
64 if (!test_bit(GLF_INITIAL, &gl->gl_flags))
65 gl->gl_lksb.sb_lkid = 0;
66 gfs2_glock_complete(gl, ret);
69 static void gdlm_bast(void *arg, int mode)
71 struct gfs2_glock *gl = arg;
75 gfs2_glock_cb(gl, LM_ST_UNLOCKED);
78 gfs2_glock_cb(gl, LM_ST_DEFERRED);
81 gfs2_glock_cb(gl, LM_ST_SHARED);
84 printk(KERN_ERR "unknown bast mode %d", mode);
89 /* convert gfs lock-state to dlm lock-mode */
91 static int make_mode(const unsigned int lmstate)
103 printk(KERN_ERR "unknown LM state %d", lmstate);
108 static u32 make_flags(const u32 lkid, const unsigned int gfs_flags,
113 if (gfs_flags & LM_FLAG_TRY)
114 lkf |= DLM_LKF_NOQUEUE;
116 if (gfs_flags & LM_FLAG_TRY_1CB) {
117 lkf |= DLM_LKF_NOQUEUE;
118 lkf |= DLM_LKF_NOQUEUEBAST;
121 if (gfs_flags & LM_FLAG_PRIORITY) {
122 lkf |= DLM_LKF_NOORDER;
123 lkf |= DLM_LKF_HEADQUE;
126 if (gfs_flags & LM_FLAG_ANY) {
127 if (req == DLM_LOCK_PR)
128 lkf |= DLM_LKF_ALTCW;
129 else if (req == DLM_LOCK_CW)
130 lkf |= DLM_LKF_ALTPR;
136 lkf |= DLM_LKF_CONVERT;
138 lkf |= DLM_LKF_VALBLK;
143 static int gdlm_lock(struct gfs2_glock *gl, unsigned int req_state,
146 struct lm_lockstruct *ls = &gl->gl_sbd->sd_lockstruct;
150 req = make_mode(req_state);
151 lkf = make_flags(gl->gl_lksb.sb_lkid, flags, req);
154 * Submit the actual lock request.
157 return dlm_lock(ls->ls_dlm, req, &gl->gl_lksb, lkf, gl->gl_strname,
158 GDLM_STRNAME_BYTES - 1, 0, gdlm_ast, gl, gdlm_bast);
161 static void gdlm_put_lock(struct gfs2_glock *gl)
163 struct gfs2_sbd *sdp = gl->gl_sbd;
164 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
167 if (gl->gl_lksb.sb_lkid == 0) {
172 error = dlm_unlock(ls->ls_dlm, gl->gl_lksb.sb_lkid, DLM_LKF_VALBLK,
175 printk(KERN_ERR "gdlm_unlock %x,%llx err=%d\n",
177 (unsigned long long)gl->gl_name.ln_number, error);
182 static void gdlm_cancel(struct gfs2_glock *gl)
184 struct lm_lockstruct *ls = &gl->gl_sbd->sd_lockstruct;
185 dlm_unlock(ls->ls_dlm, gl->gl_lksb.sb_lkid, DLM_LKF_CANCEL, NULL, gl);
188 static int gdlm_mount(struct gfs2_sbd *sdp, const char *fsname)
190 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
193 if (fsname == NULL) {
194 fs_info(sdp, "no fsname found\n");
198 error = dlm_new_lockspace(fsname, strlen(fsname), &ls->ls_dlm,
199 DLM_LSFL_FS | DLM_LSFL_NEWEXCL |
200 (ls->ls_nodir ? DLM_LSFL_NODIR : 0),
203 printk(KERN_ERR "dlm_new_lockspace error %d", error);
208 static void gdlm_unmount(struct gfs2_sbd *sdp)
210 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
213 dlm_release_lockspace(ls->ls_dlm, 2);
218 static const match_table_t dlm_tokens = {
219 { Opt_jid, "jid=%d"},
221 { Opt_first, "first=%d"},
222 { Opt_nodir, "nodir=%d"},
226 const struct lm_lockops gfs2_dlm_ops = {
227 .lm_proto_name = "lock_dlm",
228 .lm_mount = gdlm_mount,
229 .lm_unmount = gdlm_unmount,
230 .lm_put_lock = gdlm_put_lock,
231 .lm_lock = gdlm_lock,
232 .lm_cancel = gdlm_cancel,
233 .lm_tokens = &dlm_tokens,