blob: 0bdd28e1d4d99b45e27a39fe6a0747f34fd4c839 [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>
Kurt Hackel6714d8e2005-12-15 14:31:23 -080033#include <linux/init.h>
34#include <linux/sysctl.h>
35#include <linux/random.h>
36#include <linux/blkdev.h>
37#include <linux/socket.h>
38#include <linux/inet.h>
39#include <linux/timer.h>
40#include <linux/kthread.h>
Kurt Hackel8d79d082006-04-27 17:58:23 -070041#include <linux/delay.h>
Kurt Hackel6714d8e2005-12-15 14:31:23 -080042
43
44#include "cluster/heartbeat.h"
45#include "cluster/nodemanager.h"
46#include "cluster/tcp.h"
47
48#include "dlmapi.h"
49#include "dlmcommon.h"
50#include "dlmdomain.h"
51
52#define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_THREAD)
53#include "cluster/masklog.h"
54
Kurt Hackel6714d8e2005-12-15 14:31:23 -080055static int dlm_thread(void *data);
Kurt Hackel6714d8e2005-12-15 14:31:23 -080056static void dlm_flush_asts(struct dlm_ctxt *dlm);
57
58#define dlm_lock_is_remote(dlm, lock) ((lock)->ml.node != (dlm)->node_num)
59
60/* will exit holding res->spinlock, but may drop in function */
61/* waits until flags are cleared on res->state */
62void __dlm_wait_on_lockres_flags(struct dlm_lock_resource *res, int flags)
63{
64 DECLARE_WAITQUEUE(wait, current);
65
66 assert_spin_locked(&res->spinlock);
67
68 add_wait_queue(&res->wq, &wait);
69repeat:
70 set_current_state(TASK_UNINTERRUPTIBLE);
71 if (res->state & flags) {
72 spin_unlock(&res->spinlock);
73 schedule();
74 spin_lock(&res->spinlock);
75 goto repeat;
76 }
77 remove_wait_queue(&res->wq, &wait);
Milind Arun Choudhary5c2c9d32007-04-26 00:29:35 -070078 __set_current_state(TASK_RUNNING);
Kurt Hackel6714d8e2005-12-15 14:31:23 -080079}
80
Kurt Hackelba2bf212006-12-01 14:47:20 -080081int __dlm_lockres_has_locks(struct dlm_lock_resource *res)
Kurt Hackel6714d8e2005-12-15 14:31:23 -080082{
83 if (list_empty(&res->granted) &&
84 list_empty(&res->converting) &&
Kurt Hackelba2bf212006-12-01 14:47:20 -080085 list_empty(&res->blocked))
86 return 0;
87 return 1;
88}
89
90/* "unused": the lockres has no locks, is not on the dirty list,
91 * has no inflight locks (in the gap between mastery and acquiring
92 * the first lock), and has no bits in its refmap.
93 * truly ready to be freed. */
94int __dlm_lockres_unused(struct dlm_lock_resource *res)
95{
96 if (!__dlm_lockres_has_locks(res) &&
Kurt Hackelddc09c82007-01-05 15:00:17 -080097 (list_empty(&res->dirty) && !(res->state & DLM_LOCK_RES_DIRTY))) {
Kurt Hackelba2bf212006-12-01 14:47:20 -080098 /* try not to scan the bitmap unless the first two
99 * conditions are already true */
100 int bit = find_next_bit(res->refmap, O2NM_MAX_NODES, 0);
101 if (bit >= O2NM_MAX_NODES) {
102 /* since the bit for dlm->node_num is not
103 * set, inflight_locks better be zero */
104 BUG_ON(res->inflight_locks != 0);
105 return 1;
106 }
107 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800108 return 0;
109}
110
111
112/* Call whenever you may have added or deleted something from one of
113 * the lockres queue's. This will figure out whether it belongs on the
114 * unused list or not and does the appropriate thing. */
115void __dlm_lockres_calc_usage(struct dlm_ctxt *dlm,
116 struct dlm_lock_resource *res)
117{
118 mlog_entry("%.*s\n", res->lockname.len, res->lockname.name);
119
120 assert_spin_locked(&dlm->spinlock);
121 assert_spin_locked(&res->spinlock);
122
123 if (__dlm_lockres_unused(res)){
124 if (list_empty(&res->purge)) {
Kurt Hackelba2bf212006-12-01 14:47:20 -0800125 mlog(0, "putting lockres %.*s:%p onto purge list\n",
126 res->lockname.len, res->lockname.name, res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800127
128 res->last_used = jiffies;
Kurt Hackelba2bf212006-12-01 14:47:20 -0800129 dlm_lockres_get(res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800130 list_add_tail(&res->purge, &dlm->purge_list);
131 dlm->purge_count++;
132 }
133 } else if (!list_empty(&res->purge)) {
Kurt Hackelba2bf212006-12-01 14:47:20 -0800134 mlog(0, "removing lockres %.*s:%p from purge list, owner=%u\n",
135 res->lockname.len, res->lockname.name, res, res->owner);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800136
137 list_del_init(&res->purge);
Kurt Hackelba2bf212006-12-01 14:47:20 -0800138 dlm_lockres_put(res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800139 dlm->purge_count--;
140 }
141}
142
143void dlm_lockres_calc_usage(struct dlm_ctxt *dlm,
144 struct dlm_lock_resource *res)
145{
146 mlog_entry("%.*s\n", res->lockname.len, res->lockname.name);
147 spin_lock(&dlm->spinlock);
148 spin_lock(&res->spinlock);
149
150 __dlm_lockres_calc_usage(dlm, res);
151
152 spin_unlock(&res->spinlock);
153 spin_unlock(&dlm->spinlock);
154}
155
Adrian Bunkfaf0ec92006-12-14 00:17:32 +0100156static int dlm_purge_lockres(struct dlm_ctxt *dlm,
157 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)) {
Kurt Hackelba2bf212006-12-01 14:47:20 -0800164 mlog(0, "%s:%.*s: tried to purge but not unused\n",
165 dlm->name, res->lockname.len, res->lockname.name);
Sunil Mushran516b7e52009-02-26 15:00:48 -0800166 __dlm_print_one_lock_resource(res);
167 spin_unlock(&res->spinlock);
168 BUG();
Kurt Hackelba2bf212006-12-01 14:47:20 -0800169 }
Sunil Mushran516b7e52009-02-26 15:00:48 -0800170
171 if (res->state & DLM_LOCK_RES_MIGRATING) {
172 mlog(0, "%s:%.*s: Delay dropref as this lockres is "
173 "being remastered\n", dlm->name, res->lockname.len,
174 res->lockname.name);
175 /* Re-add the lockres to the end of the purge list */
176 if (!list_empty(&res->purge)) {
177 list_del_init(&res->purge);
178 list_add_tail(&res->purge, &dlm->purge_list);
179 }
180 spin_unlock(&res->spinlock);
181 return 0;
182 }
183
Kurt Hackelba2bf212006-12-01 14:47:20 -0800184 master = (res->owner == dlm->node_num);
Sunil Mushran516b7e52009-02-26 15:00:48 -0800185
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800186 if (!master)
Kurt Hackelba2bf212006-12-01 14:47:20 -0800187 res->state |= DLM_LOCK_RES_DROPPING_REF;
188 spin_unlock(&res->spinlock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800189
Kurt Hackelba2bf212006-12-01 14:47:20 -0800190 mlog(0, "purging lockres %.*s, master = %d\n", res->lockname.len,
191 res->lockname.name, master);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800192
Kurt Hackelba2bf212006-12-01 14:47:20 -0800193 if (!master) {
Sunil Mushranc824c3c2008-03-01 14:04:25 -0800194 /* drop spinlock... retake below */
195 spin_unlock(&dlm->spinlock);
196
Kurt Hackel3b8118c2007-01-17 17:05:53 -0800197 spin_lock(&res->spinlock);
198 /* This ensures that clear refmap is sent after the set */
Sunil Mushran7dc102b2009-02-03 12:37:13 -0800199 __dlm_wait_on_lockres_flags(res, DLM_LOCK_RES_SETREF_INPROG);
Kurt Hackel3b8118c2007-01-17 17:05:53 -0800200 spin_unlock(&res->spinlock);
Sunil Mushranc824c3c2008-03-01 14:04:25 -0800201
Kurt Hackelba2bf212006-12-01 14:47:20 -0800202 /* clear our bit from the master's refmap, ignore errors */
203 ret = dlm_drop_lockres_ref(dlm, res);
204 if (ret < 0) {
205 mlog_errno(ret);
206 if (!dlm_is_host_down(ret))
207 BUG();
208 }
209 mlog(0, "%s:%.*s: dlm_deref_lockres returned %d\n",
210 dlm->name, res->lockname.len, res->lockname.name, ret);
211 spin_lock(&dlm->spinlock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800212 }
213
Wengang Wang83e32d92009-09-03 15:56:33 +0800214 spin_lock(&res->spinlock);
Kurt Hackelba2bf212006-12-01 14:47:20 -0800215 if (!list_empty(&res->purge)) {
216 mlog(0, "removing lockres %.*s:%p from purgelist, "
217 "master = %d\n", res->lockname.len, res->lockname.name,
218 res, master);
219 list_del_init(&res->purge);
Wengang Wang83e32d92009-09-03 15:56:33 +0800220 spin_unlock(&res->spinlock);
Kurt Hackelba2bf212006-12-01 14:47:20 -0800221 dlm_lockres_put(res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800222 dlm->purge_count--;
Wengang Wang83e32d92009-09-03 15:56:33 +0800223 } else
224 spin_unlock(&res->spinlock);
225
Kurt Hackelba2bf212006-12-01 14:47:20 -0800226 __dlm_unhash_lockres(res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800227
Kurt Hackelba2bf212006-12-01 14:47:20 -0800228 /* lockres is not in the hash now. drop the flag and wake up
229 * any processes waiting in dlm_get_lock_resource. */
230 if (!master) {
231 spin_lock(&res->spinlock);
232 res->state &= ~DLM_LOCK_RES_DROPPING_REF;
233 spin_unlock(&res->spinlock);
234 wake_up(&res->wq);
Kurt Hackel8b219802006-05-01 11:16:45 -0700235 }
Kurt Hackelba2bf212006-12-01 14:47:20 -0800236 return 0;
Kurt Hackel8b219802006-05-01 11:16:45 -0700237}
238
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800239static void dlm_run_purge_list(struct dlm_ctxt *dlm,
240 int purge_now)
241{
242 unsigned int run_max, unused;
243 unsigned long purge_jiffies;
244 struct dlm_lock_resource *lockres;
245
246 spin_lock(&dlm->spinlock);
247 run_max = dlm->purge_count;
248
249 while(run_max && !list_empty(&dlm->purge_list)) {
250 run_max--;
251
252 lockres = list_entry(dlm->purge_list.next,
253 struct dlm_lock_resource, purge);
254
255 /* Status of the lockres *might* change so double
256 * check. If the lockres is unused, holding the dlm
257 * spinlock will prevent people from getting and more
258 * refs on it -- there's no need to keep the lockres
259 * spinlock. */
260 spin_lock(&lockres->spinlock);
261 unused = __dlm_lockres_unused(lockres);
262 spin_unlock(&lockres->spinlock);
263
264 if (!unused)
265 continue;
266
267 purge_jiffies = lockres->last_used +
268 msecs_to_jiffies(DLM_PURGE_INTERVAL_MS);
269
270 /* Make sure that we want to be processing this guy at
271 * this time. */
272 if (!purge_now && time_after(purge_jiffies, jiffies)) {
273 /* Since resources are added to the purge list
274 * in tail order, we can stop at the first
275 * unpurgable resource -- anyone added after
276 * him will have a greater last_used value */
277 break;
278 }
279
Sunil Mushran78062cb2007-03-22 17:01:07 -0700280 dlm_lockres_get(lockres);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800281
282 /* This may drop and reacquire the dlm spinlock if it
283 * has to do migration. */
Kurt Hackelba2bf212006-12-01 14:47:20 -0800284 if (dlm_purge_lockres(dlm, lockres))
285 BUG();
Sunil Mushran78062cb2007-03-22 17:01:07 -0700286
Sunil Mushran3fca0892007-03-12 13:24:34 -0700287 dlm_lockres_put(lockres);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800288
289 /* Avoid adding any scheduling latencies */
290 cond_resched_lock(&dlm->spinlock);
291 }
292
293 spin_unlock(&dlm->spinlock);
294}
295
296static void dlm_shuffle_lists(struct dlm_ctxt *dlm,
297 struct dlm_lock_resource *res)
298{
299 struct dlm_lock *lock, *target;
300 struct list_head *iter;
301 struct list_head *head;
302 int can_grant = 1;
303
304 //mlog(0, "res->lockname.len=%d\n", res->lockname.len);
305 //mlog(0, "res->lockname.name=%p\n", res->lockname.name);
306 //mlog(0, "shuffle res %.*s\n", res->lockname.len,
307 // res->lockname.name);
308
309 /* because this function is called with the lockres
310 * spinlock, and because we know that it is not migrating/
311 * recovering/in-progress, it is fine to reserve asts and
312 * basts right before queueing them all throughout */
Wengang Wangd9ef7522010-05-17 20:20:44 +0800313 assert_spin_locked(&dlm->ast_lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800314 assert_spin_locked(&res->spinlock);
315 BUG_ON((res->state & (DLM_LOCK_RES_MIGRATING|
316 DLM_LOCK_RES_RECOVERING|
317 DLM_LOCK_RES_IN_PROGRESS)));
318
319converting:
320 if (list_empty(&res->converting))
321 goto blocked;
322 mlog(0, "res %.*s has locks on a convert queue\n", res->lockname.len,
323 res->lockname.name);
324
325 target = list_entry(res->converting.next, struct dlm_lock, list);
326 if (target->ml.convert_type == LKM_IVMODE) {
327 mlog(ML_ERROR, "%.*s: converting a lock with no "
328 "convert_type!\n", res->lockname.len, res->lockname.name);
329 BUG();
330 }
331 head = &res->granted;
332 list_for_each(iter, head) {
333 lock = list_entry(iter, struct dlm_lock, list);
334 if (lock==target)
335 continue;
336 if (!dlm_lock_compatible(lock->ml.type,
337 target->ml.convert_type)) {
338 can_grant = 0;
339 /* queue the BAST if not already */
340 if (lock->ml.highest_blocked == LKM_IVMODE) {
341 __dlm_lockres_reserve_ast(res);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800342 __dlm_queue_bast(dlm, lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800343 }
344 /* update the highest_blocked if needed */
345 if (lock->ml.highest_blocked < target->ml.convert_type)
346 lock->ml.highest_blocked =
347 target->ml.convert_type;
348 }
349 }
350 head = &res->converting;
351 list_for_each(iter, head) {
352 lock = list_entry(iter, struct dlm_lock, list);
353 if (lock==target)
354 continue;
355 if (!dlm_lock_compatible(lock->ml.type,
356 target->ml.convert_type)) {
357 can_grant = 0;
358 if (lock->ml.highest_blocked == LKM_IVMODE) {
359 __dlm_lockres_reserve_ast(res);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800360 __dlm_queue_bast(dlm, lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800361 }
362 if (lock->ml.highest_blocked < target->ml.convert_type)
363 lock->ml.highest_blocked =
364 target->ml.convert_type;
365 }
366 }
367
368 /* we can convert the lock */
369 if (can_grant) {
370 spin_lock(&target->spinlock);
371 BUG_ON(target->ml.highest_blocked != LKM_IVMODE);
372
373 mlog(0, "calling ast for converting lock: %.*s, have: %d, "
374 "granting: %d, node: %u\n", res->lockname.len,
375 res->lockname.name, target->ml.type,
376 target->ml.convert_type, target->ml.node);
377
378 target->ml.type = target->ml.convert_type;
379 target->ml.convert_type = LKM_IVMODE;
Akinobu Mitaf1166292006-06-26 00:24:46 -0700380 list_move_tail(&target->list, &res->granted);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800381
382 BUG_ON(!target->lksb);
383 target->lksb->status = DLM_NORMAL;
384
385 spin_unlock(&target->spinlock);
386
387 __dlm_lockres_reserve_ast(res);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800388 __dlm_queue_ast(dlm, target);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800389 /* go back and check for more */
390 goto converting;
391 }
392
393blocked:
394 if (list_empty(&res->blocked))
395 goto leave;
396 target = list_entry(res->blocked.next, struct dlm_lock, list);
397
398 head = &res->granted;
399 list_for_each(iter, head) {
400 lock = list_entry(iter, struct dlm_lock, list);
401 if (lock==target)
402 continue;
403 if (!dlm_lock_compatible(lock->ml.type, target->ml.type)) {
404 can_grant = 0;
405 if (lock->ml.highest_blocked == LKM_IVMODE) {
406 __dlm_lockres_reserve_ast(res);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800407 __dlm_queue_bast(dlm, lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800408 }
409 if (lock->ml.highest_blocked < target->ml.type)
410 lock->ml.highest_blocked = target->ml.type;
411 }
412 }
413
414 head = &res->converting;
415 list_for_each(iter, head) {
416 lock = list_entry(iter, struct dlm_lock, list);
417 if (lock==target)
418 continue;
419 if (!dlm_lock_compatible(lock->ml.type, target->ml.type)) {
420 can_grant = 0;
421 if (lock->ml.highest_blocked == LKM_IVMODE) {
422 __dlm_lockres_reserve_ast(res);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800423 __dlm_queue_bast(dlm, lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800424 }
425 if (lock->ml.highest_blocked < target->ml.type)
426 lock->ml.highest_blocked = target->ml.type;
427 }
428 }
429
430 /* we can grant the blocked lock (only
431 * possible if converting list empty) */
432 if (can_grant) {
433 spin_lock(&target->spinlock);
434 BUG_ON(target->ml.highest_blocked != LKM_IVMODE);
435
436 mlog(0, "calling ast for blocked lock: %.*s, granting: %d, "
437 "node: %u\n", res->lockname.len, res->lockname.name,
438 target->ml.type, target->ml.node);
439
440 // target->ml.type is already correct
Akinobu Mitaf1166292006-06-26 00:24:46 -0700441 list_move_tail(&target->list, &res->granted);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800442
443 BUG_ON(!target->lksb);
444 target->lksb->status = DLM_NORMAL;
445
446 spin_unlock(&target->spinlock);
447
448 __dlm_lockres_reserve_ast(res);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800449 __dlm_queue_ast(dlm, target);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800450 /* go back and check for more */
451 goto converting;
452 }
453
454leave:
455 return;
456}
457
458/* must have NO locks when calling this with res !=NULL * */
459void dlm_kick_thread(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
460{
461 mlog_entry("dlm=%p, res=%p\n", dlm, res);
462 if (res) {
463 spin_lock(&dlm->spinlock);
464 spin_lock(&res->spinlock);
465 __dlm_dirty_lockres(dlm, res);
466 spin_unlock(&res->spinlock);
467 spin_unlock(&dlm->spinlock);
468 }
469 wake_up(&dlm->dlm_thread_wq);
470}
471
472void __dlm_dirty_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
473{
474 mlog_entry("dlm=%p, res=%p\n", dlm, res);
475
476 assert_spin_locked(&dlm->spinlock);
477 assert_spin_locked(&res->spinlock);
478
479 /* don't shuffle secondary queues */
Kurt Hackelddc09c82007-01-05 15:00:17 -0800480 if ((res->owner == dlm->node_num)) {
481 if (res->state & (DLM_LOCK_RES_MIGRATING |
482 DLM_LOCK_RES_BLOCK_DIRTY))
483 return;
484
485 if (list_empty(&res->dirty)) {
486 /* ref for dirty_list */
487 dlm_lockres_get(res);
488 list_add_tail(&res->dirty, &dlm->dirty_list);
489 res->state |= DLM_LOCK_RES_DIRTY;
490 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800491 }
492}
493
494
495/* Launch the NM thread for the mounted volume */
496int dlm_launch_thread(struct dlm_ctxt *dlm)
497{
498 mlog(0, "starting dlm thread...\n");
499
500 dlm->dlm_thread_task = kthread_run(dlm_thread, dlm, "dlm_thread");
501 if (IS_ERR(dlm->dlm_thread_task)) {
502 mlog_errno(PTR_ERR(dlm->dlm_thread_task));
503 dlm->dlm_thread_task = NULL;
504 return -EINVAL;
505 }
506
507 return 0;
508}
509
510void dlm_complete_thread(struct dlm_ctxt *dlm)
511{
512 if (dlm->dlm_thread_task) {
513 mlog(ML_KTHREAD, "waiting for dlm thread to exit\n");
514 kthread_stop(dlm->dlm_thread_task);
515 dlm->dlm_thread_task = NULL;
516 }
517}
518
519static int dlm_dirty_list_empty(struct dlm_ctxt *dlm)
520{
521 int empty;
522
523 spin_lock(&dlm->spinlock);
524 empty = list_empty(&dlm->dirty_list);
525 spin_unlock(&dlm->spinlock);
526
527 return empty;
528}
529
530static void dlm_flush_asts(struct dlm_ctxt *dlm)
531{
532 int ret;
533 struct dlm_lock *lock;
534 struct dlm_lock_resource *res;
535 u8 hi;
536
537 spin_lock(&dlm->ast_lock);
538 while (!list_empty(&dlm->pending_asts)) {
539 lock = list_entry(dlm->pending_asts.next,
540 struct dlm_lock, ast_list);
541 /* get an extra ref on lock */
542 dlm_lock_get(lock);
543 res = lock->lockres;
544 mlog(0, "delivering an ast for this lockres\n");
545
546 BUG_ON(!lock->ast_pending);
547
548 /* remove from list (including ref) */
549 list_del_init(&lock->ast_list);
550 dlm_lock_put(lock);
551 spin_unlock(&dlm->ast_lock);
552
553 if (lock->ml.node != dlm->node_num) {
554 ret = dlm_do_remote_ast(dlm, res, lock);
555 if (ret < 0)
556 mlog_errno(ret);
557 } else
558 dlm_do_local_ast(dlm, res, lock);
559
560 spin_lock(&dlm->ast_lock);
561
562 /* possible that another ast was queued while
563 * we were delivering the last one */
564 if (!list_empty(&lock->ast_list)) {
565 mlog(0, "aha another ast got queued while "
566 "we were finishing the last one. will "
567 "keep the ast_pending flag set.\n");
568 } else
569 lock->ast_pending = 0;
570
571 /* drop the extra ref.
572 * this may drop it completely. */
573 dlm_lock_put(lock);
574 dlm_lockres_release_ast(dlm, res);
575 }
576
577 while (!list_empty(&dlm->pending_basts)) {
578 lock = list_entry(dlm->pending_basts.next,
579 struct dlm_lock, bast_list);
580 /* get an extra ref on lock */
581 dlm_lock_get(lock);
582 res = lock->lockres;
583
584 BUG_ON(!lock->bast_pending);
585
586 /* get the highest blocked lock, and reset */
587 spin_lock(&lock->spinlock);
588 BUG_ON(lock->ml.highest_blocked <= LKM_IVMODE);
589 hi = lock->ml.highest_blocked;
590 lock->ml.highest_blocked = LKM_IVMODE;
591 spin_unlock(&lock->spinlock);
592
593 /* remove from list (including ref) */
594 list_del_init(&lock->bast_list);
595 dlm_lock_put(lock);
596 spin_unlock(&dlm->ast_lock);
597
598 mlog(0, "delivering a bast for this lockres "
599 "(blocked = %d\n", hi);
600
601 if (lock->ml.node != dlm->node_num) {
602 ret = dlm_send_proxy_bast(dlm, res, lock, hi);
603 if (ret < 0)
604 mlog_errno(ret);
605 } else
606 dlm_do_local_bast(dlm, res, lock, hi);
607
608 spin_lock(&dlm->ast_lock);
609
610 /* possible that another bast was queued while
611 * we were delivering the last one */
612 if (!list_empty(&lock->bast_list)) {
613 mlog(0, "aha another bast got queued while "
614 "we were finishing the last one. will "
615 "keep the bast_pending flag set.\n");
616 } else
617 lock->bast_pending = 0;
618
619 /* drop the extra ref.
620 * this may drop it completely. */
621 dlm_lock_put(lock);
622 dlm_lockres_release_ast(dlm, res);
623 }
624 wake_up(&dlm->ast_wq);
625 spin_unlock(&dlm->ast_lock);
626}
627
628
629#define DLM_THREAD_TIMEOUT_MS (4 * 1000)
630#define DLM_THREAD_MAX_DIRTY 100
631#define DLM_THREAD_MAX_ASTS 10
632
633static int dlm_thread(void *data)
634{
635 struct dlm_lock_resource *res;
636 struct dlm_ctxt *dlm = data;
637 unsigned long timeout = msecs_to_jiffies(DLM_THREAD_TIMEOUT_MS);
638
639 mlog(0, "dlm thread running for %s...\n", dlm->name);
640
641 while (!kthread_should_stop()) {
642 int n = DLM_THREAD_MAX_DIRTY;
643
644 /* dlm_shutting_down is very point-in-time, but that
645 * doesn't matter as we'll just loop back around if we
646 * get false on the leading edge of a state
647 * transition. */
648 dlm_run_purge_list(dlm, dlm_shutting_down(dlm));
649
650 /* We really don't want to hold dlm->spinlock while
651 * calling dlm_shuffle_lists on each lockres that
652 * needs to have its queues adjusted and AST/BASTs
653 * run. So let's pull each entry off the dirty_list
654 * and drop dlm->spinlock ASAP. Once off the list,
655 * res->spinlock needs to be taken again to protect
656 * the queues while calling dlm_shuffle_lists. */
657 spin_lock(&dlm->spinlock);
658 while (!list_empty(&dlm->dirty_list)) {
659 int delay = 0;
660 res = list_entry(dlm->dirty_list.next,
661 struct dlm_lock_resource, dirty);
662
663 /* peel a lockres off, remove it from the list,
664 * unset the dirty flag and drop the dlm lock */
665 BUG_ON(!res);
666 dlm_lockres_get(res);
667
668 spin_lock(&res->spinlock);
Kurt Hackelddc09c82007-01-05 15:00:17 -0800669 /* We clear the DLM_LOCK_RES_DIRTY state once we shuffle lists below */
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800670 list_del_init(&res->dirty);
671 spin_unlock(&res->spinlock);
672 spin_unlock(&dlm->spinlock);
Kurt Hackel6ff06a92006-05-01 11:51:45 -0700673 /* Drop dirty_list ref */
674 dlm_lockres_put(res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800675
676 /* lockres can be re-dirtied/re-added to the
677 * dirty_list in this gap, but that is ok */
678
Wengang Wangd9ef7522010-05-17 20:20:44 +0800679 spin_lock(&dlm->ast_lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800680 spin_lock(&res->spinlock);
681 if (res->owner != dlm->node_num) {
682 __dlm_print_one_lock_resource(res);
683 mlog(ML_ERROR, "inprog:%s, mig:%s, reco:%s, dirty:%s\n",
684 res->state & DLM_LOCK_RES_IN_PROGRESS ? "yes" : "no",
685 res->state & DLM_LOCK_RES_MIGRATING ? "yes" : "no",
686 res->state & DLM_LOCK_RES_RECOVERING ? "yes" : "no",
687 res->state & DLM_LOCK_RES_DIRTY ? "yes" : "no");
688 }
689 BUG_ON(res->owner != dlm->node_num);
690
691 /* it is now ok to move lockreses in these states
692 * to the dirty list, assuming that they will only be
693 * dirty for a short while. */
Kurt Hackelddc09c82007-01-05 15:00:17 -0800694 BUG_ON(res->state & DLM_LOCK_RES_MIGRATING);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800695 if (res->state & (DLM_LOCK_RES_IN_PROGRESS |
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800696 DLM_LOCK_RES_RECOVERING)) {
697 /* move it to the tail and keep going */
Kurt Hackelddc09c82007-01-05 15:00:17 -0800698 res->state &= ~DLM_LOCK_RES_DIRTY;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800699 spin_unlock(&res->spinlock);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800700 spin_unlock(&dlm->ast_lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800701 mlog(0, "delaying list shuffling for in-"
702 "progress lockres %.*s, state=%d\n",
703 res->lockname.len, res->lockname.name,
704 res->state);
705 delay = 1;
706 goto in_progress;
707 }
708
709 /* at this point the lockres is not migrating/
710 * recovering/in-progress. we have the lockres
711 * spinlock and do NOT have the dlm lock.
712 * safe to reserve/queue asts and run the lists. */
713
Kurt Hackel8d79d082006-04-27 17:58:23 -0700714 mlog(0, "calling dlm_shuffle_lists with dlm=%s, "
715 "res=%.*s\n", dlm->name,
716 res->lockname.len, res->lockname.name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800717
718 /* called while holding lockres lock */
719 dlm_shuffle_lists(dlm, res);
Kurt Hackelddc09c82007-01-05 15:00:17 -0800720 res->state &= ~DLM_LOCK_RES_DIRTY;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800721 spin_unlock(&res->spinlock);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800722 spin_unlock(&dlm->ast_lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800723
724 dlm_lockres_calc_usage(dlm, res);
725
726in_progress:
727
728 spin_lock(&dlm->spinlock);
729 /* if the lock was in-progress, stick
730 * it on the back of the list */
731 if (delay) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800732 spin_lock(&res->spinlock);
Kurt Hackelddc09c82007-01-05 15:00:17 -0800733 __dlm_dirty_lockres(dlm, res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800734 spin_unlock(&res->spinlock);
735 }
736 dlm_lockres_put(res);
737
738 /* unlikely, but we may need to give time to
739 * other tasks */
740 if (!--n) {
741 mlog(0, "throttling dlm_thread\n");
742 break;
743 }
744 }
745
746 spin_unlock(&dlm->spinlock);
747 dlm_flush_asts(dlm);
748
749 /* yield and continue right away if there is more work to do */
750 if (!n) {
Kurt Hackelf85cd472006-05-01 14:27:41 -0700751 cond_resched();
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800752 continue;
753 }
754
755 wait_event_interruptible_timeout(dlm->dlm_thread_wq,
756 !dlm_dirty_list_empty(dlm) ||
757 kthread_should_stop(),
758 timeout);
759 }
760
761 mlog(0, "quitting DLM thread\n");
762 return 0;
763}