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;
25 BUG_ON(gl->gl_lksb.sb_flags & DLM_SBF_DEMOTED);
27 if (gl->gl_lksb.sb_flags & DLM_SBF_VALNOTVALID)
28 memset(gl->gl_lvb, 0, GDLM_LVB_SIZE);
30 switch (gl->gl_lksb.sb_status) {
31 case -DLM_EUNLOCK: /* Unlocked, so glock can be freed */
32 kmem_cache_free(gfs2_glock_cachep, gl);
34 case -DLM_ECANCEL: /* Cancel while getting lock */
35 ret |= LM_OUT_CANCELED;
37 case -EAGAIN: /* Try lock fails */
39 case -EINVAL: /* Invalid */
40 case -ENOMEM: /* Out of memory */
45 default: /* Something unexpected */
50 if (gl->gl_lksb.sb_flags & DLM_SBF_ALTMODE) {
51 if (gl->gl_req == LM_ST_SHARED)
53 else if (gl->gl_req == LM_ST_DEFERRED)
59 set_bit(GLF_INITIAL, &gl->gl_flags);
60 gfs2_glock_complete(gl, ret);
63 if (!test_bit(GLF_INITIAL, &gl->gl_flags))
64 gl->gl_lksb.sb_lkid = 0;
65 gfs2_glock_complete(gl, ret);
68 static void gdlm_bast(void *arg, int mode)
70 struct gfs2_glock *gl = arg;
74 gfs2_glock_cb(gl, LM_ST_UNLOCKED);
77 gfs2_glock_cb(gl, LM_ST_DEFERRED);
80 gfs2_glock_cb(gl, LM_ST_SHARED);
83 printk(KERN_ERR "unknown bast mode %d", mode);
88 /* convert gfs lock-state to dlm lock-mode */
90 static int make_mode(const unsigned int lmstate)
102 printk(KERN_ERR "unknown LM state %d", lmstate);
107 static u32 make_flags(const u32 lkid, const unsigned int gfs_flags,
112 if (gfs_flags & LM_FLAG_TRY)
113 lkf |= DLM_LKF_NOQUEUE;
115 if (gfs_flags & LM_FLAG_TRY_1CB) {
116 lkf |= DLM_LKF_NOQUEUE;
117 lkf |= DLM_LKF_NOQUEUEBAST;
120 if (gfs_flags & LM_FLAG_PRIORITY) {
121 lkf |= DLM_LKF_NOORDER;
122 lkf |= DLM_LKF_HEADQUE;
125 if (gfs_flags & LM_FLAG_ANY) {
126 if (req == DLM_LOCK_PR)
127 lkf |= DLM_LKF_ALTCW;
128 else if (req == DLM_LOCK_CW)
129 lkf |= DLM_LKF_ALTPR;
135 lkf |= DLM_LKF_CONVERT;
137 lkf |= DLM_LKF_VALBLK;
142 static unsigned int gdlm_lock(struct gfs2_glock *gl,
143 unsigned int req_state, unsigned int flags)
145 struct lm_lockstruct *ls = &gl->gl_sbd->sd_lockstruct;
150 gl->gl_req = req_state;
151 req = make_mode(req_state);
152 lkf = make_flags(gl->gl_lksb.sb_lkid, flags, req);
155 * Submit the actual lock request.
158 error = dlm_lock(ls->ls_dlm, req, &gl->gl_lksb, lkf, gl->gl_strname,
159 GDLM_STRNAME_BYTES - 1, 0, gdlm_ast, gl, gdlm_bast);
160 if (error == -EAGAIN)
167 static void gdlm_put_lock(struct kmem_cache *cachep, void *ptr)
169 struct gfs2_glock *gl = ptr;
170 struct lm_lockstruct *ls = &gl->gl_sbd->sd_lockstruct;
173 if (gl->gl_lksb.sb_lkid == 0) {
174 kmem_cache_free(cachep, gl);
178 error = dlm_unlock(ls->ls_dlm, gl->gl_lksb.sb_lkid, DLM_LKF_VALBLK,
181 printk(KERN_ERR "gdlm_unlock %x,%llx err=%d\n",
183 (unsigned long long)gl->gl_name.ln_number, error);
188 static void gdlm_cancel(struct gfs2_glock *gl)
190 struct lm_lockstruct *ls = &gl->gl_sbd->sd_lockstruct;
191 dlm_unlock(ls->ls_dlm, gl->gl_lksb.sb_lkid, DLM_LKF_CANCEL, NULL, gl);
194 static int gdlm_mount(struct gfs2_sbd *sdp, const char *fsname)
196 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
199 if (fsname == NULL) {
200 fs_info(sdp, "no fsname found\n");
204 error = dlm_new_lockspace(fsname, strlen(fsname), &ls->ls_dlm,
205 DLM_LSFL_FS | DLM_LSFL_NEWEXCL |
206 (ls->ls_nodir ? DLM_LSFL_NODIR : 0),
209 printk(KERN_ERR "dlm_new_lockspace error %d", error);
214 static void gdlm_unmount(struct gfs2_sbd *sdp)
216 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
219 dlm_release_lockspace(ls->ls_dlm, 2);
224 static const match_table_t dlm_tokens = {
225 { Opt_jid, "jid=%d"},
227 { Opt_first, "first=%d"},
228 { Opt_nodir, "nodir=%d"},
232 const struct lm_lockops gfs2_dlm_ops = {
233 .lm_proto_name = "lock_dlm",
234 .lm_mount = gdlm_mount,
235 .lm_unmount = gdlm_unmount,
236 .lm_put_lock = gdlm_put_lock,
237 .lm_lock = gdlm_lock,
238 .lm_cancel = gdlm_cancel,
239 .lm_tokens = &dlm_tokens,