blob: 9db869de829d0ebcf35ff598c3ee4b7541934f26 [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{
Wengang Wanga5248122010-07-30 16:14:44 +080095 int bit;
96
Sunil Mushranff0a5222011-07-24 10:29:54 -070097 assert_spin_locked(&res->spinlock);
98
Wengang Wanga5248122010-07-30 16:14:44 +080099 if (__dlm_lockres_has_locks(res))
100 return 0;
101
Sunil Mushranff0a5222011-07-24 10:29:54 -0700102 /* Locks are in the process of being created */
103 if (res->inflight_locks)
104 return 0;
105
Wengang Wanga5248122010-07-30 16:14:44 +0800106 if (!list_empty(&res->dirty) || res->state & DLM_LOCK_RES_DIRTY)
107 return 0;
108
109 if (res->state & DLM_LOCK_RES_RECOVERING)
110 return 0;
111
Sunil Mushranff0a5222011-07-24 10:29:54 -0700112 /* Another node has this resource with this node as the master */
Wengang Wanga5248122010-07-30 16:14:44 +0800113 bit = find_next_bit(res->refmap, O2NM_MAX_NODES, 0);
114 if (bit < O2NM_MAX_NODES)
115 return 0;
116
Wengang Wanga5248122010-07-30 16:14:44 +0800117 return 1;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800118}
119
120
121/* Call whenever you may have added or deleted something from one of
122 * the lockres queue's. This will figure out whether it belongs on the
123 * unused list or not and does the appropriate thing. */
124void __dlm_lockres_calc_usage(struct dlm_ctxt *dlm,
125 struct dlm_lock_resource *res)
126{
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800127 assert_spin_locked(&dlm->spinlock);
128 assert_spin_locked(&res->spinlock);
129
130 if (__dlm_lockres_unused(res)){
131 if (list_empty(&res->purge)) {
Sunil Mushran8e17d162010-11-19 15:06:49 -0800132 mlog(0, "%s: Adding res %.*s to purge list\n",
133 dlm->name, res->lockname.len, res->lockname.name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800134
135 res->last_used = jiffies;
Kurt Hackelba2bf212006-12-01 14:47:20 -0800136 dlm_lockres_get(res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800137 list_add_tail(&res->purge, &dlm->purge_list);
138 dlm->purge_count++;
139 }
140 } else if (!list_empty(&res->purge)) {
Sunil Mushran8e17d162010-11-19 15:06:49 -0800141 mlog(0, "%s: Removing res %.*s from purge list\n",
142 dlm->name, res->lockname.len, res->lockname.name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800143
144 list_del_init(&res->purge);
Kurt Hackelba2bf212006-12-01 14:47:20 -0800145 dlm_lockres_put(res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800146 dlm->purge_count--;
147 }
148}
149
150void dlm_lockres_calc_usage(struct dlm_ctxt *dlm,
151 struct dlm_lock_resource *res)
152{
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800153 spin_lock(&dlm->spinlock);
154 spin_lock(&res->spinlock);
155
156 __dlm_lockres_calc_usage(dlm, res);
157
158 spin_unlock(&res->spinlock);
159 spin_unlock(&dlm->spinlock);
160}
161
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700162static void dlm_purge_lockres(struct dlm_ctxt *dlm,
Adrian Bunkfaf0ec92006-12-14 00:17:32 +0100163 struct dlm_lock_resource *res)
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800164{
165 int master;
Kurt Hackelba2bf212006-12-01 14:47:20 -0800166 int ret = 0;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800167
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700168 assert_spin_locked(&dlm->spinlock);
169 assert_spin_locked(&res->spinlock);
Sunil Mushran516b7e52009-02-26 15:00:48 -0800170
Kurt Hackelba2bf212006-12-01 14:47:20 -0800171 master = (res->owner == dlm->node_num);
Sunil Mushran516b7e52009-02-26 15:00:48 -0800172
Sunil Mushran8e17d162010-11-19 15:06:49 -0800173 mlog(0, "%s: Purging res %.*s, master %d\n", dlm->name,
174 res->lockname.len, res->lockname.name, master);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800175
Kurt Hackelba2bf212006-12-01 14:47:20 -0800176 if (!master) {
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700177 res->state |= DLM_LOCK_RES_DROPPING_REF;
Sunil Mushranc824c3c2008-03-01 14:04:25 -0800178 /* drop spinlock... retake below */
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700179 spin_unlock(&res->spinlock);
Sunil Mushranc824c3c2008-03-01 14:04:25 -0800180 spin_unlock(&dlm->spinlock);
181
Kurt Hackel3b8118c2007-01-17 17:05:53 -0800182 spin_lock(&res->spinlock);
183 /* This ensures that clear refmap is sent after the set */
Sunil Mushran7dc102b2009-02-03 12:37:13 -0800184 __dlm_wait_on_lockres_flags(res, DLM_LOCK_RES_SETREF_INPROG);
Kurt Hackel3b8118c2007-01-17 17:05:53 -0800185 spin_unlock(&res->spinlock);
Sunil Mushranc824c3c2008-03-01 14:04:25 -0800186
Kurt Hackelba2bf212006-12-01 14:47:20 -0800187 /* clear our bit from the master's refmap, ignore errors */
188 ret = dlm_drop_lockres_ref(dlm, res);
189 if (ret < 0) {
Kurt Hackelba2bf212006-12-01 14:47:20 -0800190 if (!dlm_is_host_down(ret))
191 BUG();
192 }
Kurt Hackelba2bf212006-12-01 14:47:20 -0800193 spin_lock(&dlm->spinlock);
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700194 spin_lock(&res->spinlock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800195 }
196
Kurt Hackelba2bf212006-12-01 14:47:20 -0800197 if (!list_empty(&res->purge)) {
Sunil Mushran8e17d162010-11-19 15:06:49 -0800198 mlog(0, "%s: Removing res %.*s from purgelist, master %d\n",
199 dlm->name, res->lockname.len, res->lockname.name, master);
Kurt Hackelba2bf212006-12-01 14:47:20 -0800200 list_del_init(&res->purge);
201 dlm_lockres_put(res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800202 dlm->purge_count--;
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700203 }
204
205 if (!__dlm_lockres_unused(res)) {
Sunil Mushran8e17d162010-11-19 15:06:49 -0800206 mlog(ML_ERROR, "%s: res %.*s in use after deref\n",
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700207 dlm->name, res->lockname.len, res->lockname.name);
208 __dlm_print_one_lock_resource(res);
209 BUG();
210 }
Wengang Wang83e32d92009-09-03 15:56:33 +0800211
Sunil Mushrane9f0b6a2011-07-24 10:27:54 -0700212 __dlm_unhash_lockres(dlm, res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800213
Kurt Hackelba2bf212006-12-01 14:47:20 -0800214 /* lockres is not in the hash now. drop the flag and wake up
215 * any processes waiting in dlm_get_lock_resource. */
216 if (!master) {
Kurt Hackelba2bf212006-12-01 14:47:20 -0800217 res->state &= ~DLM_LOCK_RES_DROPPING_REF;
218 spin_unlock(&res->spinlock);
219 wake_up(&res->wq);
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700220 } else
221 spin_unlock(&res->spinlock);
Kurt Hackel8b219802006-05-01 11:16:45 -0700222}
223
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800224static void dlm_run_purge_list(struct dlm_ctxt *dlm,
225 int purge_now)
226{
227 unsigned int run_max, unused;
228 unsigned long purge_jiffies;
229 struct dlm_lock_resource *lockres;
230
231 spin_lock(&dlm->spinlock);
232 run_max = dlm->purge_count;
233
234 while(run_max && !list_empty(&dlm->purge_list)) {
235 run_max--;
236
237 lockres = list_entry(dlm->purge_list.next,
238 struct dlm_lock_resource, purge);
239
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800240 spin_lock(&lockres->spinlock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800241
242 purge_jiffies = lockres->last_used +
243 msecs_to_jiffies(DLM_PURGE_INTERVAL_MS);
244
245 /* Make sure that we want to be processing this guy at
246 * this time. */
247 if (!purge_now && time_after(purge_jiffies, jiffies)) {
248 /* Since resources are added to the purge list
249 * in tail order, we can stop at the first
250 * unpurgable resource -- anyone added after
251 * him will have a greater last_used value */
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700252 spin_unlock(&lockres->spinlock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800253 break;
254 }
255
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700256 /* Status of the lockres *might* change so double
257 * check. If the lockres is unused, holding the dlm
258 * spinlock will prevent people from getting and more
259 * refs on it. */
260 unused = __dlm_lockres_unused(lockres);
261 if (!unused ||
262 (lockres->state & DLM_LOCK_RES_MIGRATING)) {
Sunil Mushran8e17d162010-11-19 15:06:49 -0800263 mlog(0, "%s: res %.*s is in use or being remastered, "
264 "used %d, state %d\n", dlm->name,
265 lockres->lockname.len, lockres->lockname.name,
266 !unused, lockres->state);
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700267 list_move_tail(&dlm->purge_list, &lockres->purge);
268 spin_unlock(&lockres->spinlock);
269 continue;
270 }
271
Sunil Mushran78062cb2007-03-22 17:01:07 -0700272 dlm_lockres_get(lockres);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800273
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700274 dlm_purge_lockres(dlm, lockres);
Sunil Mushran78062cb2007-03-22 17:01:07 -0700275
Sunil Mushran3fca0892007-03-12 13:24:34 -0700276 dlm_lockres_put(lockres);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800277
278 /* Avoid adding any scheduling latencies */
279 cond_resched_lock(&dlm->spinlock);
280 }
281
282 spin_unlock(&dlm->spinlock);
283}
284
285static void dlm_shuffle_lists(struct dlm_ctxt *dlm,
286 struct dlm_lock_resource *res)
287{
288 struct dlm_lock *lock, *target;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800289 int can_grant = 1;
290
Sunil Mushran8e17d162010-11-19 15:06:49 -0800291 /*
292 * Because this function is called with the lockres
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800293 * spinlock, and because we know that it is not migrating/
294 * recovering/in-progress, it is fine to reserve asts and
Sunil Mushran8e17d162010-11-19 15:06:49 -0800295 * basts right before queueing them all throughout
296 */
Wengang Wangd9ef7522010-05-17 20:20:44 +0800297 assert_spin_locked(&dlm->ast_lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800298 assert_spin_locked(&res->spinlock);
299 BUG_ON((res->state & (DLM_LOCK_RES_MIGRATING|
300 DLM_LOCK_RES_RECOVERING|
301 DLM_LOCK_RES_IN_PROGRESS)));
302
303converting:
304 if (list_empty(&res->converting))
305 goto blocked;
Sunil Mushran8e17d162010-11-19 15:06:49 -0800306 mlog(0, "%s: res %.*s has locks on the convert queue\n", dlm->name,
307 res->lockname.len, res->lockname.name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800308
309 target = list_entry(res->converting.next, struct dlm_lock, list);
310 if (target->ml.convert_type == LKM_IVMODE) {
Sunil Mushran8e17d162010-11-19 15:06:49 -0800311 mlog(ML_ERROR, "%s: res %.*s converting lock to invalid mode\n",
312 dlm->name, res->lockname.len, res->lockname.name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800313 BUG();
314 }
Dong Fangdf53cd32013-09-11 14:19:50 -0700315 list_for_each_entry(lock, &res->granted, list) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800316 if (lock==target)
317 continue;
318 if (!dlm_lock_compatible(lock->ml.type,
319 target->ml.convert_type)) {
320 can_grant = 0;
321 /* queue the BAST if not already */
322 if (lock->ml.highest_blocked == LKM_IVMODE) {
323 __dlm_lockres_reserve_ast(res);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800324 __dlm_queue_bast(dlm, lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800325 }
326 /* update the highest_blocked if needed */
327 if (lock->ml.highest_blocked < target->ml.convert_type)
328 lock->ml.highest_blocked =
329 target->ml.convert_type;
330 }
331 }
Dong Fangdf53cd32013-09-11 14:19:50 -0700332
333 list_for_each_entry(lock, &res->converting, list) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800334 if (lock==target)
335 continue;
336 if (!dlm_lock_compatible(lock->ml.type,
337 target->ml.convert_type)) {
338 can_grant = 0;
339 if (lock->ml.highest_blocked == LKM_IVMODE) {
340 __dlm_lockres_reserve_ast(res);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800341 __dlm_queue_bast(dlm, lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800342 }
343 if (lock->ml.highest_blocked < target->ml.convert_type)
344 lock->ml.highest_blocked =
345 target->ml.convert_type;
346 }
347 }
348
349 /* we can convert the lock */
350 if (can_grant) {
351 spin_lock(&target->spinlock);
352 BUG_ON(target->ml.highest_blocked != LKM_IVMODE);
353
Sunil Mushran8e17d162010-11-19 15:06:49 -0800354 mlog(0, "%s: res %.*s, AST for Converting lock %u:%llu, type "
355 "%d => %d, node %u\n", dlm->name, res->lockname.len,
356 res->lockname.name,
357 dlm_get_lock_cookie_node(be64_to_cpu(target->ml.cookie)),
358 dlm_get_lock_cookie_seq(be64_to_cpu(target->ml.cookie)),
359 target->ml.type,
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800360 target->ml.convert_type, target->ml.node);
361
362 target->ml.type = target->ml.convert_type;
363 target->ml.convert_type = LKM_IVMODE;
Akinobu Mitaf1166292006-06-26 00:24:46 -0700364 list_move_tail(&target->list, &res->granted);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800365
366 BUG_ON(!target->lksb);
367 target->lksb->status = DLM_NORMAL;
368
369 spin_unlock(&target->spinlock);
370
371 __dlm_lockres_reserve_ast(res);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800372 __dlm_queue_ast(dlm, target);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800373 /* go back and check for more */
374 goto converting;
375 }
376
377blocked:
378 if (list_empty(&res->blocked))
379 goto leave;
380 target = list_entry(res->blocked.next, struct dlm_lock, list);
381
Dong Fangdf53cd32013-09-11 14:19:50 -0700382 list_for_each_entry(lock, &res->granted, list) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800383 if (lock==target)
384 continue;
385 if (!dlm_lock_compatible(lock->ml.type, target->ml.type)) {
386 can_grant = 0;
387 if (lock->ml.highest_blocked == LKM_IVMODE) {
388 __dlm_lockres_reserve_ast(res);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800389 __dlm_queue_bast(dlm, lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800390 }
391 if (lock->ml.highest_blocked < target->ml.type)
392 lock->ml.highest_blocked = target->ml.type;
393 }
394 }
395
Dong Fangdf53cd32013-09-11 14:19:50 -0700396 list_for_each_entry(lock, &res->converting, list) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800397 if (lock==target)
398 continue;
399 if (!dlm_lock_compatible(lock->ml.type, target->ml.type)) {
400 can_grant = 0;
401 if (lock->ml.highest_blocked == LKM_IVMODE) {
402 __dlm_lockres_reserve_ast(res);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800403 __dlm_queue_bast(dlm, lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800404 }
405 if (lock->ml.highest_blocked < target->ml.type)
406 lock->ml.highest_blocked = target->ml.type;
407 }
408 }
409
410 /* we can grant the blocked lock (only
411 * possible if converting list empty) */
412 if (can_grant) {
413 spin_lock(&target->spinlock);
414 BUG_ON(target->ml.highest_blocked != LKM_IVMODE);
415
Sunil Mushran8e17d162010-11-19 15:06:49 -0800416 mlog(0, "%s: res %.*s, AST for Blocked lock %u:%llu, type %d, "
417 "node %u\n", dlm->name, res->lockname.len,
418 res->lockname.name,
419 dlm_get_lock_cookie_node(be64_to_cpu(target->ml.cookie)),
420 dlm_get_lock_cookie_seq(be64_to_cpu(target->ml.cookie)),
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800421 target->ml.type, target->ml.node);
422
Sunil Mushran8e17d162010-11-19 15:06:49 -0800423 /* target->ml.type is already correct */
Akinobu Mitaf1166292006-06-26 00:24:46 -0700424 list_move_tail(&target->list, &res->granted);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800425
426 BUG_ON(!target->lksb);
427 target->lksb->status = DLM_NORMAL;
428
429 spin_unlock(&target->spinlock);
430
431 __dlm_lockres_reserve_ast(res);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800432 __dlm_queue_ast(dlm, target);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800433 /* go back and check for more */
434 goto converting;
435 }
436
437leave:
438 return;
439}
440
441/* must have NO locks when calling this with res !=NULL * */
442void dlm_kick_thread(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
443{
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800444 if (res) {
445 spin_lock(&dlm->spinlock);
446 spin_lock(&res->spinlock);
447 __dlm_dirty_lockres(dlm, res);
448 spin_unlock(&res->spinlock);
449 spin_unlock(&dlm->spinlock);
450 }
451 wake_up(&dlm->dlm_thread_wq);
452}
453
454void __dlm_dirty_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
455{
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800456 assert_spin_locked(&dlm->spinlock);
457 assert_spin_locked(&res->spinlock);
458
459 /* don't shuffle secondary queues */
Kurt Hackelddc09c82007-01-05 15:00:17 -0800460 if ((res->owner == dlm->node_num)) {
461 if (res->state & (DLM_LOCK_RES_MIGRATING |
462 DLM_LOCK_RES_BLOCK_DIRTY))
463 return;
464
465 if (list_empty(&res->dirty)) {
466 /* ref for dirty_list */
467 dlm_lockres_get(res);
468 list_add_tail(&res->dirty, &dlm->dirty_list);
469 res->state |= DLM_LOCK_RES_DIRTY;
470 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800471 }
Sunil Mushran8e17d162010-11-19 15:06:49 -0800472
473 mlog(0, "%s: res %.*s\n", dlm->name, res->lockname.len,
474 res->lockname.name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800475}
476
477
478/* Launch the NM thread for the mounted volume */
479int dlm_launch_thread(struct dlm_ctxt *dlm)
480{
Sunil Mushran8e17d162010-11-19 15:06:49 -0800481 mlog(0, "Starting dlm_thread...\n");
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800482
483 dlm->dlm_thread_task = kthread_run(dlm_thread, dlm, "dlm_thread");
484 if (IS_ERR(dlm->dlm_thread_task)) {
485 mlog_errno(PTR_ERR(dlm->dlm_thread_task));
486 dlm->dlm_thread_task = NULL;
487 return -EINVAL;
488 }
489
490 return 0;
491}
492
493void dlm_complete_thread(struct dlm_ctxt *dlm)
494{
495 if (dlm->dlm_thread_task) {
Sunil Mushran8e17d162010-11-19 15:06:49 -0800496 mlog(ML_KTHREAD, "Waiting for dlm thread to exit\n");
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800497 kthread_stop(dlm->dlm_thread_task);
498 dlm->dlm_thread_task = NULL;
499 }
500}
501
502static int dlm_dirty_list_empty(struct dlm_ctxt *dlm)
503{
504 int empty;
505
506 spin_lock(&dlm->spinlock);
507 empty = list_empty(&dlm->dirty_list);
508 spin_unlock(&dlm->spinlock);
509
510 return empty;
511}
512
513static void dlm_flush_asts(struct dlm_ctxt *dlm)
514{
515 int ret;
516 struct dlm_lock *lock;
517 struct dlm_lock_resource *res;
518 u8 hi;
519
520 spin_lock(&dlm->ast_lock);
521 while (!list_empty(&dlm->pending_asts)) {
522 lock = list_entry(dlm->pending_asts.next,
523 struct dlm_lock, ast_list);
524 /* get an extra ref on lock */
525 dlm_lock_get(lock);
526 res = lock->lockres;
Sunil Mushran8e17d162010-11-19 15:06:49 -0800527 mlog(0, "%s: res %.*s, Flush AST for lock %u:%llu, type %d, "
528 "node %u\n", dlm->name, res->lockname.len,
529 res->lockname.name,
530 dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
531 dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)),
532 lock->ml.type, lock->ml.node);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800533
534 BUG_ON(!lock->ast_pending);
535
536 /* remove from list (including ref) */
537 list_del_init(&lock->ast_list);
538 dlm_lock_put(lock);
539 spin_unlock(&dlm->ast_lock);
540
541 if (lock->ml.node != dlm->node_num) {
542 ret = dlm_do_remote_ast(dlm, res, lock);
543 if (ret < 0)
544 mlog_errno(ret);
545 } else
546 dlm_do_local_ast(dlm, res, lock);
547
548 spin_lock(&dlm->ast_lock);
549
550 /* possible that another ast was queued while
551 * we were delivering the last one */
552 if (!list_empty(&lock->ast_list)) {
Sunil Mushran8e17d162010-11-19 15:06:49 -0800553 mlog(0, "%s: res %.*s, AST queued while flushing last "
554 "one\n", dlm->name, res->lockname.len,
555 res->lockname.name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800556 } else
557 lock->ast_pending = 0;
558
559 /* drop the extra ref.
560 * this may drop it completely. */
561 dlm_lock_put(lock);
562 dlm_lockres_release_ast(dlm, res);
563 }
564
565 while (!list_empty(&dlm->pending_basts)) {
566 lock = list_entry(dlm->pending_basts.next,
567 struct dlm_lock, bast_list);
568 /* get an extra ref on lock */
569 dlm_lock_get(lock);
570 res = lock->lockres;
571
572 BUG_ON(!lock->bast_pending);
573
574 /* get the highest blocked lock, and reset */
575 spin_lock(&lock->spinlock);
576 BUG_ON(lock->ml.highest_blocked <= LKM_IVMODE);
577 hi = lock->ml.highest_blocked;
578 lock->ml.highest_blocked = LKM_IVMODE;
579 spin_unlock(&lock->spinlock);
580
581 /* remove from list (including ref) */
582 list_del_init(&lock->bast_list);
583 dlm_lock_put(lock);
584 spin_unlock(&dlm->ast_lock);
585
Sunil Mushran8e17d162010-11-19 15:06:49 -0800586 mlog(0, "%s: res %.*s, Flush BAST for lock %u:%llu, "
587 "blocked %d, node %u\n",
588 dlm->name, res->lockname.len, res->lockname.name,
589 dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
590 dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)),
591 hi, lock->ml.node);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800592
593 if (lock->ml.node != dlm->node_num) {
594 ret = dlm_send_proxy_bast(dlm, res, lock, hi);
595 if (ret < 0)
596 mlog_errno(ret);
597 } else
598 dlm_do_local_bast(dlm, res, lock, hi);
599
600 spin_lock(&dlm->ast_lock);
601
602 /* possible that another bast was queued while
603 * we were delivering the last one */
604 if (!list_empty(&lock->bast_list)) {
Sunil Mushran8e17d162010-11-19 15:06:49 -0800605 mlog(0, "%s: res %.*s, BAST queued while flushing last "
606 "one\n", dlm->name, res->lockname.len,
607 res->lockname.name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800608 } else
609 lock->bast_pending = 0;
610
611 /* drop the extra ref.
612 * this may drop it completely. */
613 dlm_lock_put(lock);
614 dlm_lockres_release_ast(dlm, res);
615 }
616 wake_up(&dlm->ast_wq);
617 spin_unlock(&dlm->ast_lock);
618}
619
620
621#define DLM_THREAD_TIMEOUT_MS (4 * 1000)
622#define DLM_THREAD_MAX_DIRTY 100
623#define DLM_THREAD_MAX_ASTS 10
624
625static int dlm_thread(void *data)
626{
627 struct dlm_lock_resource *res;
628 struct dlm_ctxt *dlm = data;
629 unsigned long timeout = msecs_to_jiffies(DLM_THREAD_TIMEOUT_MS);
630
631 mlog(0, "dlm thread running for %s...\n", dlm->name);
632
633 while (!kthread_should_stop()) {
634 int n = DLM_THREAD_MAX_DIRTY;
635
636 /* dlm_shutting_down is very point-in-time, but that
637 * doesn't matter as we'll just loop back around if we
638 * get false on the leading edge of a state
639 * transition. */
640 dlm_run_purge_list(dlm, dlm_shutting_down(dlm));
641
642 /* We really don't want to hold dlm->spinlock while
643 * calling dlm_shuffle_lists on each lockres that
644 * needs to have its queues adjusted and AST/BASTs
645 * run. So let's pull each entry off the dirty_list
646 * and drop dlm->spinlock ASAP. Once off the list,
647 * res->spinlock needs to be taken again to protect
648 * the queues while calling dlm_shuffle_lists. */
649 spin_lock(&dlm->spinlock);
650 while (!list_empty(&dlm->dirty_list)) {
651 int delay = 0;
652 res = list_entry(dlm->dirty_list.next,
653 struct dlm_lock_resource, dirty);
654
655 /* peel a lockres off, remove it from the list,
656 * unset the dirty flag and drop the dlm lock */
657 BUG_ON(!res);
658 dlm_lockres_get(res);
659
660 spin_lock(&res->spinlock);
Kurt Hackelddc09c82007-01-05 15:00:17 -0800661 /* We clear the DLM_LOCK_RES_DIRTY state once we shuffle lists below */
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800662 list_del_init(&res->dirty);
663 spin_unlock(&res->spinlock);
664 spin_unlock(&dlm->spinlock);
Kurt Hackel6ff06a92006-05-01 11:51:45 -0700665 /* Drop dirty_list ref */
666 dlm_lockres_put(res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800667
668 /* lockres can be re-dirtied/re-added to the
669 * dirty_list in this gap, but that is ok */
670
Wengang Wangd9ef7522010-05-17 20:20:44 +0800671 spin_lock(&dlm->ast_lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800672 spin_lock(&res->spinlock);
673 if (res->owner != dlm->node_num) {
674 __dlm_print_one_lock_resource(res);
Sunil Mushran8e17d162010-11-19 15:06:49 -0800675 mlog(ML_ERROR, "%s: inprog %d, mig %d, reco %d,"
676 " dirty %d\n", dlm->name,
677 !!(res->state & DLM_LOCK_RES_IN_PROGRESS),
678 !!(res->state & DLM_LOCK_RES_MIGRATING),
679 !!(res->state & DLM_LOCK_RES_RECOVERING),
680 !!(res->state & DLM_LOCK_RES_DIRTY));
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800681 }
682 BUG_ON(res->owner != dlm->node_num);
683
684 /* it is now ok to move lockreses in these states
685 * to the dirty list, assuming that they will only be
686 * dirty for a short while. */
Kurt Hackelddc09c82007-01-05 15:00:17 -0800687 BUG_ON(res->state & DLM_LOCK_RES_MIGRATING);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800688 if (res->state & (DLM_LOCK_RES_IN_PROGRESS |
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800689 DLM_LOCK_RES_RECOVERING)) {
690 /* move it to the tail and keep going */
Kurt Hackelddc09c82007-01-05 15:00:17 -0800691 res->state &= ~DLM_LOCK_RES_DIRTY;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800692 spin_unlock(&res->spinlock);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800693 spin_unlock(&dlm->ast_lock);
Sunil Mushran8e17d162010-11-19 15:06:49 -0800694 mlog(0, "%s: res %.*s, inprogress, delay list "
695 "shuffle, state %d\n", dlm->name,
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800696 res->lockname.len, res->lockname.name,
697 res->state);
698 delay = 1;
699 goto in_progress;
700 }
701
702 /* at this point the lockres is not migrating/
703 * recovering/in-progress. we have the lockres
704 * spinlock and do NOT have the dlm lock.
705 * safe to reserve/queue asts and run the lists. */
706
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800707 /* called while holding lockres lock */
708 dlm_shuffle_lists(dlm, res);
Kurt Hackelddc09c82007-01-05 15:00:17 -0800709 res->state &= ~DLM_LOCK_RES_DIRTY;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800710 spin_unlock(&res->spinlock);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800711 spin_unlock(&dlm->ast_lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800712
713 dlm_lockres_calc_usage(dlm, res);
714
715in_progress:
716
717 spin_lock(&dlm->spinlock);
718 /* if the lock was in-progress, stick
719 * it on the back of the list */
720 if (delay) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800721 spin_lock(&res->spinlock);
Kurt Hackelddc09c82007-01-05 15:00:17 -0800722 __dlm_dirty_lockres(dlm, res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800723 spin_unlock(&res->spinlock);
724 }
725 dlm_lockres_put(res);
726
727 /* unlikely, but we may need to give time to
728 * other tasks */
729 if (!--n) {
Sunil Mushran8e17d162010-11-19 15:06:49 -0800730 mlog(0, "%s: Throttling dlm thread\n",
731 dlm->name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800732 break;
733 }
734 }
735
736 spin_unlock(&dlm->spinlock);
737 dlm_flush_asts(dlm);
738
739 /* yield and continue right away if there is more work to do */
740 if (!n) {
Kurt Hackelf85cd472006-05-01 14:27:41 -0700741 cond_resched();
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800742 continue;
743 }
744
745 wait_event_interruptible_timeout(dlm->dlm_thread_wq,
746 !dlm_dirty_list_empty(dlm) ||
747 kthread_should_stop(),
748 timeout);
749 }
750
751 mlog(0, "quitting DLM thread\n");
752 return 0;
753}