blob: 620eb824ce1ddfa838566f703411ad0f1a072ed8 [file] [log] [blame]
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001/* -*- 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>
Kurt Hackel8d79d082006-04-27 17:58:23 -070042#include <linux/delay.h>
Kurt Hackel6714d8e2005-12-15 14:31:23 -080043
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
Kurt Hackel6714d8e2005-12-15 14:31:23 -080056static int dlm_thread(void *data);
Kurt Hackel6714d8e2005-12-15 14:31:23 -080057static void dlm_flush_asts(struct dlm_ctxt *dlm);
58
59#define dlm_lock_is_remote(dlm, lock) ((lock)->ml.node != (dlm)->node_num)
60
61/* will exit holding res->spinlock, but may drop in function */
62/* waits until flags are cleared on res->state */
63void __dlm_wait_on_lockres_flags(struct dlm_lock_resource *res, int flags)
64{
65 DECLARE_WAITQUEUE(wait, current);
66
67 assert_spin_locked(&res->spinlock);
68
69 add_wait_queue(&res->wq, &wait);
70repeat:
71 set_current_state(TASK_UNINTERRUPTIBLE);
72 if (res->state & flags) {
73 spin_unlock(&res->spinlock);
74 schedule();
75 spin_lock(&res->spinlock);
76 goto repeat;
77 }
78 remove_wait_queue(&res->wq, &wait);
79 current->state = TASK_RUNNING;
80}
81
Kurt Hackelba2bf212006-12-01 14:47:20 -080082int __dlm_lockres_has_locks(struct dlm_lock_resource *res)
Kurt Hackel6714d8e2005-12-15 14:31:23 -080083{
84 if (list_empty(&res->granted) &&
85 list_empty(&res->converting) &&
Kurt Hackelba2bf212006-12-01 14:47:20 -080086 list_empty(&res->blocked))
87 return 0;
88 return 1;
89}
90
91/* "unused": the lockres has no locks, is not on the dirty list,
92 * has no inflight locks (in the gap between mastery and acquiring
93 * the first lock), and has no bits in its refmap.
94 * truly ready to be freed. */
95int __dlm_lockres_unused(struct dlm_lock_resource *res)
96{
97 if (!__dlm_lockres_has_locks(res) &&
98 list_empty(&res->dirty)) {
99 /* try not to scan the bitmap unless the first two
100 * conditions are already true */
101 int bit = find_next_bit(res->refmap, O2NM_MAX_NODES, 0);
102 if (bit >= O2NM_MAX_NODES) {
103 /* since the bit for dlm->node_num is not
104 * set, inflight_locks better be zero */
105 BUG_ON(res->inflight_locks != 0);
106 return 1;
107 }
108 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800109 return 0;
110}
111
112
113/* Call whenever you may have added or deleted something from one of
114 * the lockres queue's. This will figure out whether it belongs on the
115 * unused list or not and does the appropriate thing. */
116void __dlm_lockres_calc_usage(struct dlm_ctxt *dlm,
117 struct dlm_lock_resource *res)
118{
119 mlog_entry("%.*s\n", res->lockname.len, res->lockname.name);
120
121 assert_spin_locked(&dlm->spinlock);
122 assert_spin_locked(&res->spinlock);
123
124 if (__dlm_lockres_unused(res)){
125 if (list_empty(&res->purge)) {
Kurt Hackelba2bf212006-12-01 14:47:20 -0800126 mlog(0, "putting lockres %.*s:%p onto purge list\n",
127 res->lockname.len, res->lockname.name, res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800128
129 res->last_used = jiffies;
Kurt Hackelba2bf212006-12-01 14:47:20 -0800130 dlm_lockres_get(res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800131 list_add_tail(&res->purge, &dlm->purge_list);
132 dlm->purge_count++;
133 }
134 } else if (!list_empty(&res->purge)) {
Kurt Hackelba2bf212006-12-01 14:47:20 -0800135 mlog(0, "removing lockres %.*s:%p from purge list, owner=%u\n",
136 res->lockname.len, res->lockname.name, res, res->owner);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800137
138 list_del_init(&res->purge);
Kurt Hackelba2bf212006-12-01 14:47:20 -0800139 dlm_lockres_put(res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800140 dlm->purge_count--;
141 }
142}
143
144void dlm_lockres_calc_usage(struct dlm_ctxt *dlm,
145 struct dlm_lock_resource *res)
146{
147 mlog_entry("%.*s\n", res->lockname.len, res->lockname.name);
148 spin_lock(&dlm->spinlock);
149 spin_lock(&res->spinlock);
150
151 __dlm_lockres_calc_usage(dlm, res);
152
153 spin_unlock(&res->spinlock);
154 spin_unlock(&dlm->spinlock);
155}
156
Kurt Hackelba2bf212006-12-01 14:47:20 -0800157int dlm_purge_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800158{
159 int master;
Kurt Hackelba2bf212006-12-01 14:47:20 -0800160 int ret = 0;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800161
Kurt Hackelba2bf212006-12-01 14:47:20 -0800162 spin_lock(&res->spinlock);
163 if (!__dlm_lockres_unused(res)) {
164 spin_unlock(&res->spinlock);
165 mlog(0, "%s:%.*s: tried to purge but not unused\n",
166 dlm->name, res->lockname.len, res->lockname.name);
167 return -ENOTEMPTY;
168 }
169 master = (res->owner == dlm->node_num);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800170 if (!master)
Kurt Hackelba2bf212006-12-01 14:47:20 -0800171 res->state |= DLM_LOCK_RES_DROPPING_REF;
172 spin_unlock(&res->spinlock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800173
Kurt Hackelba2bf212006-12-01 14:47:20 -0800174 mlog(0, "purging lockres %.*s, master = %d\n", res->lockname.len,
175 res->lockname.name, master);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800176
Kurt Hackelba2bf212006-12-01 14:47:20 -0800177 if (!master) {
178 /* drop spinlock to do messaging, retake below */
179 spin_unlock(&dlm->spinlock);
180 /* clear our bit from the master's refmap, ignore errors */
181 ret = dlm_drop_lockres_ref(dlm, res);
182 if (ret < 0) {
183 mlog_errno(ret);
184 if (!dlm_is_host_down(ret))
185 BUG();
186 }
187 mlog(0, "%s:%.*s: dlm_deref_lockres returned %d\n",
188 dlm->name, res->lockname.len, res->lockname.name, ret);
189 spin_lock(&dlm->spinlock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800190 }
191
Kurt Hackelba2bf212006-12-01 14:47:20 -0800192 if (!list_empty(&res->purge)) {
193 mlog(0, "removing lockres %.*s:%p from purgelist, "
194 "master = %d\n", res->lockname.len, res->lockname.name,
195 res, master);
196 list_del_init(&res->purge);
197 dlm_lockres_put(res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800198 dlm->purge_count--;
199 }
Kurt Hackelba2bf212006-12-01 14:47:20 -0800200 __dlm_unhash_lockres(res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800201
Kurt Hackelba2bf212006-12-01 14:47:20 -0800202 /* lockres is not in the hash now. drop the flag and wake up
203 * any processes waiting in dlm_get_lock_resource. */
204 if (!master) {
205 spin_lock(&res->spinlock);
206 res->state &= ~DLM_LOCK_RES_DROPPING_REF;
207 spin_unlock(&res->spinlock);
208 wake_up(&res->wq);
Kurt Hackel8b219802006-05-01 11:16:45 -0700209 }
Kurt Hackelba2bf212006-12-01 14:47:20 -0800210 return 0;
Kurt Hackel8b219802006-05-01 11:16:45 -0700211}
212
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800213static void dlm_run_purge_list(struct dlm_ctxt *dlm,
214 int purge_now)
215{
216 unsigned int run_max, unused;
217 unsigned long purge_jiffies;
218 struct dlm_lock_resource *lockres;
219
220 spin_lock(&dlm->spinlock);
221 run_max = dlm->purge_count;
222
223 while(run_max && !list_empty(&dlm->purge_list)) {
224 run_max--;
225
226 lockres = list_entry(dlm->purge_list.next,
227 struct dlm_lock_resource, purge);
228
229 /* Status of the lockres *might* change so double
230 * check. If the lockres is unused, holding the dlm
231 * spinlock will prevent people from getting and more
232 * refs on it -- there's no need to keep the lockres
233 * spinlock. */
234 spin_lock(&lockres->spinlock);
235 unused = __dlm_lockres_unused(lockres);
236 spin_unlock(&lockres->spinlock);
237
238 if (!unused)
239 continue;
240
241 purge_jiffies = lockres->last_used +
242 msecs_to_jiffies(DLM_PURGE_INTERVAL_MS);
243
244 /* Make sure that we want to be processing this guy at
245 * this time. */
246 if (!purge_now && time_after(purge_jiffies, jiffies)) {
247 /* Since resources are added to the purge list
248 * in tail order, we can stop at the first
249 * unpurgable resource -- anyone added after
250 * him will have a greater last_used value */
251 break;
252 }
253
Kurt Hackelba2bf212006-12-01 14:47:20 -0800254 mlog(0, "removing lockres %.*s:%p from purgelist\n",
255 lockres->lockname.len, lockres->lockname.name, lockres);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800256 list_del_init(&lockres->purge);
Kurt Hackelba2bf212006-12-01 14:47:20 -0800257 dlm_lockres_put(lockres);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800258 dlm->purge_count--;
259
260 /* This may drop and reacquire the dlm spinlock if it
261 * has to do migration. */
262 mlog(0, "calling dlm_purge_lockres!\n");
Kurt Hackelba2bf212006-12-01 14:47:20 -0800263 if (dlm_purge_lockres(dlm, lockres))
264 BUG();
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800265 mlog(0, "DONE calling dlm_purge_lockres!\n");
266
267 /* Avoid adding any scheduling latencies */
268 cond_resched_lock(&dlm->spinlock);
269 }
270
271 spin_unlock(&dlm->spinlock);
272}
273
274static void dlm_shuffle_lists(struct dlm_ctxt *dlm,
275 struct dlm_lock_resource *res)
276{
277 struct dlm_lock *lock, *target;
278 struct list_head *iter;
279 struct list_head *head;
280 int can_grant = 1;
281
282 //mlog(0, "res->lockname.len=%d\n", res->lockname.len);
283 //mlog(0, "res->lockname.name=%p\n", res->lockname.name);
284 //mlog(0, "shuffle res %.*s\n", res->lockname.len,
285 // res->lockname.name);
286
287 /* because this function is called with the lockres
288 * spinlock, and because we know that it is not migrating/
289 * recovering/in-progress, it is fine to reserve asts and
290 * basts right before queueing them all throughout */
291 assert_spin_locked(&res->spinlock);
292 BUG_ON((res->state & (DLM_LOCK_RES_MIGRATING|
293 DLM_LOCK_RES_RECOVERING|
294 DLM_LOCK_RES_IN_PROGRESS)));
295
296converting:
297 if (list_empty(&res->converting))
298 goto blocked;
299 mlog(0, "res %.*s has locks on a convert queue\n", res->lockname.len,
300 res->lockname.name);
301
302 target = list_entry(res->converting.next, struct dlm_lock, list);
303 if (target->ml.convert_type == LKM_IVMODE) {
304 mlog(ML_ERROR, "%.*s: converting a lock with no "
305 "convert_type!\n", res->lockname.len, res->lockname.name);
306 BUG();
307 }
308 head = &res->granted;
309 list_for_each(iter, head) {
310 lock = list_entry(iter, struct dlm_lock, list);
311 if (lock==target)
312 continue;
313 if (!dlm_lock_compatible(lock->ml.type,
314 target->ml.convert_type)) {
315 can_grant = 0;
316 /* queue the BAST if not already */
317 if (lock->ml.highest_blocked == LKM_IVMODE) {
318 __dlm_lockres_reserve_ast(res);
319 dlm_queue_bast(dlm, lock);
320 }
321 /* update the highest_blocked if needed */
322 if (lock->ml.highest_blocked < target->ml.convert_type)
323 lock->ml.highest_blocked =
324 target->ml.convert_type;
325 }
326 }
327 head = &res->converting;
328 list_for_each(iter, head) {
329 lock = list_entry(iter, struct dlm_lock, list);
330 if (lock==target)
331 continue;
332 if (!dlm_lock_compatible(lock->ml.type,
333 target->ml.convert_type)) {
334 can_grant = 0;
335 if (lock->ml.highest_blocked == LKM_IVMODE) {
336 __dlm_lockres_reserve_ast(res);
337 dlm_queue_bast(dlm, lock);
338 }
339 if (lock->ml.highest_blocked < target->ml.convert_type)
340 lock->ml.highest_blocked =
341 target->ml.convert_type;
342 }
343 }
344
345 /* we can convert the lock */
346 if (can_grant) {
347 spin_lock(&target->spinlock);
348 BUG_ON(target->ml.highest_blocked != LKM_IVMODE);
349
350 mlog(0, "calling ast for converting lock: %.*s, have: %d, "
351 "granting: %d, node: %u\n", res->lockname.len,
352 res->lockname.name, target->ml.type,
353 target->ml.convert_type, target->ml.node);
354
355 target->ml.type = target->ml.convert_type;
356 target->ml.convert_type = LKM_IVMODE;
Akinobu Mitaf1166292006-06-26 00:24:46 -0700357 list_move_tail(&target->list, &res->granted);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800358
359 BUG_ON(!target->lksb);
360 target->lksb->status = DLM_NORMAL;
361
362 spin_unlock(&target->spinlock);
363
364 __dlm_lockres_reserve_ast(res);
365 dlm_queue_ast(dlm, target);
366 /* go back and check for more */
367 goto converting;
368 }
369
370blocked:
371 if (list_empty(&res->blocked))
372 goto leave;
373 target = list_entry(res->blocked.next, struct dlm_lock, list);
374
375 head = &res->granted;
376 list_for_each(iter, head) {
377 lock = list_entry(iter, struct dlm_lock, list);
378 if (lock==target)
379 continue;
380 if (!dlm_lock_compatible(lock->ml.type, target->ml.type)) {
381 can_grant = 0;
382 if (lock->ml.highest_blocked == LKM_IVMODE) {
383 __dlm_lockres_reserve_ast(res);
384 dlm_queue_bast(dlm, lock);
385 }
386 if (lock->ml.highest_blocked < target->ml.type)
387 lock->ml.highest_blocked = target->ml.type;
388 }
389 }
390
391 head = &res->converting;
392 list_for_each(iter, head) {
393 lock = list_entry(iter, struct dlm_lock, list);
394 if (lock==target)
395 continue;
396 if (!dlm_lock_compatible(lock->ml.type, target->ml.type)) {
397 can_grant = 0;
398 if (lock->ml.highest_blocked == LKM_IVMODE) {
399 __dlm_lockres_reserve_ast(res);
400 dlm_queue_bast(dlm, lock);
401 }
402 if (lock->ml.highest_blocked < target->ml.type)
403 lock->ml.highest_blocked = target->ml.type;
404 }
405 }
406
407 /* we can grant the blocked lock (only
408 * possible if converting list empty) */
409 if (can_grant) {
410 spin_lock(&target->spinlock);
411 BUG_ON(target->ml.highest_blocked != LKM_IVMODE);
412
413 mlog(0, "calling ast for blocked lock: %.*s, granting: %d, "
414 "node: %u\n", res->lockname.len, res->lockname.name,
415 target->ml.type, target->ml.node);
416
417 // target->ml.type is already correct
Akinobu Mitaf1166292006-06-26 00:24:46 -0700418 list_move_tail(&target->list, &res->granted);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800419
420 BUG_ON(!target->lksb);
421 target->lksb->status = DLM_NORMAL;
422
423 spin_unlock(&target->spinlock);
424
425 __dlm_lockres_reserve_ast(res);
426 dlm_queue_ast(dlm, target);
427 /* go back and check for more */
428 goto converting;
429 }
430
431leave:
432 return;
433}
434
435/* must have NO locks when calling this with res !=NULL * */
436void dlm_kick_thread(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
437{
438 mlog_entry("dlm=%p, res=%p\n", dlm, res);
439 if (res) {
440 spin_lock(&dlm->spinlock);
441 spin_lock(&res->spinlock);
442 __dlm_dirty_lockres(dlm, res);
443 spin_unlock(&res->spinlock);
444 spin_unlock(&dlm->spinlock);
445 }
446 wake_up(&dlm->dlm_thread_wq);
447}
448
449void __dlm_dirty_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
450{
451 mlog_entry("dlm=%p, res=%p\n", dlm, res);
452
453 assert_spin_locked(&dlm->spinlock);
454 assert_spin_locked(&res->spinlock);
455
456 /* don't shuffle secondary queues */
457 if ((res->owner == dlm->node_num) &&
458 !(res->state & DLM_LOCK_RES_DIRTY)) {
Kurt Hackel6ff06a92006-05-01 11:51:45 -0700459 /* ref for dirty_list */
460 dlm_lockres_get(res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800461 list_add_tail(&res->dirty, &dlm->dirty_list);
462 res->state |= DLM_LOCK_RES_DIRTY;
463 }
464}
465
466
467/* Launch the NM thread for the mounted volume */
468int dlm_launch_thread(struct dlm_ctxt *dlm)
469{
470 mlog(0, "starting dlm thread...\n");
471
472 dlm->dlm_thread_task = kthread_run(dlm_thread, dlm, "dlm_thread");
473 if (IS_ERR(dlm->dlm_thread_task)) {
474 mlog_errno(PTR_ERR(dlm->dlm_thread_task));
475 dlm->dlm_thread_task = NULL;
476 return -EINVAL;
477 }
478
479 return 0;
480}
481
482void dlm_complete_thread(struct dlm_ctxt *dlm)
483{
484 if (dlm->dlm_thread_task) {
485 mlog(ML_KTHREAD, "waiting for dlm thread to exit\n");
486 kthread_stop(dlm->dlm_thread_task);
487 dlm->dlm_thread_task = NULL;
488 }
489}
490
491static int dlm_dirty_list_empty(struct dlm_ctxt *dlm)
492{
493 int empty;
494
495 spin_lock(&dlm->spinlock);
496 empty = list_empty(&dlm->dirty_list);
497 spin_unlock(&dlm->spinlock);
498
499 return empty;
500}
501
502static void dlm_flush_asts(struct dlm_ctxt *dlm)
503{
504 int ret;
505 struct dlm_lock *lock;
506 struct dlm_lock_resource *res;
507 u8 hi;
508
509 spin_lock(&dlm->ast_lock);
510 while (!list_empty(&dlm->pending_asts)) {
511 lock = list_entry(dlm->pending_asts.next,
512 struct dlm_lock, ast_list);
513 /* get an extra ref on lock */
514 dlm_lock_get(lock);
515 res = lock->lockres;
516 mlog(0, "delivering an ast for this lockres\n");
517
518 BUG_ON(!lock->ast_pending);
519
520 /* remove from list (including ref) */
521 list_del_init(&lock->ast_list);
522 dlm_lock_put(lock);
523 spin_unlock(&dlm->ast_lock);
524
525 if (lock->ml.node != dlm->node_num) {
526 ret = dlm_do_remote_ast(dlm, res, lock);
527 if (ret < 0)
528 mlog_errno(ret);
529 } else
530 dlm_do_local_ast(dlm, res, lock);
531
532 spin_lock(&dlm->ast_lock);
533
534 /* possible that another ast was queued while
535 * we were delivering the last one */
536 if (!list_empty(&lock->ast_list)) {
537 mlog(0, "aha another ast got queued while "
538 "we were finishing the last one. will "
539 "keep the ast_pending flag set.\n");
540 } else
541 lock->ast_pending = 0;
542
543 /* drop the extra ref.
544 * this may drop it completely. */
545 dlm_lock_put(lock);
546 dlm_lockres_release_ast(dlm, res);
547 }
548
549 while (!list_empty(&dlm->pending_basts)) {
550 lock = list_entry(dlm->pending_basts.next,
551 struct dlm_lock, bast_list);
552 /* get an extra ref on lock */
553 dlm_lock_get(lock);
554 res = lock->lockres;
555
556 BUG_ON(!lock->bast_pending);
557
558 /* get the highest blocked lock, and reset */
559 spin_lock(&lock->spinlock);
560 BUG_ON(lock->ml.highest_blocked <= LKM_IVMODE);
561 hi = lock->ml.highest_blocked;
562 lock->ml.highest_blocked = LKM_IVMODE;
563 spin_unlock(&lock->spinlock);
564
565 /* remove from list (including ref) */
566 list_del_init(&lock->bast_list);
567 dlm_lock_put(lock);
568 spin_unlock(&dlm->ast_lock);
569
570 mlog(0, "delivering a bast for this lockres "
571 "(blocked = %d\n", hi);
572
573 if (lock->ml.node != dlm->node_num) {
574 ret = dlm_send_proxy_bast(dlm, res, lock, hi);
575 if (ret < 0)
576 mlog_errno(ret);
577 } else
578 dlm_do_local_bast(dlm, res, lock, hi);
579
580 spin_lock(&dlm->ast_lock);
581
582 /* possible that another bast was queued while
583 * we were delivering the last one */
584 if (!list_empty(&lock->bast_list)) {
585 mlog(0, "aha another bast got queued while "
586 "we were finishing the last one. will "
587 "keep the bast_pending flag set.\n");
588 } else
589 lock->bast_pending = 0;
590
591 /* drop the extra ref.
592 * this may drop it completely. */
593 dlm_lock_put(lock);
594 dlm_lockres_release_ast(dlm, res);
595 }
596 wake_up(&dlm->ast_wq);
597 spin_unlock(&dlm->ast_lock);
598}
599
600
601#define DLM_THREAD_TIMEOUT_MS (4 * 1000)
602#define DLM_THREAD_MAX_DIRTY 100
603#define DLM_THREAD_MAX_ASTS 10
604
605static int dlm_thread(void *data)
606{
607 struct dlm_lock_resource *res;
608 struct dlm_ctxt *dlm = data;
609 unsigned long timeout = msecs_to_jiffies(DLM_THREAD_TIMEOUT_MS);
610
611 mlog(0, "dlm thread running for %s...\n", dlm->name);
612
613 while (!kthread_should_stop()) {
614 int n = DLM_THREAD_MAX_DIRTY;
615
616 /* dlm_shutting_down is very point-in-time, but that
617 * doesn't matter as we'll just loop back around if we
618 * get false on the leading edge of a state
619 * transition. */
620 dlm_run_purge_list(dlm, dlm_shutting_down(dlm));
621
622 /* We really don't want to hold dlm->spinlock while
623 * calling dlm_shuffle_lists on each lockres that
624 * needs to have its queues adjusted and AST/BASTs
625 * run. So let's pull each entry off the dirty_list
626 * and drop dlm->spinlock ASAP. Once off the list,
627 * res->spinlock needs to be taken again to protect
628 * the queues while calling dlm_shuffle_lists. */
629 spin_lock(&dlm->spinlock);
630 while (!list_empty(&dlm->dirty_list)) {
631 int delay = 0;
632 res = list_entry(dlm->dirty_list.next,
633 struct dlm_lock_resource, dirty);
634
635 /* peel a lockres off, remove it from the list,
636 * unset the dirty flag and drop the dlm lock */
637 BUG_ON(!res);
638 dlm_lockres_get(res);
639
640 spin_lock(&res->spinlock);
641 res->state &= ~DLM_LOCK_RES_DIRTY;
642 list_del_init(&res->dirty);
643 spin_unlock(&res->spinlock);
644 spin_unlock(&dlm->spinlock);
Kurt Hackel6ff06a92006-05-01 11:51:45 -0700645 /* Drop dirty_list ref */
646 dlm_lockres_put(res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800647
648 /* lockres can be re-dirtied/re-added to the
649 * dirty_list in this gap, but that is ok */
650
651 spin_lock(&res->spinlock);
652 if (res->owner != dlm->node_num) {
653 __dlm_print_one_lock_resource(res);
654 mlog(ML_ERROR, "inprog:%s, mig:%s, reco:%s, dirty:%s\n",
655 res->state & DLM_LOCK_RES_IN_PROGRESS ? "yes" : "no",
656 res->state & DLM_LOCK_RES_MIGRATING ? "yes" : "no",
657 res->state & DLM_LOCK_RES_RECOVERING ? "yes" : "no",
658 res->state & DLM_LOCK_RES_DIRTY ? "yes" : "no");
659 }
660 BUG_ON(res->owner != dlm->node_num);
661
662 /* it is now ok to move lockreses in these states
663 * to the dirty list, assuming that they will only be
664 * dirty for a short while. */
665 if (res->state & (DLM_LOCK_RES_IN_PROGRESS |
666 DLM_LOCK_RES_MIGRATING |
667 DLM_LOCK_RES_RECOVERING)) {
668 /* move it to the tail and keep going */
669 spin_unlock(&res->spinlock);
670 mlog(0, "delaying list shuffling for in-"
671 "progress lockres %.*s, state=%d\n",
672 res->lockname.len, res->lockname.name,
673 res->state);
674 delay = 1;
675 goto in_progress;
676 }
677
678 /* at this point the lockres is not migrating/
679 * recovering/in-progress. we have the lockres
680 * spinlock and do NOT have the dlm lock.
681 * safe to reserve/queue asts and run the lists. */
682
Kurt Hackel8d79d082006-04-27 17:58:23 -0700683 mlog(0, "calling dlm_shuffle_lists with dlm=%s, "
684 "res=%.*s\n", dlm->name,
685 res->lockname.len, res->lockname.name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800686
687 /* called while holding lockres lock */
688 dlm_shuffle_lists(dlm, res);
689 spin_unlock(&res->spinlock);
690
691 dlm_lockres_calc_usage(dlm, res);
692
693in_progress:
694
695 spin_lock(&dlm->spinlock);
696 /* if the lock was in-progress, stick
697 * it on the back of the list */
698 if (delay) {
Kurt Hackel6ff06a92006-05-01 11:51:45 -0700699 /* ref for dirty_list */
700 dlm_lockres_get(res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800701 spin_lock(&res->spinlock);
702 list_add_tail(&res->dirty, &dlm->dirty_list);
703 res->state |= DLM_LOCK_RES_DIRTY;
704 spin_unlock(&res->spinlock);
705 }
706 dlm_lockres_put(res);
707
708 /* unlikely, but we may need to give time to
709 * other tasks */
710 if (!--n) {
711 mlog(0, "throttling dlm_thread\n");
712 break;
713 }
714 }
715
716 spin_unlock(&dlm->spinlock);
717 dlm_flush_asts(dlm);
718
719 /* yield and continue right away if there is more work to do */
720 if (!n) {
Kurt Hackelf85cd472006-05-01 14:27:41 -0700721 cond_resched();
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800722 continue;
723 }
724
725 wait_event_interruptible_timeout(dlm->dlm_thread_wq,
726 !dlm_dirty_list_empty(dlm) ||
727 kthread_should_stop(),
728 timeout);
729 }
730
731 mlog(0, "quitting DLM thread\n");
732 return 0;
733}