Commit | Line | Data |
---|---|---|
6714d8e8 KH |
1 | /* -*- mode: c; c-basic-offset: 8; -*- |
2 | * vim: noexpandtab sw=8 ts=8 sts=0: | |
3 | * | |
4 | * dlmthread.c | |
5 | * | |
6 | * standalone DLM module | |
7 | * | |
8 | * Copyright (C) 2004 Oracle. All rights reserved. | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or | |
11 | * modify it under the terms of the GNU General Public | |
12 | * License as published by the Free Software Foundation; either | |
13 | * version 2 of the License, or (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
18 | * General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public | |
21 | * License along with this program; if not, write to the | |
22 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 | * Boston, MA 021110-1307, USA. | |
24 | * | |
25 | */ | |
26 | ||
27 | ||
28 | #include <linux/module.h> | |
29 | #include <linux/fs.h> | |
30 | #include <linux/types.h> | |
31 | #include <linux/slab.h> | |
32 | #include <linux/highmem.h> | |
33 | #include <linux/utsname.h> | |
34 | #include <linux/init.h> | |
35 | #include <linux/sysctl.h> | |
36 | #include <linux/random.h> | |
37 | #include <linux/blkdev.h> | |
38 | #include <linux/socket.h> | |
39 | #include <linux/inet.h> | |
40 | #include <linux/timer.h> | |
41 | #include <linux/kthread.h> | |
8d79d088 | 42 | #include <linux/delay.h> |
6714d8e8 KH |
43 | |
44 | ||
45 | #include "cluster/heartbeat.h" | |
46 | #include "cluster/nodemanager.h" | |
47 | #include "cluster/tcp.h" | |
48 | ||
49 | #include "dlmapi.h" | |
50 | #include "dlmcommon.h" | |
51 | #include "dlmdomain.h" | |
52 | ||
53 | #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_THREAD) | |
54 | #include "cluster/masklog.h" | |
55 | ||
6714d8e8 KH |
56 | static int dlm_thread(void *data); |
57 | ||
58 | static void dlm_flush_asts(struct dlm_ctxt *dlm); | |
59 | ||
60 | #define dlm_lock_is_remote(dlm, lock) ((lock)->ml.node != (dlm)->node_num) | |
61 | ||
62 | /* will exit holding res->spinlock, but may drop in function */ | |
63 | /* waits until flags are cleared on res->state */ | |
64 | void __dlm_wait_on_lockres_flags(struct dlm_lock_resource *res, int flags) | |
65 | { | |
66 | DECLARE_WAITQUEUE(wait, current); | |
67 | ||
68 | assert_spin_locked(&res->spinlock); | |
69 | ||
70 | add_wait_queue(&res->wq, &wait); | |
71 | repeat: | |
72 | set_current_state(TASK_UNINTERRUPTIBLE); | |
73 | if (res->state & flags) { | |
74 | spin_unlock(&res->spinlock); | |
75 | schedule(); | |
76 | spin_lock(&res->spinlock); | |
77 | goto repeat; | |
78 | } | |
79 | remove_wait_queue(&res->wq, &wait); | |
80 | current->state = TASK_RUNNING; | |
81 | } | |
82 | ||
83 | ||
69d72b06 | 84 | int __dlm_lockres_unused(struct dlm_lock_resource *res) |
6714d8e8 KH |
85 | { |
86 | if (list_empty(&res->granted) && | |
87 | list_empty(&res->converting) && | |
88 | list_empty(&res->blocked) && | |
89 | list_empty(&res->dirty)) | |
90 | return 1; | |
91 | return 0; | |
92 | } | |
93 | ||
94 | ||
95 | /* Call whenever you may have added or deleted something from one of | |
96 | * the lockres queue's. This will figure out whether it belongs on the | |
97 | * unused list or not and does the appropriate thing. */ | |
98 | void __dlm_lockres_calc_usage(struct dlm_ctxt *dlm, | |
99 | struct dlm_lock_resource *res) | |
100 | { | |
101 | mlog_entry("%.*s\n", res->lockname.len, res->lockname.name); | |
102 | ||
103 | assert_spin_locked(&dlm->spinlock); | |
104 | assert_spin_locked(&res->spinlock); | |
105 | ||
106 | if (__dlm_lockres_unused(res)){ | |
107 | if (list_empty(&res->purge)) { | |
108 | mlog(0, "putting lockres %.*s from purge list\n", | |
109 | res->lockname.len, res->lockname.name); | |
110 | ||
111 | res->last_used = jiffies; | |
112 | list_add_tail(&res->purge, &dlm->purge_list); | |
113 | dlm->purge_count++; | |
114 | } | |
115 | } else if (!list_empty(&res->purge)) { | |
116 | mlog(0, "removing lockres %.*s from purge list\n", | |
117 | res->lockname.len, res->lockname.name); | |
118 | ||
119 | list_del_init(&res->purge); | |
120 | dlm->purge_count--; | |
121 | } | |
122 | } | |
123 | ||
124 | void dlm_lockres_calc_usage(struct dlm_ctxt *dlm, | |
125 | struct dlm_lock_resource *res) | |
126 | { | |
127 | mlog_entry("%.*s\n", res->lockname.len, res->lockname.name); | |
128 | spin_lock(&dlm->spinlock); | |
129 | spin_lock(&res->spinlock); | |
130 | ||
131 | __dlm_lockres_calc_usage(dlm, res); | |
132 | ||
133 | spin_unlock(&res->spinlock); | |
134 | spin_unlock(&dlm->spinlock); | |
135 | } | |
136 | ||
137 | /* TODO: Eventual API: Called with the dlm spinlock held, may drop it | |
138 | * to do migration, but will re-acquire before exit. */ | |
139 | void dlm_purge_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *lockres) | |
140 | { | |
141 | int master; | |
142 | int ret; | |
143 | ||
144 | spin_lock(&lockres->spinlock); | |
145 | master = lockres->owner == dlm->node_num; | |
146 | spin_unlock(&lockres->spinlock); | |
147 | ||
148 | mlog(0, "purging lockres %.*s, master = %d\n", lockres->lockname.len, | |
149 | lockres->lockname.name, master); | |
150 | ||
151 | /* Non master is the easy case -- no migration required, just | |
152 | * quit. */ | |
153 | if (!master) | |
154 | goto finish; | |
155 | ||
156 | /* Wheee! Migrate lockres here! */ | |
157 | spin_unlock(&dlm->spinlock); | |
158 | again: | |
159 | ||
160 | ret = dlm_migrate_lockres(dlm, lockres, O2NM_MAX_NODES); | |
161 | if (ret == -ENOTEMPTY) { | |
162 | mlog(ML_ERROR, "lockres %.*s still has local locks!\n", | |
163 | lockres->lockname.len, lockres->lockname.name); | |
164 | ||
165 | BUG(); | |
166 | } else if (ret < 0) { | |
167 | mlog(ML_NOTICE, "lockres %.*s: migrate failed, retrying\n", | |
168 | lockres->lockname.len, lockres->lockname.name); | |
8d79d088 | 169 | msleep(100); |
6714d8e8 KH |
170 | goto again; |
171 | } | |
172 | ||
173 | spin_lock(&dlm->spinlock); | |
174 | ||
175 | finish: | |
176 | if (!list_empty(&lockres->purge)) { | |
177 | list_del_init(&lockres->purge); | |
178 | dlm->purge_count--; | |
179 | } | |
180 | __dlm_unhash_lockres(lockres); | |
181 | } | |
182 | ||
183 | static void dlm_run_purge_list(struct dlm_ctxt *dlm, | |
184 | int purge_now) | |
185 | { | |
186 | unsigned int run_max, unused; | |
187 | unsigned long purge_jiffies; | |
188 | struct dlm_lock_resource *lockres; | |
189 | ||
190 | spin_lock(&dlm->spinlock); | |
191 | run_max = dlm->purge_count; | |
192 | ||
193 | while(run_max && !list_empty(&dlm->purge_list)) { | |
194 | run_max--; | |
195 | ||
196 | lockres = list_entry(dlm->purge_list.next, | |
197 | struct dlm_lock_resource, purge); | |
198 | ||
199 | /* Status of the lockres *might* change so double | |
200 | * check. If the lockres is unused, holding the dlm | |
201 | * spinlock will prevent people from getting and more | |
202 | * refs on it -- there's no need to keep the lockres | |
203 | * spinlock. */ | |
204 | spin_lock(&lockres->spinlock); | |
205 | unused = __dlm_lockres_unused(lockres); | |
206 | spin_unlock(&lockres->spinlock); | |
207 | ||
208 | if (!unused) | |
209 | continue; | |
210 | ||
211 | purge_jiffies = lockres->last_used + | |
212 | msecs_to_jiffies(DLM_PURGE_INTERVAL_MS); | |
213 | ||
214 | /* Make sure that we want to be processing this guy at | |
215 | * this time. */ | |
216 | if (!purge_now && time_after(purge_jiffies, jiffies)) { | |
217 | /* Since resources are added to the purge list | |
218 | * in tail order, we can stop at the first | |
219 | * unpurgable resource -- anyone added after | |
220 | * him will have a greater last_used value */ | |
221 | break; | |
222 | } | |
223 | ||
224 | list_del_init(&lockres->purge); | |
225 | dlm->purge_count--; | |
226 | ||
227 | /* This may drop and reacquire the dlm spinlock if it | |
228 | * has to do migration. */ | |
229 | mlog(0, "calling dlm_purge_lockres!\n"); | |
230 | dlm_purge_lockres(dlm, lockres); | |
231 | mlog(0, "DONE calling dlm_purge_lockres!\n"); | |
232 | ||
233 | /* Avoid adding any scheduling latencies */ | |
234 | cond_resched_lock(&dlm->spinlock); | |
235 | } | |
236 | ||
237 | spin_unlock(&dlm->spinlock); | |
238 | } | |
239 | ||
240 | static void dlm_shuffle_lists(struct dlm_ctxt *dlm, | |
241 | struct dlm_lock_resource *res) | |
242 | { | |
243 | struct dlm_lock *lock, *target; | |
244 | struct list_head *iter; | |
245 | struct list_head *head; | |
246 | int can_grant = 1; | |
247 | ||
248 | //mlog(0, "res->lockname.len=%d\n", res->lockname.len); | |
249 | //mlog(0, "res->lockname.name=%p\n", res->lockname.name); | |
250 | //mlog(0, "shuffle res %.*s\n", res->lockname.len, | |
251 | // res->lockname.name); | |
252 | ||
253 | /* because this function is called with the lockres | |
254 | * spinlock, and because we know that it is not migrating/ | |
255 | * recovering/in-progress, it is fine to reserve asts and | |
256 | * basts right before queueing them all throughout */ | |
257 | assert_spin_locked(&res->spinlock); | |
258 | BUG_ON((res->state & (DLM_LOCK_RES_MIGRATING| | |
259 | DLM_LOCK_RES_RECOVERING| | |
260 | DLM_LOCK_RES_IN_PROGRESS))); | |
261 | ||
262 | converting: | |
263 | if (list_empty(&res->converting)) | |
264 | goto blocked; | |
265 | mlog(0, "res %.*s has locks on a convert queue\n", res->lockname.len, | |
266 | res->lockname.name); | |
267 | ||
268 | target = list_entry(res->converting.next, struct dlm_lock, list); | |
269 | if (target->ml.convert_type == LKM_IVMODE) { | |
270 | mlog(ML_ERROR, "%.*s: converting a lock with no " | |
271 | "convert_type!\n", res->lockname.len, res->lockname.name); | |
272 | BUG(); | |
273 | } | |
274 | head = &res->granted; | |
275 | list_for_each(iter, head) { | |
276 | lock = list_entry(iter, struct dlm_lock, list); | |
277 | if (lock==target) | |
278 | continue; | |
279 | if (!dlm_lock_compatible(lock->ml.type, | |
280 | target->ml.convert_type)) { | |
281 | can_grant = 0; | |
282 | /* queue the BAST if not already */ | |
283 | if (lock->ml.highest_blocked == LKM_IVMODE) { | |
284 | __dlm_lockres_reserve_ast(res); | |
285 | dlm_queue_bast(dlm, lock); | |
286 | } | |
287 | /* update the highest_blocked if needed */ | |
288 | if (lock->ml.highest_blocked < target->ml.convert_type) | |
289 | lock->ml.highest_blocked = | |
290 | target->ml.convert_type; | |
291 | } | |
292 | } | |
293 | head = &res->converting; | |
294 | list_for_each(iter, head) { | |
295 | lock = list_entry(iter, struct dlm_lock, list); | |
296 | if (lock==target) | |
297 | continue; | |
298 | if (!dlm_lock_compatible(lock->ml.type, | |
299 | target->ml.convert_type)) { | |
300 | can_grant = 0; | |
301 | if (lock->ml.highest_blocked == LKM_IVMODE) { | |
302 | __dlm_lockres_reserve_ast(res); | |
303 | dlm_queue_bast(dlm, lock); | |
304 | } | |
305 | if (lock->ml.highest_blocked < target->ml.convert_type) | |
306 | lock->ml.highest_blocked = | |
307 | target->ml.convert_type; | |
308 | } | |
309 | } | |
310 | ||
311 | /* we can convert the lock */ | |
312 | if (can_grant) { | |
313 | spin_lock(&target->spinlock); | |
314 | BUG_ON(target->ml.highest_blocked != LKM_IVMODE); | |
315 | ||
316 | mlog(0, "calling ast for converting lock: %.*s, have: %d, " | |
317 | "granting: %d, node: %u\n", res->lockname.len, | |
318 | res->lockname.name, target->ml.type, | |
319 | target->ml.convert_type, target->ml.node); | |
320 | ||
321 | target->ml.type = target->ml.convert_type; | |
322 | target->ml.convert_type = LKM_IVMODE; | |
f116629d | 323 | list_move_tail(&target->list, &res->granted); |
6714d8e8 KH |
324 | |
325 | BUG_ON(!target->lksb); | |
326 | target->lksb->status = DLM_NORMAL; | |
327 | ||
328 | spin_unlock(&target->spinlock); | |
329 | ||
330 | __dlm_lockres_reserve_ast(res); | |
331 | dlm_queue_ast(dlm, target); | |
332 | /* go back and check for more */ | |
333 | goto converting; | |
334 | } | |
335 | ||
336 | blocked: | |
337 | if (list_empty(&res->blocked)) | |
338 | goto leave; | |
339 | target = list_entry(res->blocked.next, struct dlm_lock, list); | |
340 | ||
341 | head = &res->granted; | |
342 | list_for_each(iter, head) { | |
343 | lock = list_entry(iter, struct dlm_lock, list); | |
344 | if (lock==target) | |
345 | continue; | |
346 | if (!dlm_lock_compatible(lock->ml.type, target->ml.type)) { | |
347 | can_grant = 0; | |
348 | if (lock->ml.highest_blocked == LKM_IVMODE) { | |
349 | __dlm_lockres_reserve_ast(res); | |
350 | dlm_queue_bast(dlm, lock); | |
351 | } | |
352 | if (lock->ml.highest_blocked < target->ml.type) | |
353 | lock->ml.highest_blocked = target->ml.type; | |
354 | } | |
355 | } | |
356 | ||
357 | head = &res->converting; | |
358 | list_for_each(iter, head) { | |
359 | lock = list_entry(iter, struct dlm_lock, list); | |
360 | if (lock==target) | |
361 | continue; | |
362 | if (!dlm_lock_compatible(lock->ml.type, target->ml.type)) { | |
363 | can_grant = 0; | |
364 | if (lock->ml.highest_blocked == LKM_IVMODE) { | |
365 | __dlm_lockres_reserve_ast(res); | |
366 | dlm_queue_bast(dlm, lock); | |
367 | } | |
368 | if (lock->ml.highest_blocked < target->ml.type) | |
369 | lock->ml.highest_blocked = target->ml.type; | |
370 | } | |
371 | } | |
372 | ||
373 | /* we can grant the blocked lock (only | |
374 | * possible if converting list empty) */ | |
375 | if (can_grant) { | |
376 | spin_lock(&target->spinlock); | |
377 | BUG_ON(target->ml.highest_blocked != LKM_IVMODE); | |
378 | ||
379 | mlog(0, "calling ast for blocked lock: %.*s, granting: %d, " | |
380 | "node: %u\n", res->lockname.len, res->lockname.name, | |
381 | target->ml.type, target->ml.node); | |
382 | ||
383 | // target->ml.type is already correct | |
f116629d | 384 | list_move_tail(&target->list, &res->granted); |
6714d8e8 KH |
385 | |
386 | BUG_ON(!target->lksb); | |
387 | target->lksb->status = DLM_NORMAL; | |
388 | ||
389 | spin_unlock(&target->spinlock); | |
390 | ||
391 | __dlm_lockres_reserve_ast(res); | |
392 | dlm_queue_ast(dlm, target); | |
393 | /* go back and check for more */ | |
394 | goto converting; | |
395 | } | |
396 | ||
397 | leave: | |
398 | return; | |
399 | } | |
400 | ||
401 | /* must have NO locks when calling this with res !=NULL * */ | |
402 | void dlm_kick_thread(struct dlm_ctxt *dlm, struct dlm_lock_resource *res) | |
403 | { | |
404 | mlog_entry("dlm=%p, res=%p\n", dlm, res); | |
405 | if (res) { | |
406 | spin_lock(&dlm->spinlock); | |
407 | spin_lock(&res->spinlock); | |
408 | __dlm_dirty_lockres(dlm, res); | |
409 | spin_unlock(&res->spinlock); | |
410 | spin_unlock(&dlm->spinlock); | |
411 | } | |
412 | wake_up(&dlm->dlm_thread_wq); | |
413 | } | |
414 | ||
415 | void __dlm_dirty_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res) | |
416 | { | |
417 | mlog_entry("dlm=%p, res=%p\n", dlm, res); | |
418 | ||
419 | assert_spin_locked(&dlm->spinlock); | |
420 | assert_spin_locked(&res->spinlock); | |
421 | ||
422 | /* don't shuffle secondary queues */ | |
423 | if ((res->owner == dlm->node_num) && | |
424 | !(res->state & DLM_LOCK_RES_DIRTY)) { | |
425 | list_add_tail(&res->dirty, &dlm->dirty_list); | |
426 | res->state |= DLM_LOCK_RES_DIRTY; | |
427 | } | |
428 | } | |
429 | ||
430 | ||
431 | /* Launch the NM thread for the mounted volume */ | |
432 | int dlm_launch_thread(struct dlm_ctxt *dlm) | |
433 | { | |
434 | mlog(0, "starting dlm thread...\n"); | |
435 | ||
436 | dlm->dlm_thread_task = kthread_run(dlm_thread, dlm, "dlm_thread"); | |
437 | if (IS_ERR(dlm->dlm_thread_task)) { | |
438 | mlog_errno(PTR_ERR(dlm->dlm_thread_task)); | |
439 | dlm->dlm_thread_task = NULL; | |
440 | return -EINVAL; | |
441 | } | |
442 | ||
443 | return 0; | |
444 | } | |
445 | ||
446 | void dlm_complete_thread(struct dlm_ctxt *dlm) | |
447 | { | |
448 | if (dlm->dlm_thread_task) { | |
449 | mlog(ML_KTHREAD, "waiting for dlm thread to exit\n"); | |
450 | kthread_stop(dlm->dlm_thread_task); | |
451 | dlm->dlm_thread_task = NULL; | |
452 | } | |
453 | } | |
454 | ||
455 | static int dlm_dirty_list_empty(struct dlm_ctxt *dlm) | |
456 | { | |
457 | int empty; | |
458 | ||
459 | spin_lock(&dlm->spinlock); | |
460 | empty = list_empty(&dlm->dirty_list); | |
461 | spin_unlock(&dlm->spinlock); | |
462 | ||
463 | return empty; | |
464 | } | |
465 | ||
466 | static void dlm_flush_asts(struct dlm_ctxt *dlm) | |
467 | { | |
468 | int ret; | |
469 | struct dlm_lock *lock; | |
470 | struct dlm_lock_resource *res; | |
471 | u8 hi; | |
472 | ||
473 | spin_lock(&dlm->ast_lock); | |
474 | while (!list_empty(&dlm->pending_asts)) { | |
475 | lock = list_entry(dlm->pending_asts.next, | |
476 | struct dlm_lock, ast_list); | |
477 | /* get an extra ref on lock */ | |
478 | dlm_lock_get(lock); | |
479 | res = lock->lockres; | |
480 | mlog(0, "delivering an ast for this lockres\n"); | |
481 | ||
482 | BUG_ON(!lock->ast_pending); | |
483 | ||
484 | /* remove from list (including ref) */ | |
485 | list_del_init(&lock->ast_list); | |
486 | dlm_lock_put(lock); | |
487 | spin_unlock(&dlm->ast_lock); | |
488 | ||
489 | if (lock->ml.node != dlm->node_num) { | |
490 | ret = dlm_do_remote_ast(dlm, res, lock); | |
491 | if (ret < 0) | |
492 | mlog_errno(ret); | |
493 | } else | |
494 | dlm_do_local_ast(dlm, res, lock); | |
495 | ||
496 | spin_lock(&dlm->ast_lock); | |
497 | ||
498 | /* possible that another ast was queued while | |
499 | * we were delivering the last one */ | |
500 | if (!list_empty(&lock->ast_list)) { | |
501 | mlog(0, "aha another ast got queued while " | |
502 | "we were finishing the last one. will " | |
503 | "keep the ast_pending flag set.\n"); | |
504 | } else | |
505 | lock->ast_pending = 0; | |
506 | ||
507 | /* drop the extra ref. | |
508 | * this may drop it completely. */ | |
509 | dlm_lock_put(lock); | |
510 | dlm_lockres_release_ast(dlm, res); | |
511 | } | |
512 | ||
513 | while (!list_empty(&dlm->pending_basts)) { | |
514 | lock = list_entry(dlm->pending_basts.next, | |
515 | struct dlm_lock, bast_list); | |
516 | /* get an extra ref on lock */ | |
517 | dlm_lock_get(lock); | |
518 | res = lock->lockres; | |
519 | ||
520 | BUG_ON(!lock->bast_pending); | |
521 | ||
522 | /* get the highest blocked lock, and reset */ | |
523 | spin_lock(&lock->spinlock); | |
524 | BUG_ON(lock->ml.highest_blocked <= LKM_IVMODE); | |
525 | hi = lock->ml.highest_blocked; | |
526 | lock->ml.highest_blocked = LKM_IVMODE; | |
527 | spin_unlock(&lock->spinlock); | |
528 | ||
529 | /* remove from list (including ref) */ | |
530 | list_del_init(&lock->bast_list); | |
531 | dlm_lock_put(lock); | |
532 | spin_unlock(&dlm->ast_lock); | |
533 | ||
534 | mlog(0, "delivering a bast for this lockres " | |
535 | "(blocked = %d\n", hi); | |
536 | ||
537 | if (lock->ml.node != dlm->node_num) { | |
538 | ret = dlm_send_proxy_bast(dlm, res, lock, hi); | |
539 | if (ret < 0) | |
540 | mlog_errno(ret); | |
541 | } else | |
542 | dlm_do_local_bast(dlm, res, lock, hi); | |
543 | ||
544 | spin_lock(&dlm->ast_lock); | |
545 | ||
546 | /* possible that another bast was queued while | |
547 | * we were delivering the last one */ | |
548 | if (!list_empty(&lock->bast_list)) { | |
549 | mlog(0, "aha another bast got queued while " | |
550 | "we were finishing the last one. will " | |
551 | "keep the bast_pending flag set.\n"); | |
552 | } else | |
553 | lock->bast_pending = 0; | |
554 | ||
555 | /* drop the extra ref. | |
556 | * this may drop it completely. */ | |
557 | dlm_lock_put(lock); | |
558 | dlm_lockres_release_ast(dlm, res); | |
559 | } | |
560 | wake_up(&dlm->ast_wq); | |
561 | spin_unlock(&dlm->ast_lock); | |
562 | } | |
563 | ||
564 | ||
565 | #define DLM_THREAD_TIMEOUT_MS (4 * 1000) | |
566 | #define DLM_THREAD_MAX_DIRTY 100 | |
567 | #define DLM_THREAD_MAX_ASTS 10 | |
568 | ||
569 | static int dlm_thread(void *data) | |
570 | { | |
571 | struct dlm_lock_resource *res; | |
572 | struct dlm_ctxt *dlm = data; | |
573 | unsigned long timeout = msecs_to_jiffies(DLM_THREAD_TIMEOUT_MS); | |
574 | ||
575 | mlog(0, "dlm thread running for %s...\n", dlm->name); | |
576 | ||
577 | while (!kthread_should_stop()) { | |
578 | int n = DLM_THREAD_MAX_DIRTY; | |
579 | ||
580 | /* dlm_shutting_down is very point-in-time, but that | |
581 | * doesn't matter as we'll just loop back around if we | |
582 | * get false on the leading edge of a state | |
583 | * transition. */ | |
584 | dlm_run_purge_list(dlm, dlm_shutting_down(dlm)); | |
585 | ||
586 | /* We really don't want to hold dlm->spinlock while | |
587 | * calling dlm_shuffle_lists on each lockres that | |
588 | * needs to have its queues adjusted and AST/BASTs | |
589 | * run. So let's pull each entry off the dirty_list | |
590 | * and drop dlm->spinlock ASAP. Once off the list, | |
591 | * res->spinlock needs to be taken again to protect | |
592 | * the queues while calling dlm_shuffle_lists. */ | |
593 | spin_lock(&dlm->spinlock); | |
594 | while (!list_empty(&dlm->dirty_list)) { | |
595 | int delay = 0; | |
596 | res = list_entry(dlm->dirty_list.next, | |
597 | struct dlm_lock_resource, dirty); | |
598 | ||
599 | /* peel a lockres off, remove it from the list, | |
600 | * unset the dirty flag and drop the dlm lock */ | |
601 | BUG_ON(!res); | |
602 | dlm_lockres_get(res); | |
603 | ||
604 | spin_lock(&res->spinlock); | |
605 | res->state &= ~DLM_LOCK_RES_DIRTY; | |
606 | list_del_init(&res->dirty); | |
607 | spin_unlock(&res->spinlock); | |
608 | spin_unlock(&dlm->spinlock); | |
609 | ||
610 | /* lockres can be re-dirtied/re-added to the | |
611 | * dirty_list in this gap, but that is ok */ | |
612 | ||
613 | spin_lock(&res->spinlock); | |
614 | if (res->owner != dlm->node_num) { | |
615 | __dlm_print_one_lock_resource(res); | |
616 | mlog(ML_ERROR, "inprog:%s, mig:%s, reco:%s, dirty:%s\n", | |
617 | res->state & DLM_LOCK_RES_IN_PROGRESS ? "yes" : "no", | |
618 | res->state & DLM_LOCK_RES_MIGRATING ? "yes" : "no", | |
619 | res->state & DLM_LOCK_RES_RECOVERING ? "yes" : "no", | |
620 | res->state & DLM_LOCK_RES_DIRTY ? "yes" : "no"); | |
621 | } | |
622 | BUG_ON(res->owner != dlm->node_num); | |
623 | ||
624 | /* it is now ok to move lockreses in these states | |
625 | * to the dirty list, assuming that they will only be | |
626 | * dirty for a short while. */ | |
627 | if (res->state & (DLM_LOCK_RES_IN_PROGRESS | | |
628 | DLM_LOCK_RES_MIGRATING | | |
629 | DLM_LOCK_RES_RECOVERING)) { | |
630 | /* move it to the tail and keep going */ | |
631 | spin_unlock(&res->spinlock); | |
632 | mlog(0, "delaying list shuffling for in-" | |
633 | "progress lockres %.*s, state=%d\n", | |
634 | res->lockname.len, res->lockname.name, | |
635 | res->state); | |
636 | delay = 1; | |
637 | goto in_progress; | |
638 | } | |
639 | ||
640 | /* at this point the lockres is not migrating/ | |
641 | * recovering/in-progress. we have the lockres | |
642 | * spinlock and do NOT have the dlm lock. | |
643 | * safe to reserve/queue asts and run the lists. */ | |
644 | ||
8d79d088 KH |
645 | mlog(0, "calling dlm_shuffle_lists with dlm=%s, " |
646 | "res=%.*s\n", dlm->name, | |
647 | res->lockname.len, res->lockname.name); | |
6714d8e8 KH |
648 | |
649 | /* called while holding lockres lock */ | |
650 | dlm_shuffle_lists(dlm, res); | |
651 | spin_unlock(&res->spinlock); | |
652 | ||
653 | dlm_lockres_calc_usage(dlm, res); | |
654 | ||
655 | in_progress: | |
656 | ||
657 | spin_lock(&dlm->spinlock); | |
658 | /* if the lock was in-progress, stick | |
659 | * it on the back of the list */ | |
660 | if (delay) { | |
661 | spin_lock(&res->spinlock); | |
662 | list_add_tail(&res->dirty, &dlm->dirty_list); | |
663 | res->state |= DLM_LOCK_RES_DIRTY; | |
664 | spin_unlock(&res->spinlock); | |
665 | } | |
666 | dlm_lockres_put(res); | |
667 | ||
668 | /* unlikely, but we may need to give time to | |
669 | * other tasks */ | |
670 | if (!--n) { | |
671 | mlog(0, "throttling dlm_thread\n"); | |
672 | break; | |
673 | } | |
674 | } | |
675 | ||
676 | spin_unlock(&dlm->spinlock); | |
677 | dlm_flush_asts(dlm); | |
678 | ||
679 | /* yield and continue right away if there is more work to do */ | |
680 | if (!n) { | |
681 | yield(); | |
682 | continue; | |
683 | } | |
684 | ||
685 | wait_event_interruptible_timeout(dlm->dlm_thread_wq, | |
686 | !dlm_dirty_list_empty(dlm) || | |
687 | kthread_should_stop(), | |
688 | timeout); | |
689 | } | |
690 | ||
691 | mlog(0, "quitting DLM thread\n"); | |
692 | return 0; | |
693 | } |