diff options
Diffstat (limited to 'fs/gfs2/locking/dlm/thread.c')
-rw-r--r-- | fs/gfs2/locking/dlm/thread.c | 359 |
1 files changed, 359 insertions, 0 deletions
diff --git a/fs/gfs2/locking/dlm/thread.c b/fs/gfs2/locking/dlm/thread.c new file mode 100644 index 000000000000..22bbe6d3a5ae --- /dev/null +++ b/fs/gfs2/locking/dlm/thread.c | |||
@@ -0,0 +1,359 @@ | |||
1 | /****************************************************************************** | ||
2 | ******************************************************************************* | ||
3 | ** | ||
4 | ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. | ||
5 | ** Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved. | ||
6 | ** | ||
7 | ** This copyrighted material is made available to anyone wishing to use, | ||
8 | ** modify, copy, or redistribute it subject to the terms and conditions | ||
9 | ** of the GNU General Public License v.2. | ||
10 | ** | ||
11 | ******************************************************************************* | ||
12 | ******************************************************************************/ | ||
13 | |||
14 | #include "lock_dlm.h" | ||
15 | |||
16 | /* A lock placed on this queue is re-submitted to DLM as soon as the lock_dlm | ||
17 | thread gets to it. */ | ||
18 | |||
19 | static void queue_submit(struct gdlm_lock *lp) | ||
20 | { | ||
21 | struct gdlm_ls *ls = lp->ls; | ||
22 | |||
23 | spin_lock(&ls->async_lock); | ||
24 | list_add_tail(&lp->delay_list, &ls->submit); | ||
25 | spin_unlock(&ls->async_lock); | ||
26 | wake_up(&ls->thread_wait); | ||
27 | } | ||
28 | |||
29 | static void process_submit(struct gdlm_lock *lp) | ||
30 | { | ||
31 | gdlm_do_lock(lp, NULL); | ||
32 | } | ||
33 | |||
34 | static void process_blocking(struct gdlm_lock *lp, int bast_mode) | ||
35 | { | ||
36 | struct gdlm_ls *ls = lp->ls; | ||
37 | unsigned int cb; | ||
38 | |||
39 | switch (gdlm_make_lmstate(bast_mode)) { | ||
40 | case LM_ST_EXCLUSIVE: | ||
41 | cb = LM_CB_NEED_E; | ||
42 | break; | ||
43 | case LM_ST_DEFERRED: | ||
44 | cb = LM_CB_NEED_D; | ||
45 | break; | ||
46 | case LM_ST_SHARED: | ||
47 | cb = LM_CB_NEED_S; | ||
48 | break; | ||
49 | default: | ||
50 | GDLM_ASSERT(0, printk("unknown bast mode %u\n",lp->bast_mode);); | ||
51 | } | ||
52 | |||
53 | ls->fscb(ls->fsdata, cb, &lp->lockname); | ||
54 | } | ||
55 | |||
56 | static void process_complete(struct gdlm_lock *lp) | ||
57 | { | ||
58 | struct gdlm_ls *ls = lp->ls; | ||
59 | struct lm_async_cb acb; | ||
60 | int16_t prev_mode = lp->cur; | ||
61 | |||
62 | memset(&acb, 0, sizeof(acb)); | ||
63 | |||
64 | if (lp->lksb.sb_status == -DLM_ECANCEL) { | ||
65 | log_all("complete dlm cancel %x,%"PRIx64" flags %lx", | ||
66 | lp->lockname.ln_type, lp->lockname.ln_number, | ||
67 | lp->flags); | ||
68 | |||
69 | lp->req = lp->cur; | ||
70 | acb.lc_ret |= LM_OUT_CANCELED; | ||
71 | if (lp->cur == DLM_LOCK_IV) | ||
72 | lp->lksb.sb_lkid = 0; | ||
73 | goto out; | ||
74 | } | ||
75 | |||
76 | if (test_and_clear_bit(LFL_DLM_UNLOCK, &lp->flags)) { | ||
77 | if (lp->lksb.sb_status != -DLM_EUNLOCK) { | ||
78 | log_all("unlock sb_status %d %x,%"PRIx64" flags %lx", | ||
79 | lp->lksb.sb_status, lp->lockname.ln_type, | ||
80 | lp->lockname.ln_number, lp->flags); | ||
81 | return; | ||
82 | } | ||
83 | |||
84 | lp->cur = DLM_LOCK_IV; | ||
85 | lp->req = DLM_LOCK_IV; | ||
86 | lp->lksb.sb_lkid = 0; | ||
87 | |||
88 | if (test_and_clear_bit(LFL_UNLOCK_DELETE, &lp->flags)) { | ||
89 | gdlm_delete_lp(lp); | ||
90 | return; | ||
91 | } | ||
92 | goto out; | ||
93 | } | ||
94 | |||
95 | if (lp->lksb.sb_flags & DLM_SBF_VALNOTVALID) | ||
96 | memset(lp->lksb.sb_lvbptr, 0, GDLM_LVB_SIZE); | ||
97 | |||
98 | if (lp->lksb.sb_flags & DLM_SBF_ALTMODE) { | ||
99 | if (lp->req == DLM_LOCK_PR) | ||
100 | lp->req = DLM_LOCK_CW; | ||
101 | else if (lp->req == DLM_LOCK_CW) | ||
102 | lp->req = DLM_LOCK_PR; | ||
103 | } | ||
104 | |||
105 | /* | ||
106 | * A canceled lock request. The lock was just taken off the delayed | ||
107 | * list and was never even submitted to dlm. | ||
108 | */ | ||
109 | |||
110 | if (test_and_clear_bit(LFL_CANCEL, &lp->flags)) { | ||
111 | log_all("complete internal cancel %x,%"PRIx64"", | ||
112 | lp->lockname.ln_type, lp->lockname.ln_number); | ||
113 | lp->req = lp->cur; | ||
114 | acb.lc_ret |= LM_OUT_CANCELED; | ||
115 | goto out; | ||
116 | } | ||
117 | |||
118 | /* | ||
119 | * An error occured. | ||
120 | */ | ||
121 | |||
122 | if (lp->lksb.sb_status) { | ||
123 | /* a "normal" error */ | ||
124 | if ((lp->lksb.sb_status == -EAGAIN) && | ||
125 | (lp->lkf & DLM_LKF_NOQUEUE)) { | ||
126 | lp->req = lp->cur; | ||
127 | if (lp->cur == DLM_LOCK_IV) | ||
128 | lp->lksb.sb_lkid = 0; | ||
129 | goto out; | ||
130 | } | ||
131 | |||
132 | /* this could only happen with cancels I think */ | ||
133 | log_all("ast sb_status %d %x,%"PRIx64" flags %lx", | ||
134 | lp->lksb.sb_status, lp->lockname.ln_type, | ||
135 | lp->lockname.ln_number, lp->flags); | ||
136 | return; | ||
137 | } | ||
138 | |||
139 | /* | ||
140 | * This is an AST for an EX->EX conversion for sync_lvb from GFS. | ||
141 | */ | ||
142 | |||
143 | if (test_and_clear_bit(LFL_SYNC_LVB, &lp->flags)) { | ||
144 | complete(&lp->ast_wait); | ||
145 | return; | ||
146 | } | ||
147 | |||
148 | /* | ||
149 | * A lock has been demoted to NL because it initially completed during | ||
150 | * BLOCK_LOCKS. Now it must be requested in the originally requested | ||
151 | * mode. | ||
152 | */ | ||
153 | |||
154 | if (test_and_clear_bit(LFL_REREQUEST, &lp->flags)) { | ||
155 | GDLM_ASSERT(lp->req == DLM_LOCK_NL,); | ||
156 | GDLM_ASSERT(lp->prev_req > DLM_LOCK_NL,); | ||
157 | |||
158 | lp->cur = DLM_LOCK_NL; | ||
159 | lp->req = lp->prev_req; | ||
160 | lp->prev_req = DLM_LOCK_IV; | ||
161 | lp->lkf &= ~DLM_LKF_CONVDEADLK; | ||
162 | |||
163 | set_bit(LFL_NOCACHE, &lp->flags); | ||
164 | |||
165 | if (test_bit(DFL_BLOCK_LOCKS, &ls->flags) && | ||
166 | !test_bit(LFL_NOBLOCK, &lp->flags)) | ||
167 | gdlm_queue_delayed(lp); | ||
168 | else | ||
169 | queue_submit(lp); | ||
170 | return; | ||
171 | } | ||
172 | |||
173 | /* | ||
174 | * A request is granted during dlm recovery. It may be granted | ||
175 | * because the locks of a failed node were cleared. In that case, | ||
176 | * there may be inconsistent data beneath this lock and we must wait | ||
177 | * for recovery to complete to use it. When gfs recovery is done this | ||
178 | * granted lock will be converted to NL and then reacquired in this | ||
179 | * granted state. | ||
180 | */ | ||
181 | |||
182 | if (test_bit(DFL_BLOCK_LOCKS, &ls->flags) && | ||
183 | !test_bit(LFL_NOBLOCK, &lp->flags) && | ||
184 | lp->req != DLM_LOCK_NL) { | ||
185 | |||
186 | lp->cur = lp->req; | ||
187 | lp->prev_req = lp->req; | ||
188 | lp->req = DLM_LOCK_NL; | ||
189 | lp->lkf |= DLM_LKF_CONVERT; | ||
190 | lp->lkf &= ~DLM_LKF_CONVDEADLK; | ||
191 | |||
192 | log_debug("rereq %x,%"PRIx64" id %x %d,%d", | ||
193 | lp->lockname.ln_type, lp->lockname.ln_number, | ||
194 | lp->lksb.sb_lkid, lp->cur, lp->req); | ||
195 | |||
196 | set_bit(LFL_REREQUEST, &lp->flags); | ||
197 | queue_submit(lp); | ||
198 | return; | ||
199 | } | ||
200 | |||
201 | /* | ||
202 | * DLM demoted the lock to NL before it was granted so GFS must be | ||
203 | * told it cannot cache data for this lock. | ||
204 | */ | ||
205 | |||
206 | if (lp->lksb.sb_flags & DLM_SBF_DEMOTED) | ||
207 | set_bit(LFL_NOCACHE, &lp->flags); | ||
208 | |||
209 | out: | ||
210 | /* | ||
211 | * This is an internal lock_dlm lock | ||
212 | */ | ||
213 | |||
214 | if (test_bit(LFL_INLOCK, &lp->flags)) { | ||
215 | clear_bit(LFL_NOBLOCK, &lp->flags); | ||
216 | lp->cur = lp->req; | ||
217 | complete(&lp->ast_wait); | ||
218 | return; | ||
219 | } | ||
220 | |||
221 | /* | ||
222 | * Normal completion of a lock request. Tell GFS it now has the lock. | ||
223 | */ | ||
224 | |||
225 | clear_bit(LFL_NOBLOCK, &lp->flags); | ||
226 | lp->cur = lp->req; | ||
227 | |||
228 | acb.lc_name = lp->lockname; | ||
229 | acb.lc_ret |= gdlm_make_lmstate(lp->cur); | ||
230 | |||
231 | if (!test_and_clear_bit(LFL_NOCACHE, &lp->flags) && | ||
232 | (lp->cur > DLM_LOCK_NL) && (prev_mode > DLM_LOCK_NL)) | ||
233 | acb.lc_ret |= LM_OUT_CACHEABLE; | ||
234 | |||
235 | ls->fscb(ls->fsdata, LM_CB_ASYNC, &acb); | ||
236 | } | ||
237 | |||
238 | static inline int no_work(struct gdlm_ls *ls, int blocking) | ||
239 | { | ||
240 | int ret; | ||
241 | |||
242 | spin_lock(&ls->async_lock); | ||
243 | ret = list_empty(&ls->complete) && list_empty(&ls->submit); | ||
244 | if (ret && blocking) | ||
245 | ret = list_empty(&ls->blocking); | ||
246 | spin_unlock(&ls->async_lock); | ||
247 | |||
248 | return ret; | ||
249 | } | ||
250 | |||
251 | static inline int check_drop(struct gdlm_ls *ls) | ||
252 | { | ||
253 | if (!ls->drop_locks_count) | ||
254 | return 0; | ||
255 | |||
256 | if (time_after(jiffies, ls->drop_time + ls->drop_locks_period * HZ)) { | ||
257 | ls->drop_time = jiffies; | ||
258 | if (ls->all_locks_count >= ls->drop_locks_count) | ||
259 | return 1; | ||
260 | } | ||
261 | return 0; | ||
262 | } | ||
263 | |||
264 | static int gdlm_thread(void *data) | ||
265 | { | ||
266 | struct gdlm_ls *ls = (struct gdlm_ls *) data; | ||
267 | struct gdlm_lock *lp = NULL; | ||
268 | int blist = 0; | ||
269 | uint8_t complete, blocking, submit, drop; | ||
270 | DECLARE_WAITQUEUE(wait, current); | ||
271 | |||
272 | /* Only thread1 is allowed to do blocking callbacks since gfs | ||
273 | may wait for a completion callback within a blocking cb. */ | ||
274 | |||
275 | if (current == ls->thread1) | ||
276 | blist = 1; | ||
277 | |||
278 | while (!kthread_should_stop()) { | ||
279 | set_current_state(TASK_INTERRUPTIBLE); | ||
280 | add_wait_queue(&ls->thread_wait, &wait); | ||
281 | if (no_work(ls, blist)) | ||
282 | schedule(); | ||
283 | remove_wait_queue(&ls->thread_wait, &wait); | ||
284 | set_current_state(TASK_RUNNING); | ||
285 | |||
286 | complete = blocking = submit = drop = 0; | ||
287 | |||
288 | spin_lock(&ls->async_lock); | ||
289 | |||
290 | if (blist && !list_empty(&ls->blocking)) { | ||
291 | lp = list_entry(ls->blocking.next, struct gdlm_lock, | ||
292 | blist); | ||
293 | list_del_init(&lp->blist); | ||
294 | blocking = lp->bast_mode; | ||
295 | lp->bast_mode = 0; | ||
296 | } else if (!list_empty(&ls->complete)) { | ||
297 | lp = list_entry(ls->complete.next, struct gdlm_lock, | ||
298 | clist); | ||
299 | list_del_init(&lp->clist); | ||
300 | complete = 1; | ||
301 | } else if (!list_empty(&ls->submit)) { | ||
302 | lp = list_entry(ls->submit.next, struct gdlm_lock, | ||
303 | delay_list); | ||
304 | list_del_init(&lp->delay_list); | ||
305 | submit = 1; | ||
306 | } | ||
307 | |||
308 | drop = check_drop(ls); | ||
309 | spin_unlock(&ls->async_lock); | ||
310 | |||
311 | if (complete) | ||
312 | process_complete(lp); | ||
313 | |||
314 | else if (blocking) | ||
315 | process_blocking(lp, blocking); | ||
316 | |||
317 | else if (submit) | ||
318 | process_submit(lp); | ||
319 | |||
320 | if (drop) | ||
321 | ls->fscb(ls->fsdata, LM_CB_DROPLOCKS, NULL); | ||
322 | |||
323 | schedule(); | ||
324 | } | ||
325 | |||
326 | return 0; | ||
327 | } | ||
328 | |||
329 | int gdlm_init_threads(struct gdlm_ls *ls) | ||
330 | { | ||
331 | struct task_struct *p; | ||
332 | int error; | ||
333 | |||
334 | p = kthread_run(gdlm_thread, ls, "lock_dlm1"); | ||
335 | error = IS_ERR(p); | ||
336 | if (error) { | ||
337 | log_all("can't start lock_dlm1 thread %d", error); | ||
338 | return error; | ||
339 | } | ||
340 | ls->thread1 = p; | ||
341 | |||
342 | p = kthread_run(gdlm_thread, ls, "lock_dlm2"); | ||
343 | error = IS_ERR(p); | ||
344 | if (error) { | ||
345 | log_all("can't start lock_dlm2 thread %d", error); | ||
346 | kthread_stop(ls->thread1); | ||
347 | return error; | ||
348 | } | ||
349 | ls->thread2 = p; | ||
350 | |||
351 | return 0; | ||
352 | } | ||
353 | |||
354 | void gdlm_release_threads(struct gdlm_ls *ls) | ||
355 | { | ||
356 | kthread_stop(ls->thread1); | ||
357 | kthread_stop(ls->thread2); | ||
358 | } | ||
359 | |||