blob: dd78ca3f360fe1c984ab64b01d55f06b2c5629dc [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>
Kurt Hackel6714d8e2005-12-15 14:31:23 -080031#include <linux/highmem.h>
Kurt Hackel6714d8e2005-12-15 14:31:23 -080032#include <linux/init.h>
33#include <linux/sysctl.h>
34#include <linux/random.h>
35#include <linux/blkdev.h>
36#include <linux/socket.h>
37#include <linux/inet.h>
38#include <linux/timer.h>
39#include <linux/kthread.h>
Kurt Hackel8d79d082006-04-27 17:58:23 -070040#include <linux/delay.h>
Kurt Hackel6714d8e2005-12-15 14:31:23 -080041
42
43#include "cluster/heartbeat.h"
44#include "cluster/nodemanager.h"
45#include "cluster/tcp.h"
46
47#include "dlmapi.h"
48#include "dlmcommon.h"
49#include "dlmdomain.h"
50
51#define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_THREAD)
52#include "cluster/masklog.h"
53
Kurt Hackel6714d8e2005-12-15 14:31:23 -080054static int dlm_thread(void *data);
Kurt Hackel6714d8e2005-12-15 14:31:23 -080055static void dlm_flush_asts(struct dlm_ctxt *dlm);
56
57#define dlm_lock_is_remote(dlm, lock) ((lock)->ml.node != (dlm)->node_num)
58
59/* will exit holding res->spinlock, but may drop in function */
60/* waits until flags are cleared on res->state */
61void __dlm_wait_on_lockres_flags(struct dlm_lock_resource *res, int flags)
62{
63 DECLARE_WAITQUEUE(wait, current);
64
65 assert_spin_locked(&res->spinlock);
66
67 add_wait_queue(&res->wq, &wait);
68repeat:
69 set_current_state(TASK_UNINTERRUPTIBLE);
70 if (res->state & flags) {
71 spin_unlock(&res->spinlock);
72 schedule();
73 spin_lock(&res->spinlock);
74 goto repeat;
75 }
76 remove_wait_queue(&res->wq, &wait);
Milind Arun Choudhary5c2c9d32007-04-26 00:29:35 -070077 __set_current_state(TASK_RUNNING);
Kurt Hackel6714d8e2005-12-15 14:31:23 -080078}
79
Kurt Hackelba2bf212006-12-01 14:47:20 -080080int __dlm_lockres_has_locks(struct dlm_lock_resource *res)
Kurt Hackel6714d8e2005-12-15 14:31:23 -080081{
82 if (list_empty(&res->granted) &&
83 list_empty(&res->converting) &&
Kurt Hackelba2bf212006-12-01 14:47:20 -080084 list_empty(&res->blocked))
85 return 0;
86 return 1;
87}
88
89/* "unused": the lockres has no locks, is not on the dirty list,
90 * has no inflight locks (in the gap between mastery and acquiring
91 * the first lock), and has no bits in its refmap.
92 * truly ready to be freed. */
93int __dlm_lockres_unused(struct dlm_lock_resource *res)
94{
95 if (!__dlm_lockres_has_locks(res) &&
Kurt Hackelddc09c82007-01-05 15:00:17 -080096 (list_empty(&res->dirty) && !(res->state & DLM_LOCK_RES_DIRTY))) {
Kurt Hackelba2bf212006-12-01 14:47:20 -080097 /* try not to scan the bitmap unless the first two
98 * conditions are already true */
99 int bit = find_next_bit(res->refmap, O2NM_MAX_NODES, 0);
100 if (bit >= O2NM_MAX_NODES) {
101 /* since the bit for dlm->node_num is not
102 * set, inflight_locks better be zero */
103 BUG_ON(res->inflight_locks != 0);
104 return 1;
105 }
106 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800107 return 0;
108}
109
110
111/* Call whenever you may have added or deleted something from one of
112 * the lockres queue's. This will figure out whether it belongs on the
113 * unused list or not and does the appropriate thing. */
114void __dlm_lockres_calc_usage(struct dlm_ctxt *dlm,
115 struct dlm_lock_resource *res)
116{
117 mlog_entry("%.*s\n", res->lockname.len, res->lockname.name);
118
119 assert_spin_locked(&dlm->spinlock);
120 assert_spin_locked(&res->spinlock);
121
122 if (__dlm_lockres_unused(res)){
123 if (list_empty(&res->purge)) {
Kurt Hackelba2bf212006-12-01 14:47:20 -0800124 mlog(0, "putting lockres %.*s:%p onto purge list\n",
125 res->lockname.len, res->lockname.name, res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800126
127 res->last_used = jiffies;
Kurt Hackelba2bf212006-12-01 14:47:20 -0800128 dlm_lockres_get(res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800129 list_add_tail(&res->purge, &dlm->purge_list);
130 dlm->purge_count++;
131 }
132 } else if (!list_empty(&res->purge)) {
Kurt Hackelba2bf212006-12-01 14:47:20 -0800133 mlog(0, "removing lockres %.*s:%p from purge list, owner=%u\n",
134 res->lockname.len, res->lockname.name, res, res->owner);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800135
136 list_del_init(&res->purge);
Kurt Hackelba2bf212006-12-01 14:47:20 -0800137 dlm_lockres_put(res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800138 dlm->purge_count--;
139 }
140}
141
142void dlm_lockres_calc_usage(struct dlm_ctxt *dlm,
143 struct dlm_lock_resource *res)
144{
145 mlog_entry("%.*s\n", res->lockname.len, res->lockname.name);
146 spin_lock(&dlm->spinlock);
147 spin_lock(&res->spinlock);
148
149 __dlm_lockres_calc_usage(dlm, res);
150
151 spin_unlock(&res->spinlock);
152 spin_unlock(&dlm->spinlock);
153}
154
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700155static void dlm_purge_lockres(struct dlm_ctxt *dlm,
Adrian Bunkfaf0ec92006-12-14 00:17:32 +0100156 struct dlm_lock_resource *res)
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800157{
158 int master;
Kurt Hackelba2bf212006-12-01 14:47:20 -0800159 int ret = 0;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800160
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700161 assert_spin_locked(&dlm->spinlock);
162 assert_spin_locked(&res->spinlock);
Sunil Mushran516b7e52009-02-26 15:00:48 -0800163
Kurt Hackelba2bf212006-12-01 14:47:20 -0800164 master = (res->owner == dlm->node_num);
Sunil Mushran516b7e52009-02-26 15:00:48 -0800165
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800166
Kurt Hackelba2bf212006-12-01 14:47:20 -0800167 mlog(0, "purging lockres %.*s, master = %d\n", res->lockname.len,
168 res->lockname.name, master);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800169
Kurt Hackelba2bf212006-12-01 14:47:20 -0800170 if (!master) {
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700171 res->state |= DLM_LOCK_RES_DROPPING_REF;
Sunil Mushranc824c3c2008-03-01 14:04:25 -0800172 /* drop spinlock... retake below */
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700173 spin_unlock(&res->spinlock);
Sunil Mushranc824c3c2008-03-01 14:04:25 -0800174 spin_unlock(&dlm->spinlock);
175
Kurt Hackel3b8118c2007-01-17 17:05:53 -0800176 spin_lock(&res->spinlock);
177 /* This ensures that clear refmap is sent after the set */
Sunil Mushran7dc102b2009-02-03 12:37:13 -0800178 __dlm_wait_on_lockres_flags(res, DLM_LOCK_RES_SETREF_INPROG);
Kurt Hackel3b8118c2007-01-17 17:05:53 -0800179 spin_unlock(&res->spinlock);
Sunil Mushranc824c3c2008-03-01 14:04:25 -0800180
Kurt Hackelba2bf212006-12-01 14:47:20 -0800181 /* clear our bit from the master's refmap, ignore errors */
182 ret = dlm_drop_lockres_ref(dlm, res);
183 if (ret < 0) {
184 mlog_errno(ret);
185 if (!dlm_is_host_down(ret))
186 BUG();
187 }
188 mlog(0, "%s:%.*s: dlm_deref_lockres returned %d\n",
189 dlm->name, res->lockname.len, res->lockname.name, ret);
190 spin_lock(&dlm->spinlock);
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700191 spin_lock(&res->spinlock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800192 }
193
Kurt Hackelba2bf212006-12-01 14:47:20 -0800194 if (!list_empty(&res->purge)) {
195 mlog(0, "removing lockres %.*s:%p from purgelist, "
196 "master = %d\n", res->lockname.len, res->lockname.name,
197 res, master);
198 list_del_init(&res->purge);
199 dlm_lockres_put(res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800200 dlm->purge_count--;
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700201 }
202
203 if (!__dlm_lockres_unused(res)) {
204 mlog(ML_ERROR, "found lockres %s:%.*s: in use after deref\n",
205 dlm->name, res->lockname.len, res->lockname.name);
206 __dlm_print_one_lock_resource(res);
207 BUG();
208 }
Wengang Wang83e32d92009-09-03 15:56:33 +0800209
Kurt Hackelba2bf212006-12-01 14:47:20 -0800210 __dlm_unhash_lockres(res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800211
Kurt Hackelba2bf212006-12-01 14:47:20 -0800212 /* lockres is not in the hash now. drop the flag and wake up
213 * any processes waiting in dlm_get_lock_resource. */
214 if (!master) {
Kurt Hackelba2bf212006-12-01 14:47:20 -0800215 res->state &= ~DLM_LOCK_RES_DROPPING_REF;
216 spin_unlock(&res->spinlock);
217 wake_up(&res->wq);
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700218 } else
219 spin_unlock(&res->spinlock);
Kurt Hackel8b219802006-05-01 11:16:45 -0700220}
221
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800222static void dlm_run_purge_list(struct dlm_ctxt *dlm,
223 int purge_now)
224{
225 unsigned int run_max, unused;
226 unsigned long purge_jiffies;
227 struct dlm_lock_resource *lockres;
228
229 spin_lock(&dlm->spinlock);
230 run_max = dlm->purge_count;
231
232 while(run_max && !list_empty(&dlm->purge_list)) {
233 run_max--;
234
235 lockres = list_entry(dlm->purge_list.next,
236 struct dlm_lock_resource, purge);
237
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800238 spin_lock(&lockres->spinlock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800239
240 purge_jiffies = lockres->last_used +
241 msecs_to_jiffies(DLM_PURGE_INTERVAL_MS);
242
243 /* Make sure that we want to be processing this guy at
244 * this time. */
245 if (!purge_now && time_after(purge_jiffies, jiffies)) {
246 /* Since resources are added to the purge list
247 * in tail order, we can stop at the first
248 * unpurgable resource -- anyone added after
249 * him will have a greater last_used value */
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700250 spin_unlock(&lockres->spinlock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800251 break;
252 }
253
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700254 /* Status of the lockres *might* change so double
255 * check. If the lockres is unused, holding the dlm
256 * spinlock will prevent people from getting and more
257 * refs on it. */
258 unused = __dlm_lockres_unused(lockres);
259 if (!unused ||
260 (lockres->state & DLM_LOCK_RES_MIGRATING)) {
261 mlog(0, "lockres %s:%.*s: is in use or "
262 "being remastered, used %d, state %d\n",
263 dlm->name, lockres->lockname.len,
264 lockres->lockname.name, !unused, lockres->state);
265 list_move_tail(&dlm->purge_list, &lockres->purge);
266 spin_unlock(&lockres->spinlock);
267 continue;
268 }
269
Sunil Mushran78062cb2007-03-22 17:01:07 -0700270 dlm_lockres_get(lockres);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800271
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700272 dlm_purge_lockres(dlm, lockres);
Sunil Mushran78062cb2007-03-22 17:01:07 -0700273
Sunil Mushran3fca0892007-03-12 13:24:34 -0700274 dlm_lockres_put(lockres);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800275
276 /* Avoid adding any scheduling latencies */
277 cond_resched_lock(&dlm->spinlock);
278 }
279
280 spin_unlock(&dlm->spinlock);
281}
282
283static void dlm_shuffle_lists(struct dlm_ctxt *dlm,
284 struct dlm_lock_resource *res)
285{
286 struct dlm_lock *lock, *target;
287 struct list_head *iter;
288 struct list_head *head;
289 int can_grant = 1;
290
291 //mlog(0, "res->lockname.len=%d\n", res->lockname.len);
292 //mlog(0, "res->lockname.name=%p\n", res->lockname.name);
293 //mlog(0, "shuffle res %.*s\n", res->lockname.len,
294 // res->lockname.name);
295
296 /* because this function is called with the lockres
297 * spinlock, and because we know that it is not migrating/
298 * recovering/in-progress, it is fine to reserve asts and
299 * basts right before queueing them all throughout */
Wengang Wangd9ef7522010-05-17 20:20:44 +0800300 assert_spin_locked(&dlm->ast_lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800301 assert_spin_locked(&res->spinlock);
302 BUG_ON((res->state & (DLM_LOCK_RES_MIGRATING|
303 DLM_LOCK_RES_RECOVERING|
304 DLM_LOCK_RES_IN_PROGRESS)));
305
306converting:
307 if (list_empty(&res->converting))
308 goto blocked;
309 mlog(0, "res %.*s has locks on a convert queue\n", res->lockname.len,
310 res->lockname.name);
311
312 target = list_entry(res->converting.next, struct dlm_lock, list);
313 if (target->ml.convert_type == LKM_IVMODE) {
314 mlog(ML_ERROR, "%.*s: converting a lock with no "
315 "convert_type!\n", res->lockname.len, res->lockname.name);
316 BUG();
317 }
318 head = &res->granted;
319 list_for_each(iter, head) {
320 lock = list_entry(iter, struct dlm_lock, list);
321 if (lock==target)
322 continue;
323 if (!dlm_lock_compatible(lock->ml.type,
324 target->ml.convert_type)) {
325 can_grant = 0;
326 /* queue the BAST if not already */
327 if (lock->ml.highest_blocked == LKM_IVMODE) {
328 __dlm_lockres_reserve_ast(res);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800329 __dlm_queue_bast(dlm, lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800330 }
331 /* update the highest_blocked if needed */
332 if (lock->ml.highest_blocked < target->ml.convert_type)
333 lock->ml.highest_blocked =
334 target->ml.convert_type;
335 }
336 }
337 head = &res->converting;
338 list_for_each(iter, head) {
339 lock = list_entry(iter, struct dlm_lock, list);
340 if (lock==target)
341 continue;
342 if (!dlm_lock_compatible(lock->ml.type,
343 target->ml.convert_type)) {
344 can_grant = 0;
345 if (lock->ml.highest_blocked == LKM_IVMODE) {
346 __dlm_lockres_reserve_ast(res);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800347 __dlm_queue_bast(dlm, lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800348 }
349 if (lock->ml.highest_blocked < target->ml.convert_type)
350 lock->ml.highest_blocked =
351 target->ml.convert_type;
352 }
353 }
354
355 /* we can convert the lock */
356 if (can_grant) {
357 spin_lock(&target->spinlock);
358 BUG_ON(target->ml.highest_blocked != LKM_IVMODE);
359
360 mlog(0, "calling ast for converting lock: %.*s, have: %d, "
361 "granting: %d, node: %u\n", res->lockname.len,
362 res->lockname.name, target->ml.type,
363 target->ml.convert_type, target->ml.node);
364
365 target->ml.type = target->ml.convert_type;
366 target->ml.convert_type = LKM_IVMODE;
Akinobu Mitaf1166292006-06-26 00:24:46 -0700367 list_move_tail(&target->list, &res->granted);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800368
369 BUG_ON(!target->lksb);
370 target->lksb->status = DLM_NORMAL;
371
372 spin_unlock(&target->spinlock);
373
374 __dlm_lockres_reserve_ast(res);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800375 __dlm_queue_ast(dlm, target);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800376 /* go back and check for more */
377 goto converting;
378 }
379
380blocked:
381 if (list_empty(&res->blocked))
382 goto leave;
383 target = list_entry(res->blocked.next, struct dlm_lock, list);
384
385 head = &res->granted;
386 list_for_each(iter, head) {
387 lock = list_entry(iter, struct dlm_lock, list);
388 if (lock==target)
389 continue;
390 if (!dlm_lock_compatible(lock->ml.type, target->ml.type)) {
391 can_grant = 0;
392 if (lock->ml.highest_blocked == LKM_IVMODE) {
393 __dlm_lockres_reserve_ast(res);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800394 __dlm_queue_bast(dlm, lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800395 }
396 if (lock->ml.highest_blocked < target->ml.type)
397 lock->ml.highest_blocked = target->ml.type;
398 }
399 }
400
401 head = &res->converting;
402 list_for_each(iter, head) {
403 lock = list_entry(iter, struct dlm_lock, list);
404 if (lock==target)
405 continue;
406 if (!dlm_lock_compatible(lock->ml.type, target->ml.type)) {
407 can_grant = 0;
408 if (lock->ml.highest_blocked == LKM_IVMODE) {
409 __dlm_lockres_reserve_ast(res);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800410 __dlm_queue_bast(dlm, lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800411 }
412 if (lock->ml.highest_blocked < target->ml.type)
413 lock->ml.highest_blocked = target->ml.type;
414 }
415 }
416
417 /* we can grant the blocked lock (only
418 * possible if converting list empty) */
419 if (can_grant) {
420 spin_lock(&target->spinlock);
421 BUG_ON(target->ml.highest_blocked != LKM_IVMODE);
422
423 mlog(0, "calling ast for blocked lock: %.*s, granting: %d, "
424 "node: %u\n", res->lockname.len, res->lockname.name,
425 target->ml.type, target->ml.node);
426
427 // target->ml.type is already correct
Akinobu Mitaf1166292006-06-26 00:24:46 -0700428 list_move_tail(&target->list, &res->granted);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800429
430 BUG_ON(!target->lksb);
431 target->lksb->status = DLM_NORMAL;
432
433 spin_unlock(&target->spinlock);
434
435 __dlm_lockres_reserve_ast(res);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800436 __dlm_queue_ast(dlm, target);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800437 /* go back and check for more */
438 goto converting;
439 }
440
441leave:
442 return;
443}
444
445/* must have NO locks when calling this with res !=NULL * */
446void dlm_kick_thread(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
447{
448 mlog_entry("dlm=%p, res=%p\n", dlm, res);
449 if (res) {
450 spin_lock(&dlm->spinlock);
451 spin_lock(&res->spinlock);
452 __dlm_dirty_lockres(dlm, res);
453 spin_unlock(&res->spinlock);
454 spin_unlock(&dlm->spinlock);
455 }
456 wake_up(&dlm->dlm_thread_wq);
457}
458
459void __dlm_dirty_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
460{
461 mlog_entry("dlm=%p, res=%p\n", dlm, res);
462
463 assert_spin_locked(&dlm->spinlock);
464 assert_spin_locked(&res->spinlock);
465
466 /* don't shuffle secondary queues */
Kurt Hackelddc09c82007-01-05 15:00:17 -0800467 if ((res->owner == dlm->node_num)) {
468 if (res->state & (DLM_LOCK_RES_MIGRATING |
469 DLM_LOCK_RES_BLOCK_DIRTY))
470 return;
471
472 if (list_empty(&res->dirty)) {
473 /* ref for dirty_list */
474 dlm_lockres_get(res);
475 list_add_tail(&res->dirty, &dlm->dirty_list);
476 res->state |= DLM_LOCK_RES_DIRTY;
477 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800478 }
479}
480
481
482/* Launch the NM thread for the mounted volume */
483int dlm_launch_thread(struct dlm_ctxt *dlm)
484{
485 mlog(0, "starting dlm thread...\n");
486
487 dlm->dlm_thread_task = kthread_run(dlm_thread, dlm, "dlm_thread");
488 if (IS_ERR(dlm->dlm_thread_task)) {
489 mlog_errno(PTR_ERR(dlm->dlm_thread_task));
490 dlm->dlm_thread_task = NULL;
491 return -EINVAL;
492 }
493
494 return 0;
495}
496
497void dlm_complete_thread(struct dlm_ctxt *dlm)
498{
499 if (dlm->dlm_thread_task) {
500 mlog(ML_KTHREAD, "waiting for dlm thread to exit\n");
501 kthread_stop(dlm->dlm_thread_task);
502 dlm->dlm_thread_task = NULL;
503 }
504}
505
506static int dlm_dirty_list_empty(struct dlm_ctxt *dlm)
507{
508 int empty;
509
510 spin_lock(&dlm->spinlock);
511 empty = list_empty(&dlm->dirty_list);
512 spin_unlock(&dlm->spinlock);
513
514 return empty;
515}
516
517static void dlm_flush_asts(struct dlm_ctxt *dlm)
518{
519 int ret;
520 struct dlm_lock *lock;
521 struct dlm_lock_resource *res;
522 u8 hi;
523
524 spin_lock(&dlm->ast_lock);
525 while (!list_empty(&dlm->pending_asts)) {
526 lock = list_entry(dlm->pending_asts.next,
527 struct dlm_lock, ast_list);
528 /* get an extra ref on lock */
529 dlm_lock_get(lock);
530 res = lock->lockres;
531 mlog(0, "delivering an ast for this lockres\n");
532
533 BUG_ON(!lock->ast_pending);
534
535 /* remove from list (including ref) */
536 list_del_init(&lock->ast_list);
537 dlm_lock_put(lock);
538 spin_unlock(&dlm->ast_lock);
539
540 if (lock->ml.node != dlm->node_num) {
541 ret = dlm_do_remote_ast(dlm, res, lock);
542 if (ret < 0)
543 mlog_errno(ret);
544 } else
545 dlm_do_local_ast(dlm, res, lock);
546
547 spin_lock(&dlm->ast_lock);
548
549 /* possible that another ast was queued while
550 * we were delivering the last one */
551 if (!list_empty(&lock->ast_list)) {
552 mlog(0, "aha another ast got queued while "
553 "we were finishing the last one. will "
554 "keep the ast_pending flag set.\n");
555 } else
556 lock->ast_pending = 0;
557
558 /* drop the extra ref.
559 * this may drop it completely. */
560 dlm_lock_put(lock);
561 dlm_lockres_release_ast(dlm, res);
562 }
563
564 while (!list_empty(&dlm->pending_basts)) {
565 lock = list_entry(dlm->pending_basts.next,
566 struct dlm_lock, bast_list);
567 /* get an extra ref on lock */
568 dlm_lock_get(lock);
569 res = lock->lockres;
570
571 BUG_ON(!lock->bast_pending);
572
573 /* get the highest blocked lock, and reset */
574 spin_lock(&lock->spinlock);
575 BUG_ON(lock->ml.highest_blocked <= LKM_IVMODE);
576 hi = lock->ml.highest_blocked;
577 lock->ml.highest_blocked = LKM_IVMODE;
578 spin_unlock(&lock->spinlock);
579
580 /* remove from list (including ref) */
581 list_del_init(&lock->bast_list);
582 dlm_lock_put(lock);
583 spin_unlock(&dlm->ast_lock);
584
585 mlog(0, "delivering a bast for this lockres "
586 "(blocked = %d\n", hi);
587
588 if (lock->ml.node != dlm->node_num) {
589 ret = dlm_send_proxy_bast(dlm, res, lock, hi);
590 if (ret < 0)
591 mlog_errno(ret);
592 } else
593 dlm_do_local_bast(dlm, res, lock, hi);
594
595 spin_lock(&dlm->ast_lock);
596
597 /* possible that another bast was queued while
598 * we were delivering the last one */
599 if (!list_empty(&lock->bast_list)) {
600 mlog(0, "aha another bast got queued while "
601 "we were finishing the last one. will "
602 "keep the bast_pending flag set.\n");
603 } else
604 lock->bast_pending = 0;
605
606 /* drop the extra ref.
607 * this may drop it completely. */
608 dlm_lock_put(lock);
609 dlm_lockres_release_ast(dlm, res);
610 }
611 wake_up(&dlm->ast_wq);
612 spin_unlock(&dlm->ast_lock);
613}
614
615
616#define DLM_THREAD_TIMEOUT_MS (4 * 1000)
617#define DLM_THREAD_MAX_DIRTY 100
618#define DLM_THREAD_MAX_ASTS 10
619
620static int dlm_thread(void *data)
621{
622 struct dlm_lock_resource *res;
623 struct dlm_ctxt *dlm = data;
624 unsigned long timeout = msecs_to_jiffies(DLM_THREAD_TIMEOUT_MS);
625
626 mlog(0, "dlm thread running for %s...\n", dlm->name);
627
628 while (!kthread_should_stop()) {
629 int n = DLM_THREAD_MAX_DIRTY;
630
631 /* dlm_shutting_down is very point-in-time, but that
632 * doesn't matter as we'll just loop back around if we
633 * get false on the leading edge of a state
634 * transition. */
635 dlm_run_purge_list(dlm, dlm_shutting_down(dlm));
636
637 /* We really don't want to hold dlm->spinlock while
638 * calling dlm_shuffle_lists on each lockres that
639 * needs to have its queues adjusted and AST/BASTs
640 * run. So let's pull each entry off the dirty_list
641 * and drop dlm->spinlock ASAP. Once off the list,
642 * res->spinlock needs to be taken again to protect
643 * the queues while calling dlm_shuffle_lists. */
644 spin_lock(&dlm->spinlock);
645 while (!list_empty(&dlm->dirty_list)) {
646 int delay = 0;
647 res = list_entry(dlm->dirty_list.next,
648 struct dlm_lock_resource, dirty);
649
650 /* peel a lockres off, remove it from the list,
651 * unset the dirty flag and drop the dlm lock */
652 BUG_ON(!res);
653 dlm_lockres_get(res);
654
655 spin_lock(&res->spinlock);
Kurt Hackelddc09c82007-01-05 15:00:17 -0800656 /* We clear the DLM_LOCK_RES_DIRTY state once we shuffle lists below */
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800657 list_del_init(&res->dirty);
658 spin_unlock(&res->spinlock);
659 spin_unlock(&dlm->spinlock);
Kurt Hackel6ff06a92006-05-01 11:51:45 -0700660 /* Drop dirty_list ref */
661 dlm_lockres_put(res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800662
663 /* lockres can be re-dirtied/re-added to the
664 * dirty_list in this gap, but that is ok */
665
Wengang Wangd9ef7522010-05-17 20:20:44 +0800666 spin_lock(&dlm->ast_lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800667 spin_lock(&res->spinlock);
668 if (res->owner != dlm->node_num) {
669 __dlm_print_one_lock_resource(res);
670 mlog(ML_ERROR, "inprog:%s, mig:%s, reco:%s, dirty:%s\n",
671 res->state & DLM_LOCK_RES_IN_PROGRESS ? "yes" : "no",
672 res->state & DLM_LOCK_RES_MIGRATING ? "yes" : "no",
673 res->state & DLM_LOCK_RES_RECOVERING ? "yes" : "no",
674 res->state & DLM_LOCK_RES_DIRTY ? "yes" : "no");
675 }
676 BUG_ON(res->owner != dlm->node_num);
677
678 /* it is now ok to move lockreses in these states
679 * to the dirty list, assuming that they will only be
680 * dirty for a short while. */
Kurt Hackelddc09c82007-01-05 15:00:17 -0800681 BUG_ON(res->state & DLM_LOCK_RES_MIGRATING);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800682 if (res->state & (DLM_LOCK_RES_IN_PROGRESS |
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800683 DLM_LOCK_RES_RECOVERING)) {
684 /* move it to the tail and keep going */
Kurt Hackelddc09c82007-01-05 15:00:17 -0800685 res->state &= ~DLM_LOCK_RES_DIRTY;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800686 spin_unlock(&res->spinlock);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800687 spin_unlock(&dlm->ast_lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800688 mlog(0, "delaying list shuffling for in-"
689 "progress lockres %.*s, state=%d\n",
690 res->lockname.len, res->lockname.name,
691 res->state);
692 delay = 1;
693 goto in_progress;
694 }
695
696 /* at this point the lockres is not migrating/
697 * recovering/in-progress. we have the lockres
698 * spinlock and do NOT have the dlm lock.
699 * safe to reserve/queue asts and run the lists. */
700
Kurt Hackel8d79d082006-04-27 17:58:23 -0700701 mlog(0, "calling dlm_shuffle_lists with dlm=%s, "
702 "res=%.*s\n", dlm->name,
703 res->lockname.len, res->lockname.name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800704
705 /* called while holding lockres lock */
706 dlm_shuffle_lists(dlm, res);
Kurt Hackelddc09c82007-01-05 15:00:17 -0800707 res->state &= ~DLM_LOCK_RES_DIRTY;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800708 spin_unlock(&res->spinlock);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800709 spin_unlock(&dlm->ast_lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800710
711 dlm_lockres_calc_usage(dlm, res);
712
713in_progress:
714
715 spin_lock(&dlm->spinlock);
716 /* if the lock was in-progress, stick
717 * it on the back of the list */
718 if (delay) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800719 spin_lock(&res->spinlock);
Kurt Hackelddc09c82007-01-05 15:00:17 -0800720 __dlm_dirty_lockres(dlm, res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800721 spin_unlock(&res->spinlock);
722 }
723 dlm_lockres_put(res);
724
725 /* unlikely, but we may need to give time to
726 * other tasks */
727 if (!--n) {
728 mlog(0, "throttling dlm_thread\n");
729 break;
730 }
731 }
732
733 spin_unlock(&dlm->spinlock);
734 dlm_flush_asts(dlm);
735
736 /* yield and continue right away if there is more work to do */
737 if (!n) {
Kurt Hackelf85cd472006-05-01 14:27:41 -0700738 cond_resched();
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800739 continue;
740 }
741
742 wait_event_interruptible_timeout(dlm->dlm_thread_wq,
743 !dlm_dirty_list_empty(dlm) ||
744 kthread_should_stop(),
745 timeout);
746 }
747
748 mlog(0, "quitting DLM thread\n");
749 return 0;
750}