diff options
author | Steven Whitehouse <swhiteho@redhat.com> | 2009-01-12 05:43:39 -0500 |
---|---|---|
committer | Steven Whitehouse <steve@dolmen.chygwyn.com> | 2009-03-24 07:21:14 -0400 |
commit | f057f6cdf64175db1151b1f5d110e29904f119a1 (patch) | |
tree | 582dbf358e351f64977620c29ebf772d693b1948 /fs/gfs2/lock_dlm.c | |
parent | 22077f57dec8fcbeb1112b35313961c0902ff038 (diff) |
GFS2: Merge lock_dlm module into GFS2
This is the big patch that I've been working on for some time
now. There are many reasons for wanting to make this change
such as:
o Reducing overhead by eliminating duplicated fields between structures
o Simplifcation of the code (reduces the code size by a fair bit)
o The locking interface is now the DLM interface itself as proposed
some time ago.
o Fewer lookups of glocks when processing replies from the DLM
o Fewer memory allocations/deallocations for each glock
o Scope to do further optimisations in the future (but this patch is
more than big enough for now!)
Please note that (a) this patch relates to the lock_dlm module and
not the DLM itself, that is still a separate module; and (b) that
we retain the ability to build GFS2 as a standalone single node
filesystem with out requiring the DLM.
This patch needs a lot of testing, hence my keeping it I restarted
my -git tree after the last merge window. That way, this has the maximum
exposure before its merged. This is (modulo a few minor bug fixes) the
same patch that I've been posting on and off the the last three months
and its passed a number of different tests so far.
Signed-off-by: Steven Whitehouse <swhiteho@redhat.com>
Diffstat (limited to 'fs/gfs2/lock_dlm.c')
-rw-r--r-- | fs/gfs2/lock_dlm.c | 240 |
1 files changed, 240 insertions, 0 deletions
diff --git a/fs/gfs2/lock_dlm.c b/fs/gfs2/lock_dlm.c new file mode 100644 index 000000000000..a0bb7d2251a0 --- /dev/null +++ b/fs/gfs2/lock_dlm.c | |||
@@ -0,0 +1,240 @@ | |||
1 | /* | ||
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> | ||
12 | #include <linux/types.h> | ||
13 | #include <linux/gfs2_ondisk.h> | ||
14 | |||
15 | #include "incore.h" | ||
16 | #include "glock.h" | ||
17 | #include "util.h" | ||
18 | |||
19 | |||
20 | static void gdlm_ast(void *arg) | ||
21 | { | ||
22 | struct gfs2_glock *gl = arg; | ||
23 | unsigned ret = gl->gl_state; | ||
24 | |||
25 | BUG_ON(gl->gl_lksb.sb_flags & DLM_SBF_DEMOTED); | ||
26 | |||
27 | if (gl->gl_lksb.sb_flags & DLM_SBF_VALNOTVALID) | ||
28 | memset(gl->gl_lvb, 0, GDLM_LVB_SIZE); | ||
29 | |||
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); | ||
33 | return; | ||
34 | case -DLM_ECANCEL: /* Cancel while getting lock */ | ||
35 | ret |= LM_OUT_CANCELED; | ||
36 | goto out; | ||
37 | case -EAGAIN: /* Try lock fails */ | ||
38 | goto out; | ||
39 | case -EINVAL: /* Invalid */ | ||
40 | case -ENOMEM: /* Out of memory */ | ||
41 | ret |= LM_OUT_ERROR; | ||
42 | goto out; | ||
43 | case 0: /* Success */ | ||
44 | break; | ||
45 | default: /* Something unexpected */ | ||
46 | BUG(); | ||
47 | } | ||
48 | |||
49 | ret = gl->gl_target; | ||
50 | if (gl->gl_lksb.sb_flags & DLM_SBF_ALTMODE) { | ||
51 | if (gl->gl_target == LM_ST_SHARED) | ||
52 | ret = LM_ST_DEFERRED; | ||
53 | else if (gl->gl_target == LM_ST_DEFERRED) | ||
54 | ret = LM_ST_SHARED; | ||
55 | else | ||
56 | BUG(); | ||
57 | } | ||
58 | |||
59 | set_bit(GLF_INITIAL, &gl->gl_flags); | ||
60 | gfs2_glock_complete(gl, ret); | ||
61 | return; | ||
62 | out: | ||
63 | if (!test_bit(GLF_INITIAL, &gl->gl_flags)) | ||
64 | gl->gl_lksb.sb_lkid = 0; | ||
65 | gfs2_glock_complete(gl, ret); | ||
66 | } | ||
67 | |||
68 | static void gdlm_bast(void *arg, int mode) | ||
69 | { | ||
70 | struct gfs2_glock *gl = arg; | ||
71 | |||
72 | switch (mode) { | ||
73 | case DLM_LOCK_EX: | ||
74 | gfs2_glock_cb(gl, LM_ST_UNLOCKED); | ||
75 | break; | ||
76 | case DLM_LOCK_CW: | ||
77 | gfs2_glock_cb(gl, LM_ST_DEFERRED); | ||
78 | break; | ||
79 | case DLM_LOCK_PR: | ||
80 | gfs2_glock_cb(gl, LM_ST_SHARED); | ||
81 | break; | ||
82 | default: | ||
83 | printk(KERN_ERR "unknown bast mode %d", mode); | ||
84 | BUG(); | ||
85 | } | ||
86 | } | ||
87 | |||
88 | /* convert gfs lock-state to dlm lock-mode */ | ||
89 | |||
90 | static int make_mode(const unsigned int lmstate) | ||
91 | { | ||
92 | switch (lmstate) { | ||
93 | case LM_ST_UNLOCKED: | ||
94 | return DLM_LOCK_NL; | ||
95 | case LM_ST_EXCLUSIVE: | ||
96 | return DLM_LOCK_EX; | ||
97 | case LM_ST_DEFERRED: | ||
98 | return DLM_LOCK_CW; | ||
99 | case LM_ST_SHARED: | ||
100 | return DLM_LOCK_PR; | ||
101 | } | ||
102 | printk(KERN_ERR "unknown LM state %d", lmstate); | ||
103 | BUG(); | ||
104 | return -1; | ||
105 | } | ||
106 | |||
107 | static u32 make_flags(const u32 lkid, const unsigned int gfs_flags, | ||
108 | const int req) | ||
109 | { | ||
110 | u32 lkf = 0; | ||
111 | |||
112 | if (gfs_flags & LM_FLAG_TRY) | ||
113 | lkf |= DLM_LKF_NOQUEUE; | ||
114 | |||
115 | if (gfs_flags & LM_FLAG_TRY_1CB) { | ||
116 | lkf |= DLM_LKF_NOQUEUE; | ||
117 | lkf |= DLM_LKF_NOQUEUEBAST; | ||
118 | } | ||
119 | |||
120 | if (gfs_flags & LM_FLAG_PRIORITY) { | ||
121 | lkf |= DLM_LKF_NOORDER; | ||
122 | lkf |= DLM_LKF_HEADQUE; | ||
123 | } | ||
124 | |||
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; | ||
130 | else | ||
131 | BUG(); | ||
132 | } | ||
133 | |||
134 | if (lkid != 0) | ||
135 | lkf |= DLM_LKF_CONVERT; | ||
136 | |||
137 | lkf |= DLM_LKF_VALBLK; | ||
138 | |||
139 | return lkf; | ||
140 | } | ||
141 | |||
142 | static unsigned int gdlm_lock(struct gfs2_glock *gl, | ||
143 | unsigned int req_state, unsigned int flags) | ||
144 | { | ||
145 | struct lm_lockstruct *ls = &gl->gl_sbd->sd_lockstruct; | ||
146 | int error; | ||
147 | int req; | ||
148 | u32 lkf; | ||
149 | |||
150 | 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 | |||
157 | error = dlm_lock(ls->ls_dlm, req, &gl->gl_lksb, lkf, gl->gl_strname, | ||
158 | GDLM_STRNAME_BYTES - 1, 0, gdlm_ast, gl, gdlm_bast); | ||
159 | if (error == -EAGAIN) | ||
160 | return 0; | ||
161 | if (error) | ||
162 | return LM_OUT_ERROR; | ||
163 | return LM_OUT_ASYNC; | ||
164 | } | ||
165 | |||
166 | static void gdlm_put_lock(struct kmem_cache *cachep, void *ptr) | ||
167 | { | ||
168 | struct gfs2_glock *gl = ptr; | ||
169 | struct lm_lockstruct *ls = &gl->gl_sbd->sd_lockstruct; | ||
170 | int error; | ||
171 | |||
172 | if (gl->gl_lksb.sb_lkid == 0) { | ||
173 | kmem_cache_free(cachep, gl); | ||
174 | return; | ||
175 | } | ||
176 | |||
177 | error = dlm_unlock(ls->ls_dlm, gl->gl_lksb.sb_lkid, DLM_LKF_VALBLK, | ||
178 | NULL, gl); | ||
179 | if (error) { | ||
180 | printk(KERN_ERR "gdlm_unlock %x,%llx err=%d\n", | ||
181 | gl->gl_name.ln_type, | ||
182 | (unsigned long long)gl->gl_name.ln_number, error); | ||
183 | return; | ||
184 | } | ||
185 | } | ||
186 | |||
187 | static void gdlm_cancel(struct gfs2_glock *gl) | ||
188 | { | ||
189 | struct lm_lockstruct *ls = &gl->gl_sbd->sd_lockstruct; | ||
190 | dlm_unlock(ls->ls_dlm, gl->gl_lksb.sb_lkid, DLM_LKF_CANCEL, NULL, gl); | ||
191 | } | ||
192 | |||
193 | static int gdlm_mount(struct gfs2_sbd *sdp, const char *fsname) | ||
194 | { | ||
195 | struct lm_lockstruct *ls = &sdp->sd_lockstruct; | ||
196 | int error; | ||
197 | |||
198 | if (fsname == NULL) { | ||
199 | fs_info(sdp, "no fsname found\n"); | ||
200 | return -EINVAL; | ||
201 | } | ||
202 | |||
203 | error = dlm_new_lockspace(fsname, strlen(fsname), &ls->ls_dlm, | ||
204 | DLM_LSFL_FS | DLM_LSFL_NEWEXCL | | ||
205 | (ls->ls_nodir ? DLM_LSFL_NODIR : 0), | ||
206 | GDLM_LVB_SIZE); | ||
207 | if (error) | ||
208 | printk(KERN_ERR "dlm_new_lockspace error %d", error); | ||
209 | |||
210 | return error; | ||
211 | } | ||
212 | |||
213 | static void gdlm_unmount(struct gfs2_sbd *sdp) | ||
214 | { | ||
215 | struct lm_lockstruct *ls = &sdp->sd_lockstruct; | ||
216 | |||
217 | if (ls->ls_dlm) { | ||
218 | dlm_release_lockspace(ls->ls_dlm, 2); | ||
219 | ls->ls_dlm = NULL; | ||
220 | } | ||
221 | } | ||
222 | |||
223 | static const match_table_t dlm_tokens = { | ||
224 | { Opt_jid, "jid=%d"}, | ||
225 | { Opt_id, "id=%d"}, | ||
226 | { Opt_first, "first=%d"}, | ||
227 | { Opt_nodir, "nodir=%d"}, | ||
228 | { Opt_err, NULL }, | ||
229 | }; | ||
230 | |||
231 | const struct lm_lockops gfs2_dlm_ops = { | ||
232 | .lm_proto_name = "lock_dlm", | ||
233 | .lm_mount = gdlm_mount, | ||
234 | .lm_unmount = gdlm_unmount, | ||
235 | .lm_put_lock = gdlm_put_lock, | ||
236 | .lm_lock = gdlm_lock, | ||
237 | .lm_cancel = gdlm_cancel, | ||
238 | .lm_tokens = &dlm_tokens, | ||
239 | }; | ||
240 | |||