blob: 98c80d8c2a62df79273433cb494a3f05d35ae02c [file] [log] [blame]
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
4 *
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.
8 */
9
10#include <linux/fs.h>
11#include <linux/dlm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Steven Whitehousef057f6c2009-01-12 10:43:39 +000013#include <linux/types.h>
14#include <linux/gfs2_ondisk.h>
15
16#include "incore.h"
17#include "glock.h"
18#include "util.h"
19
20
21static void gdlm_ast(void *arg)
22{
23 struct gfs2_glock *gl = arg;
24 unsigned ret = gl->gl_state;
25
26 BUG_ON(gl->gl_lksb.sb_flags & DLM_SBF_DEMOTED);
27
28 if (gl->gl_lksb.sb_flags & DLM_SBF_VALNOTVALID)
29 memset(gl->gl_lvb, 0, GDLM_LVB_SIZE);
30
31 switch (gl->gl_lksb.sb_status) {
32 case -DLM_EUNLOCK: /* Unlocked, so glock can be freed */
Steven Whitehousefc0e38d2011-03-09 10:58:04 +000033 gfs2_glock_free(gl);
Steven Whitehousef057f6c2009-01-12 10:43:39 +000034 return;
35 case -DLM_ECANCEL: /* Cancel while getting lock */
36 ret |= LM_OUT_CANCELED;
37 goto out;
38 case -EAGAIN: /* Try lock fails */
Steven Whitehouse1fea7c22010-09-08 10:09:25 +010039 case -EDEADLK: /* Deadlock detected */
Steven Whitehousef057f6c2009-01-12 10:43:39 +000040 goto out;
Steven Whitehouse1fea7c22010-09-08 10:09:25 +010041 case -ETIMEDOUT: /* Canceled due to timeout */
Steven Whitehousef057f6c2009-01-12 10:43:39 +000042 ret |= LM_OUT_ERROR;
43 goto out;
44 case 0: /* Success */
45 break;
46 default: /* Something unexpected */
47 BUG();
48 }
49
Benjamin Marzinski02ffad02009-03-06 10:03:20 -060050 ret = gl->gl_req;
Steven Whitehousef057f6c2009-01-12 10:43:39 +000051 if (gl->gl_lksb.sb_flags & DLM_SBF_ALTMODE) {
Benjamin Marzinski02ffad02009-03-06 10:03:20 -060052 if (gl->gl_req == LM_ST_SHARED)
Steven Whitehousef057f6c2009-01-12 10:43:39 +000053 ret = LM_ST_DEFERRED;
Benjamin Marzinski02ffad02009-03-06 10:03:20 -060054 else if (gl->gl_req == LM_ST_DEFERRED)
Steven Whitehousef057f6c2009-01-12 10:43:39 +000055 ret = LM_ST_SHARED;
56 else
57 BUG();
58 }
59
60 set_bit(GLF_INITIAL, &gl->gl_flags);
61 gfs2_glock_complete(gl, ret);
62 return;
63out:
64 if (!test_bit(GLF_INITIAL, &gl->gl_flags))
65 gl->gl_lksb.sb_lkid = 0;
66 gfs2_glock_complete(gl, ret);
67}
68
69static void gdlm_bast(void *arg, int mode)
70{
71 struct gfs2_glock *gl = arg;
72
73 switch (mode) {
74 case DLM_LOCK_EX:
75 gfs2_glock_cb(gl, LM_ST_UNLOCKED);
76 break;
77 case DLM_LOCK_CW:
78 gfs2_glock_cb(gl, LM_ST_DEFERRED);
79 break;
80 case DLM_LOCK_PR:
81 gfs2_glock_cb(gl, LM_ST_SHARED);
82 break;
83 default:
84 printk(KERN_ERR "unknown bast mode %d", mode);
85 BUG();
86 }
87}
88
89/* convert gfs lock-state to dlm lock-mode */
90
91static int make_mode(const unsigned int lmstate)
92{
93 switch (lmstate) {
94 case LM_ST_UNLOCKED:
95 return DLM_LOCK_NL;
96 case LM_ST_EXCLUSIVE:
97 return DLM_LOCK_EX;
98 case LM_ST_DEFERRED:
99 return DLM_LOCK_CW;
100 case LM_ST_SHARED:
101 return DLM_LOCK_PR;
102 }
103 printk(KERN_ERR "unknown LM state %d", lmstate);
104 BUG();
105 return -1;
106}
107
108static u32 make_flags(const u32 lkid, const unsigned int gfs_flags,
109 const int req)
110{
111 u32 lkf = 0;
112
113 if (gfs_flags & LM_FLAG_TRY)
114 lkf |= DLM_LKF_NOQUEUE;
115
116 if (gfs_flags & LM_FLAG_TRY_1CB) {
117 lkf |= DLM_LKF_NOQUEUE;
118 lkf |= DLM_LKF_NOQUEUEBAST;
119 }
120
121 if (gfs_flags & LM_FLAG_PRIORITY) {
122 lkf |= DLM_LKF_NOORDER;
123 lkf |= DLM_LKF_HEADQUE;
124 }
125
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;
131 else
132 BUG();
133 }
134
135 if (lkid != 0)
136 lkf |= DLM_LKF_CONVERT;
137
138 lkf |= DLM_LKF_VALBLK;
139
140 return lkf;
141}
142
Steven Whitehouse921169c2010-11-29 12:50:38 +0000143static int gdlm_lock(struct gfs2_glock *gl, unsigned int req_state,
144 unsigned int flags)
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000145{
146 struct lm_lockstruct *ls = &gl->gl_sbd->sd_lockstruct;
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000147 int req;
148 u32 lkf;
149
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000150 req = make_mode(req_state);
151 lkf = make_flags(gl->gl_lksb.sb_lkid, flags, req);
152
153 /*
154 * Submit the actual lock request.
155 */
156
Steven Whitehouse921169c2010-11-29 12:50:38 +0000157 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);
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000159}
160
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000161static void gdlm_put_lock(struct gfs2_glock *gl)
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000162{
Steven Whitehousee4027462010-01-25 11:20:19 +0000163 struct gfs2_sbd *sdp = gl->gl_sbd;
164 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000165 int error;
166
167 if (gl->gl_lksb.sb_lkid == 0) {
Steven Whitehousefc0e38d2011-03-09 10:58:04 +0000168 gfs2_glock_free(gl);
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000169 return;
170 }
171
172 error = dlm_unlock(ls->ls_dlm, gl->gl_lksb.sb_lkid, DLM_LKF_VALBLK,
173 NULL, gl);
174 if (error) {
175 printk(KERN_ERR "gdlm_unlock %x,%llx err=%d\n",
176 gl->gl_name.ln_type,
177 (unsigned long long)gl->gl_name.ln_number, error);
178 return;
179 }
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000180}
181
182static void gdlm_cancel(struct gfs2_glock *gl)
183{
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);
186}
187
188static int gdlm_mount(struct gfs2_sbd *sdp, const char *fsname)
189{
190 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
191 int error;
192
193 if (fsname == NULL) {
194 fs_info(sdp, "no fsname found\n");
195 return -EINVAL;
196 }
197
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),
201 GDLM_LVB_SIZE);
202 if (error)
203 printk(KERN_ERR "dlm_new_lockspace error %d", error);
204
205 return error;
206}
207
208static void gdlm_unmount(struct gfs2_sbd *sdp)
209{
210 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
211
212 if (ls->ls_dlm) {
213 dlm_release_lockspace(ls->ls_dlm, 2);
214 ls->ls_dlm = NULL;
215 }
216}
217
218static const match_table_t dlm_tokens = {
219 { Opt_jid, "jid=%d"},
220 { Opt_id, "id=%d"},
221 { Opt_first, "first=%d"},
222 { Opt_nodir, "nodir=%d"},
223 { Opt_err, NULL },
224};
225
226const 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,
234};
235