blob: 838a06d4066a6e36d820a1350d45b7656a26f31f [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
Jiufei Xue814ce692016-03-15 14:53:20 -0700109 if (res->state & (DLM_LOCK_RES_RECOVERING|
110 DLM_LOCK_RES_RECOVERY_WAITING))
Wengang Wanga5248122010-07-30 16:14:44 +0800111 return 0;
112
Sunil Mushranff0a5222011-07-24 10:29:54 -0700113 /* Another node has this resource with this node as the master */
Wengang Wanga5248122010-07-30 16:14:44 +0800114 bit = find_next_bit(res->refmap, O2NM_MAX_NODES, 0);
115 if (bit < O2NM_MAX_NODES)
116 return 0;
117
Wengang Wanga5248122010-07-30 16:14:44 +0800118 return 1;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800119}
120
121
122/* Call whenever you may have added or deleted something from one of
123 * the lockres queue's. This will figure out whether it belongs on the
124 * unused list or not and does the appropriate thing. */
125void __dlm_lockres_calc_usage(struct dlm_ctxt *dlm,
126 struct dlm_lock_resource *res)
127{
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800128 assert_spin_locked(&dlm->spinlock);
129 assert_spin_locked(&res->spinlock);
130
131 if (__dlm_lockres_unused(res)){
132 if (list_empty(&res->purge)) {
Sunil Mushran8e17d162010-11-19 15:06:49 -0800133 mlog(0, "%s: Adding res %.*s to purge list\n",
134 dlm->name, res->lockname.len, res->lockname.name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800135
136 res->last_used = jiffies;
Kurt Hackelba2bf212006-12-01 14:47:20 -0800137 dlm_lockres_get(res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800138 list_add_tail(&res->purge, &dlm->purge_list);
139 dlm->purge_count++;
140 }
141 } else if (!list_empty(&res->purge)) {
Sunil Mushran8e17d162010-11-19 15:06:49 -0800142 mlog(0, "%s: Removing res %.*s from purge list\n",
143 dlm->name, res->lockname.len, res->lockname.name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800144
145 list_del_init(&res->purge);
Kurt Hackelba2bf212006-12-01 14:47:20 -0800146 dlm_lockres_put(res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800147 dlm->purge_count--;
148 }
149}
150
151void dlm_lockres_calc_usage(struct dlm_ctxt *dlm,
152 struct dlm_lock_resource *res)
153{
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800154 spin_lock(&dlm->spinlock);
155 spin_lock(&res->spinlock);
156
157 __dlm_lockres_calc_usage(dlm, res);
158
159 spin_unlock(&res->spinlock);
160 spin_unlock(&dlm->spinlock);
161}
162
piaojunee8f7fc2016-08-02 14:02:19 -0700163/*
164 * Do the real purge work:
165 * unhash the lockres, and
166 * clear flag DLM_LOCK_RES_DROPPING_REF.
167 * It requires dlm and lockres spinlock to be taken.
168 */
169void __dlm_do_purge_lockres(struct dlm_ctxt *dlm,
170 struct dlm_lock_resource *res)
171{
172 assert_spin_locked(&dlm->spinlock);
173 assert_spin_locked(&res->spinlock);
174
175 if (!list_empty(&res->purge)) {
176 mlog(0, "%s: Removing res %.*s from purgelist\n",
177 dlm->name, res->lockname.len, res->lockname.name);
178 list_del_init(&res->purge);
179 dlm_lockres_put(res);
180 dlm->purge_count--;
181 }
182
183 if (!__dlm_lockres_unused(res)) {
184 mlog(ML_ERROR, "%s: res %.*s in use after deref\n",
185 dlm->name, res->lockname.len, res->lockname.name);
186 __dlm_print_one_lock_resource(res);
187 BUG();
188 }
189
190 __dlm_unhash_lockres(dlm, res);
191
192 spin_lock(&dlm->track_lock);
193 if (!list_empty(&res->tracking))
194 list_del_init(&res->tracking);
195 else {
196 mlog(ML_ERROR, "%s: Resource %.*s not on the Tracking list\n",
197 dlm->name, res->lockname.len, res->lockname.name);
198 __dlm_print_one_lock_resource(res);
199 }
200 spin_unlock(&dlm->track_lock);
201
202 /*
203 * lockres is not in the hash now. drop the flag and wake up
204 * any processes waiting in dlm_get_lock_resource.
205 */
206 res->state &= ~DLM_LOCK_RES_DROPPING_REF;
207}
208
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700209static void dlm_purge_lockres(struct dlm_ctxt *dlm,
Adrian Bunkfaf0ec92006-12-14 00:17:32 +0100210 struct dlm_lock_resource *res)
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800211{
212 int master;
Kurt Hackelba2bf212006-12-01 14:47:20 -0800213 int ret = 0;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800214
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700215 assert_spin_locked(&dlm->spinlock);
216 assert_spin_locked(&res->spinlock);
Sunil Mushran516b7e52009-02-26 15:00:48 -0800217
Kurt Hackelba2bf212006-12-01 14:47:20 -0800218 master = (res->owner == dlm->node_num);
Sunil Mushran516b7e52009-02-26 15:00:48 -0800219
Sunil Mushran8e17d162010-11-19 15:06:49 -0800220 mlog(0, "%s: Purging res %.*s, master %d\n", dlm->name,
221 res->lockname.len, res->lockname.name, master);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800222
Kurt Hackelba2bf212006-12-01 14:47:20 -0800223 if (!master) {
piaojun309e9192016-08-02 14:02:16 -0700224 if (res->state & DLM_LOCK_RES_DROPPING_REF) {
piaojunee8f7fc2016-08-02 14:02:19 -0700225 mlog(ML_NOTICE, "%s: res %.*s already in DLM_LOCK_RES_DROPPING_REF state\n",
226 dlm->name, res->lockname.len, res->lockname.name);
piaojun309e9192016-08-02 14:02:16 -0700227 spin_unlock(&res->spinlock);
228 return;
229 }
230
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700231 res->state |= DLM_LOCK_RES_DROPPING_REF;
Sunil Mushranc824c3c2008-03-01 14:04:25 -0800232 /* drop spinlock... retake below */
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700233 spin_unlock(&res->spinlock);
Sunil Mushranc824c3c2008-03-01 14:04:25 -0800234 spin_unlock(&dlm->spinlock);
235
Kurt Hackel3b8118c2007-01-17 17:05:53 -0800236 spin_lock(&res->spinlock);
237 /* This ensures that clear refmap is sent after the set */
Sunil Mushran7dc102b2009-02-03 12:37:13 -0800238 __dlm_wait_on_lockres_flags(res, DLM_LOCK_RES_SETREF_INPROG);
Kurt Hackel3b8118c2007-01-17 17:05:53 -0800239 spin_unlock(&res->spinlock);
Sunil Mushranc824c3c2008-03-01 14:04:25 -0800240
Kurt Hackelba2bf212006-12-01 14:47:20 -0800241 /* clear our bit from the master's refmap, ignore errors */
242 ret = dlm_drop_lockres_ref(dlm, res);
243 if (ret < 0) {
Kurt Hackelba2bf212006-12-01 14:47:20 -0800244 if (!dlm_is_host_down(ret))
245 BUG();
246 }
Kurt Hackelba2bf212006-12-01 14:47:20 -0800247 spin_lock(&dlm->spinlock);
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700248 spin_lock(&res->spinlock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800249 }
250
Kurt Hackelba2bf212006-12-01 14:47:20 -0800251 if (!list_empty(&res->purge)) {
Sunil Mushran8e17d162010-11-19 15:06:49 -0800252 mlog(0, "%s: Removing res %.*s from purgelist, master %d\n",
253 dlm->name, res->lockname.len, res->lockname.name, master);
Kurt Hackelba2bf212006-12-01 14:47:20 -0800254 list_del_init(&res->purge);
255 dlm_lockres_put(res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800256 dlm->purge_count--;
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700257 }
258
piaojun309e9192016-08-02 14:02:16 -0700259 if (!master && ret == DLM_DEREF_RESPONSE_INPROG) {
260 mlog(0, "%s: deref %.*s in progress\n",
xuejiufei842b90b2016-03-15 14:53:11 -0700261 dlm->name, res->lockname.len, res->lockname.name);
262 spin_unlock(&res->spinlock);
263 return;
264 }
265
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700266 if (!__dlm_lockres_unused(res)) {
Sunil Mushran8e17d162010-11-19 15:06:49 -0800267 mlog(ML_ERROR, "%s: res %.*s in use after deref\n",
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700268 dlm->name, res->lockname.len, res->lockname.name);
269 __dlm_print_one_lock_resource(res);
270 BUG();
271 }
Wengang Wang83e32d92009-09-03 15:56:33 +0800272
Sunil Mushrane9f0b6a2011-07-24 10:27:54 -0700273 __dlm_unhash_lockres(dlm, res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800274
Yiwen Jiangf57a22d2015-09-04 15:44:37 -0700275 spin_lock(&dlm->track_lock);
276 if (!list_empty(&res->tracking))
277 list_del_init(&res->tracking);
278 else {
279 mlog(ML_ERROR, "Resource %.*s not on the Tracking list\n",
280 res->lockname.len, res->lockname.name);
281 __dlm_print_one_lock_resource(res);
282 }
283 spin_unlock(&dlm->track_lock);
284
Kurt Hackelba2bf212006-12-01 14:47:20 -0800285 /* lockres is not in the hash now. drop the flag and wake up
286 * any processes waiting in dlm_get_lock_resource. */
287 if (!master) {
Kurt Hackelba2bf212006-12-01 14:47:20 -0800288 res->state &= ~DLM_LOCK_RES_DROPPING_REF;
289 spin_unlock(&res->spinlock);
290 wake_up(&res->wq);
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700291 } else
292 spin_unlock(&res->spinlock);
Kurt Hackel8b219802006-05-01 11:16:45 -0700293}
294
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800295static void dlm_run_purge_list(struct dlm_ctxt *dlm,
296 int purge_now)
297{
298 unsigned int run_max, unused;
299 unsigned long purge_jiffies;
300 struct dlm_lock_resource *lockres;
301
302 spin_lock(&dlm->spinlock);
303 run_max = dlm->purge_count;
304
305 while(run_max && !list_empty(&dlm->purge_list)) {
306 run_max--;
307
308 lockres = list_entry(dlm->purge_list.next,
309 struct dlm_lock_resource, purge);
310
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800311 spin_lock(&lockres->spinlock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800312
313 purge_jiffies = lockres->last_used +
314 msecs_to_jiffies(DLM_PURGE_INTERVAL_MS);
315
316 /* Make sure that we want to be processing this guy at
317 * this time. */
318 if (!purge_now && time_after(purge_jiffies, jiffies)) {
319 /* Since resources are added to the purge list
320 * in tail order, we can stop at the first
321 * unpurgable resource -- anyone added after
322 * him will have a greater last_used value */
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700323 spin_unlock(&lockres->spinlock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800324 break;
325 }
326
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700327 /* Status of the lockres *might* change so double
328 * check. If the lockres is unused, holding the dlm
329 * spinlock will prevent people from getting and more
330 * refs on it. */
331 unused = __dlm_lockres_unused(lockres);
332 if (!unused ||
Xue jiufeiac4fef42014-06-23 13:22:09 -0700333 (lockres->state & DLM_LOCK_RES_MIGRATING) ||
334 (lockres->inflight_assert_workers != 0)) {
Sunil Mushran8e17d162010-11-19 15:06:49 -0800335 mlog(0, "%s: res %.*s is in use or being remastered, "
Xue jiufeiac4fef42014-06-23 13:22:09 -0700336 "used %d, state %d, assert master workers %u\n",
337 dlm->name, lockres->lockname.len,
338 lockres->lockname.name,
339 !unused, lockres->state,
340 lockres->inflight_assert_workers);
Xue jiufeia270c6d2014-06-23 13:22:08 -0700341 list_move_tail(&lockres->purge, &dlm->purge_list);
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700342 spin_unlock(&lockres->spinlock);
343 continue;
344 }
345
Sunil Mushran78062cb2007-03-22 17:01:07 -0700346 dlm_lockres_get(lockres);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800347
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700348 dlm_purge_lockres(dlm, lockres);
Sunil Mushran78062cb2007-03-22 17:01:07 -0700349
Sunil Mushran3fca0892007-03-12 13:24:34 -0700350 dlm_lockres_put(lockres);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800351
352 /* Avoid adding any scheduling latencies */
353 cond_resched_lock(&dlm->spinlock);
354 }
355
356 spin_unlock(&dlm->spinlock);
357}
358
359static void dlm_shuffle_lists(struct dlm_ctxt *dlm,
360 struct dlm_lock_resource *res)
361{
362 struct dlm_lock *lock, *target;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800363 int can_grant = 1;
364
Sunil Mushran8e17d162010-11-19 15:06:49 -0800365 /*
366 * Because this function is called with the lockres
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800367 * spinlock, and because we know that it is not migrating/
368 * recovering/in-progress, it is fine to reserve asts and
Sunil Mushran8e17d162010-11-19 15:06:49 -0800369 * basts right before queueing them all throughout
370 */
Wengang Wangd9ef7522010-05-17 20:20:44 +0800371 assert_spin_locked(&dlm->ast_lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800372 assert_spin_locked(&res->spinlock);
373 BUG_ON((res->state & (DLM_LOCK_RES_MIGRATING|
374 DLM_LOCK_RES_RECOVERING|
375 DLM_LOCK_RES_IN_PROGRESS)));
376
377converting:
378 if (list_empty(&res->converting))
379 goto blocked;
Sunil Mushran8e17d162010-11-19 15:06:49 -0800380 mlog(0, "%s: res %.*s has locks on the convert queue\n", dlm->name,
381 res->lockname.len, res->lockname.name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800382
383 target = list_entry(res->converting.next, struct dlm_lock, list);
384 if (target->ml.convert_type == LKM_IVMODE) {
Sunil Mushran8e17d162010-11-19 15:06:49 -0800385 mlog(ML_ERROR, "%s: res %.*s converting lock to invalid mode\n",
386 dlm->name, res->lockname.len, res->lockname.name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800387 BUG();
388 }
Dong Fangdf53cd32013-09-11 14:19:50 -0700389 list_for_each_entry(lock, &res->granted, list) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800390 if (lock==target)
391 continue;
392 if (!dlm_lock_compatible(lock->ml.type,
393 target->ml.convert_type)) {
394 can_grant = 0;
395 /* queue the BAST if not already */
396 if (lock->ml.highest_blocked == LKM_IVMODE) {
397 __dlm_lockres_reserve_ast(res);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800398 __dlm_queue_bast(dlm, lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800399 }
400 /* update the highest_blocked if needed */
401 if (lock->ml.highest_blocked < target->ml.convert_type)
402 lock->ml.highest_blocked =
403 target->ml.convert_type;
404 }
405 }
Dong Fangdf53cd32013-09-11 14:19:50 -0700406
407 list_for_each_entry(lock, &res->converting, list) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800408 if (lock==target)
409 continue;
410 if (!dlm_lock_compatible(lock->ml.type,
411 target->ml.convert_type)) {
412 can_grant = 0;
413 if (lock->ml.highest_blocked == LKM_IVMODE) {
414 __dlm_lockres_reserve_ast(res);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800415 __dlm_queue_bast(dlm, lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800416 }
417 if (lock->ml.highest_blocked < target->ml.convert_type)
418 lock->ml.highest_blocked =
419 target->ml.convert_type;
420 }
421 }
422
423 /* we can convert the lock */
424 if (can_grant) {
425 spin_lock(&target->spinlock);
426 BUG_ON(target->ml.highest_blocked != LKM_IVMODE);
427
Sunil Mushran8e17d162010-11-19 15:06:49 -0800428 mlog(0, "%s: res %.*s, AST for Converting lock %u:%llu, type "
429 "%d => %d, node %u\n", dlm->name, res->lockname.len,
430 res->lockname.name,
431 dlm_get_lock_cookie_node(be64_to_cpu(target->ml.cookie)),
432 dlm_get_lock_cookie_seq(be64_to_cpu(target->ml.cookie)),
433 target->ml.type,
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800434 target->ml.convert_type, target->ml.node);
435
436 target->ml.type = target->ml.convert_type;
437 target->ml.convert_type = LKM_IVMODE;
Akinobu Mitaf1166292006-06-26 00:24:46 -0700438 list_move_tail(&target->list, &res->granted);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800439
440 BUG_ON(!target->lksb);
441 target->lksb->status = DLM_NORMAL;
442
443 spin_unlock(&target->spinlock);
444
445 __dlm_lockres_reserve_ast(res);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800446 __dlm_queue_ast(dlm, target);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800447 /* go back and check for more */
448 goto converting;
449 }
450
451blocked:
452 if (list_empty(&res->blocked))
453 goto leave;
454 target = list_entry(res->blocked.next, struct dlm_lock, list);
455
Dong Fangdf53cd32013-09-11 14:19:50 -0700456 list_for_each_entry(lock, &res->granted, list) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800457 if (lock==target)
458 continue;
459 if (!dlm_lock_compatible(lock->ml.type, target->ml.type)) {
460 can_grant = 0;
461 if (lock->ml.highest_blocked == LKM_IVMODE) {
462 __dlm_lockres_reserve_ast(res);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800463 __dlm_queue_bast(dlm, lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800464 }
465 if (lock->ml.highest_blocked < target->ml.type)
466 lock->ml.highest_blocked = target->ml.type;
467 }
468 }
469
Dong Fangdf53cd32013-09-11 14:19:50 -0700470 list_for_each_entry(lock, &res->converting, list) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800471 if (lock==target)
472 continue;
473 if (!dlm_lock_compatible(lock->ml.type, target->ml.type)) {
474 can_grant = 0;
475 if (lock->ml.highest_blocked == LKM_IVMODE) {
476 __dlm_lockres_reserve_ast(res);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800477 __dlm_queue_bast(dlm, lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800478 }
479 if (lock->ml.highest_blocked < target->ml.type)
480 lock->ml.highest_blocked = target->ml.type;
481 }
482 }
483
484 /* we can grant the blocked lock (only
485 * possible if converting list empty) */
486 if (can_grant) {
487 spin_lock(&target->spinlock);
488 BUG_ON(target->ml.highest_blocked != LKM_IVMODE);
489
Sunil Mushran8e17d162010-11-19 15:06:49 -0800490 mlog(0, "%s: res %.*s, AST for Blocked lock %u:%llu, type %d, "
491 "node %u\n", dlm->name, res->lockname.len,
492 res->lockname.name,
493 dlm_get_lock_cookie_node(be64_to_cpu(target->ml.cookie)),
494 dlm_get_lock_cookie_seq(be64_to_cpu(target->ml.cookie)),
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800495 target->ml.type, target->ml.node);
496
Sunil Mushran8e17d162010-11-19 15:06:49 -0800497 /* target->ml.type is already correct */
Akinobu Mitaf1166292006-06-26 00:24:46 -0700498 list_move_tail(&target->list, &res->granted);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800499
500 BUG_ON(!target->lksb);
501 target->lksb->status = DLM_NORMAL;
502
503 spin_unlock(&target->spinlock);
504
505 __dlm_lockres_reserve_ast(res);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800506 __dlm_queue_ast(dlm, target);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800507 /* go back and check for more */
508 goto converting;
509 }
510
511leave:
512 return;
513}
514
515/* must have NO locks when calling this with res !=NULL * */
516void dlm_kick_thread(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
517{
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800518 if (res) {
519 spin_lock(&dlm->spinlock);
520 spin_lock(&res->spinlock);
521 __dlm_dirty_lockres(dlm, res);
522 spin_unlock(&res->spinlock);
523 spin_unlock(&dlm->spinlock);
524 }
525 wake_up(&dlm->dlm_thread_wq);
526}
527
528void __dlm_dirty_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
529{
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800530 assert_spin_locked(&dlm->spinlock);
531 assert_spin_locked(&res->spinlock);
532
533 /* don't shuffle secondary queues */
Kurt Hackelddc09c82007-01-05 15:00:17 -0800534 if ((res->owner == dlm->node_num)) {
535 if (res->state & (DLM_LOCK_RES_MIGRATING |
536 DLM_LOCK_RES_BLOCK_DIRTY))
537 return;
538
539 if (list_empty(&res->dirty)) {
540 /* ref for dirty_list */
541 dlm_lockres_get(res);
542 list_add_tail(&res->dirty, &dlm->dirty_list);
543 res->state |= DLM_LOCK_RES_DIRTY;
544 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800545 }
Sunil Mushran8e17d162010-11-19 15:06:49 -0800546
547 mlog(0, "%s: res %.*s\n", dlm->name, res->lockname.len,
548 res->lockname.name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800549}
550
551
552/* Launch the NM thread for the mounted volume */
553int dlm_launch_thread(struct dlm_ctxt *dlm)
554{
Sunil Mushran8e17d162010-11-19 15:06:49 -0800555 mlog(0, "Starting dlm_thread...\n");
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800556
Joseph Qi5afc44e2015-11-05 18:44:13 -0800557 dlm->dlm_thread_task = kthread_run(dlm_thread, dlm, "dlm-%s",
558 dlm->name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800559 if (IS_ERR(dlm->dlm_thread_task)) {
560 mlog_errno(PTR_ERR(dlm->dlm_thread_task));
561 dlm->dlm_thread_task = NULL;
562 return -EINVAL;
563 }
564
565 return 0;
566}
567
568void dlm_complete_thread(struct dlm_ctxt *dlm)
569{
570 if (dlm->dlm_thread_task) {
Sunil Mushran8e17d162010-11-19 15:06:49 -0800571 mlog(ML_KTHREAD, "Waiting for dlm thread to exit\n");
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800572 kthread_stop(dlm->dlm_thread_task);
573 dlm->dlm_thread_task = NULL;
574 }
575}
576
577static int dlm_dirty_list_empty(struct dlm_ctxt *dlm)
578{
579 int empty;
580
581 spin_lock(&dlm->spinlock);
582 empty = list_empty(&dlm->dirty_list);
583 spin_unlock(&dlm->spinlock);
584
585 return empty;
586}
587
588static void dlm_flush_asts(struct dlm_ctxt *dlm)
589{
590 int ret;
591 struct dlm_lock *lock;
592 struct dlm_lock_resource *res;
593 u8 hi;
594
595 spin_lock(&dlm->ast_lock);
596 while (!list_empty(&dlm->pending_asts)) {
597 lock = list_entry(dlm->pending_asts.next,
598 struct dlm_lock, ast_list);
599 /* get an extra ref on lock */
600 dlm_lock_get(lock);
601 res = lock->lockres;
Sunil Mushran8e17d162010-11-19 15:06:49 -0800602 mlog(0, "%s: res %.*s, Flush AST for lock %u:%llu, type %d, "
603 "node %u\n", dlm->name, res->lockname.len,
604 res->lockname.name,
605 dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
606 dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)),
607 lock->ml.type, lock->ml.node);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800608
609 BUG_ON(!lock->ast_pending);
610
611 /* remove from list (including ref) */
612 list_del_init(&lock->ast_list);
613 dlm_lock_put(lock);
614 spin_unlock(&dlm->ast_lock);
615
616 if (lock->ml.node != dlm->node_num) {
617 ret = dlm_do_remote_ast(dlm, res, lock);
618 if (ret < 0)
619 mlog_errno(ret);
620 } else
621 dlm_do_local_ast(dlm, res, lock);
622
623 spin_lock(&dlm->ast_lock);
624
625 /* possible that another ast was queued while
626 * we were delivering the last one */
627 if (!list_empty(&lock->ast_list)) {
Sunil Mushran8e17d162010-11-19 15:06:49 -0800628 mlog(0, "%s: res %.*s, AST queued while flushing last "
629 "one\n", dlm->name, res->lockname.len,
630 res->lockname.name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800631 } else
632 lock->ast_pending = 0;
633
634 /* drop the extra ref.
635 * this may drop it completely. */
636 dlm_lock_put(lock);
637 dlm_lockres_release_ast(dlm, res);
638 }
639
640 while (!list_empty(&dlm->pending_basts)) {
641 lock = list_entry(dlm->pending_basts.next,
642 struct dlm_lock, bast_list);
643 /* get an extra ref on lock */
644 dlm_lock_get(lock);
645 res = lock->lockres;
646
647 BUG_ON(!lock->bast_pending);
648
649 /* get the highest blocked lock, and reset */
650 spin_lock(&lock->spinlock);
651 BUG_ON(lock->ml.highest_blocked <= LKM_IVMODE);
652 hi = lock->ml.highest_blocked;
653 lock->ml.highest_blocked = LKM_IVMODE;
654 spin_unlock(&lock->spinlock);
655
656 /* remove from list (including ref) */
657 list_del_init(&lock->bast_list);
658 dlm_lock_put(lock);
659 spin_unlock(&dlm->ast_lock);
660
Sunil Mushran8e17d162010-11-19 15:06:49 -0800661 mlog(0, "%s: res %.*s, Flush BAST for lock %u:%llu, "
662 "blocked %d, node %u\n",
663 dlm->name, res->lockname.len, res->lockname.name,
664 dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
665 dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)),
666 hi, lock->ml.node);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800667
668 if (lock->ml.node != dlm->node_num) {
669 ret = dlm_send_proxy_bast(dlm, res, lock, hi);
670 if (ret < 0)
671 mlog_errno(ret);
672 } else
673 dlm_do_local_bast(dlm, res, lock, hi);
674
675 spin_lock(&dlm->ast_lock);
676
677 /* possible that another bast was queued while
678 * we were delivering the last one */
679 if (!list_empty(&lock->bast_list)) {
Sunil Mushran8e17d162010-11-19 15:06:49 -0800680 mlog(0, "%s: res %.*s, BAST queued while flushing last "
681 "one\n", dlm->name, res->lockname.len,
682 res->lockname.name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800683 } else
684 lock->bast_pending = 0;
685
686 /* drop the extra ref.
687 * this may drop it completely. */
688 dlm_lock_put(lock);
689 dlm_lockres_release_ast(dlm, res);
690 }
691 wake_up(&dlm->ast_wq);
692 spin_unlock(&dlm->ast_lock);
693}
694
695
696#define DLM_THREAD_TIMEOUT_MS (4 * 1000)
697#define DLM_THREAD_MAX_DIRTY 100
698#define DLM_THREAD_MAX_ASTS 10
699
700static int dlm_thread(void *data)
701{
702 struct dlm_lock_resource *res;
703 struct dlm_ctxt *dlm = data;
704 unsigned long timeout = msecs_to_jiffies(DLM_THREAD_TIMEOUT_MS);
705
706 mlog(0, "dlm thread running for %s...\n", dlm->name);
707
708 while (!kthread_should_stop()) {
709 int n = DLM_THREAD_MAX_DIRTY;
710
711 /* dlm_shutting_down is very point-in-time, but that
712 * doesn't matter as we'll just loop back around if we
713 * get false on the leading edge of a state
714 * transition. */
715 dlm_run_purge_list(dlm, dlm_shutting_down(dlm));
716
717 /* We really don't want to hold dlm->spinlock while
718 * calling dlm_shuffle_lists on each lockres that
719 * needs to have its queues adjusted and AST/BASTs
720 * run. So let's pull each entry off the dirty_list
721 * and drop dlm->spinlock ASAP. Once off the list,
722 * res->spinlock needs to be taken again to protect
723 * the queues while calling dlm_shuffle_lists. */
724 spin_lock(&dlm->spinlock);
725 while (!list_empty(&dlm->dirty_list)) {
726 int delay = 0;
727 res = list_entry(dlm->dirty_list.next,
728 struct dlm_lock_resource, dirty);
729
730 /* peel a lockres off, remove it from the list,
731 * unset the dirty flag and drop the dlm lock */
732 BUG_ON(!res);
733 dlm_lockres_get(res);
734
735 spin_lock(&res->spinlock);
Kurt Hackelddc09c82007-01-05 15:00:17 -0800736 /* We clear the DLM_LOCK_RES_DIRTY state once we shuffle lists below */
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800737 list_del_init(&res->dirty);
738 spin_unlock(&res->spinlock);
739 spin_unlock(&dlm->spinlock);
Kurt Hackel6ff06a92006-05-01 11:51:45 -0700740 /* Drop dirty_list ref */
741 dlm_lockres_put(res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800742
743 /* lockres can be re-dirtied/re-added to the
744 * dirty_list in this gap, but that is ok */
745
Wengang Wangd9ef7522010-05-17 20:20:44 +0800746 spin_lock(&dlm->ast_lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800747 spin_lock(&res->spinlock);
748 if (res->owner != dlm->node_num) {
749 __dlm_print_one_lock_resource(res);
Sunil Mushran8e17d162010-11-19 15:06:49 -0800750 mlog(ML_ERROR, "%s: inprog %d, mig %d, reco %d,"
751 " dirty %d\n", dlm->name,
752 !!(res->state & DLM_LOCK_RES_IN_PROGRESS),
753 !!(res->state & DLM_LOCK_RES_MIGRATING),
754 !!(res->state & DLM_LOCK_RES_RECOVERING),
755 !!(res->state & DLM_LOCK_RES_DIRTY));
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800756 }
757 BUG_ON(res->owner != dlm->node_num);
758
759 /* it is now ok to move lockreses in these states
760 * to the dirty list, assuming that they will only be
761 * dirty for a short while. */
Kurt Hackelddc09c82007-01-05 15:00:17 -0800762 BUG_ON(res->state & DLM_LOCK_RES_MIGRATING);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800763 if (res->state & (DLM_LOCK_RES_IN_PROGRESS |
Jiufei Xue814ce692016-03-15 14:53:20 -0700764 DLM_LOCK_RES_RECOVERING |
765 DLM_LOCK_RES_RECOVERY_WAITING)) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800766 /* move it to the tail and keep going */
Kurt Hackelddc09c82007-01-05 15:00:17 -0800767 res->state &= ~DLM_LOCK_RES_DIRTY;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800768 spin_unlock(&res->spinlock);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800769 spin_unlock(&dlm->ast_lock);
Sunil Mushran8e17d162010-11-19 15:06:49 -0800770 mlog(0, "%s: res %.*s, inprogress, delay list "
771 "shuffle, state %d\n", dlm->name,
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800772 res->lockname.len, res->lockname.name,
773 res->state);
774 delay = 1;
775 goto in_progress;
776 }
777
778 /* at this point the lockres is not migrating/
779 * recovering/in-progress. we have the lockres
780 * spinlock and do NOT have the dlm lock.
781 * safe to reserve/queue asts and run the lists. */
782
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800783 /* called while holding lockres lock */
784 dlm_shuffle_lists(dlm, res);
Kurt Hackelddc09c82007-01-05 15:00:17 -0800785 res->state &= ~DLM_LOCK_RES_DIRTY;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800786 spin_unlock(&res->spinlock);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800787 spin_unlock(&dlm->ast_lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800788
789 dlm_lockres_calc_usage(dlm, res);
790
791in_progress:
792
793 spin_lock(&dlm->spinlock);
794 /* if the lock was in-progress, stick
795 * it on the back of the list */
796 if (delay) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800797 spin_lock(&res->spinlock);
Kurt Hackelddc09c82007-01-05 15:00:17 -0800798 __dlm_dirty_lockres(dlm, res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800799 spin_unlock(&res->spinlock);
800 }
801 dlm_lockres_put(res);
802
803 /* unlikely, but we may need to give time to
804 * other tasks */
805 if (!--n) {
Sunil Mushran8e17d162010-11-19 15:06:49 -0800806 mlog(0, "%s: Throttling dlm thread\n",
807 dlm->name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800808 break;
809 }
810 }
811
812 spin_unlock(&dlm->spinlock);
813 dlm_flush_asts(dlm);
814
815 /* yield and continue right away if there is more work to do */
816 if (!n) {
Kurt Hackelf85cd472006-05-01 14:27:41 -0700817 cond_resched();
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800818 continue;
819 }
820
821 wait_event_interruptible_timeout(dlm->dlm_thread_wq,
822 !dlm_dirty_list_empty(dlm) ||
823 kthread_should_stop(),
824 timeout);
825 }
826
827 mlog(0, "quitting DLM thread\n");
828 return 0;
829}