blob: ce397229acc04e28bad738d69bf143b42ca5f59d [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
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700163static void dlm_purge_lockres(struct dlm_ctxt *dlm,
Adrian Bunkfaf0ec92006-12-14 00:17:32 +0100164 struct dlm_lock_resource *res)
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800165{
166 int master;
Kurt Hackelba2bf212006-12-01 14:47:20 -0800167 int ret = 0;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800168
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700169 assert_spin_locked(&dlm->spinlock);
170 assert_spin_locked(&res->spinlock);
Sunil Mushran516b7e52009-02-26 15:00:48 -0800171
Kurt Hackelba2bf212006-12-01 14:47:20 -0800172 master = (res->owner == dlm->node_num);
Sunil Mushran516b7e52009-02-26 15:00:48 -0800173
Sunil Mushran8e17d162010-11-19 15:06:49 -0800174 mlog(0, "%s: Purging res %.*s, master %d\n", dlm->name,
175 res->lockname.len, res->lockname.name, master);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800176
Kurt Hackelba2bf212006-12-01 14:47:20 -0800177 if (!master) {
piaojun309e9192016-08-02 14:02:16 -0700178 if (res->state & DLM_LOCK_RES_DROPPING_REF) {
179 mlog(ML_NOTICE, "%s: res %.*s already in "
180 "DLM_LOCK_RES_DROPPING_REF state\n",
181 dlm->name, res->lockname.len,
182 res->lockname.name);
183 spin_unlock(&res->spinlock);
184 return;
185 }
186
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700187 res->state |= DLM_LOCK_RES_DROPPING_REF;
Sunil Mushranc824c3c2008-03-01 14:04:25 -0800188 /* drop spinlock... retake below */
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700189 spin_unlock(&res->spinlock);
Sunil Mushranc824c3c2008-03-01 14:04:25 -0800190 spin_unlock(&dlm->spinlock);
191
Kurt Hackel3b8118c2007-01-17 17:05:53 -0800192 spin_lock(&res->spinlock);
193 /* This ensures that clear refmap is sent after the set */
Sunil Mushran7dc102b2009-02-03 12:37:13 -0800194 __dlm_wait_on_lockres_flags(res, DLM_LOCK_RES_SETREF_INPROG);
Kurt Hackel3b8118c2007-01-17 17:05:53 -0800195 spin_unlock(&res->spinlock);
Sunil Mushranc824c3c2008-03-01 14:04:25 -0800196
Kurt Hackelba2bf212006-12-01 14:47:20 -0800197 /* clear our bit from the master's refmap, ignore errors */
198 ret = dlm_drop_lockres_ref(dlm, res);
199 if (ret < 0) {
Kurt Hackelba2bf212006-12-01 14:47:20 -0800200 if (!dlm_is_host_down(ret))
201 BUG();
202 }
Kurt Hackelba2bf212006-12-01 14:47:20 -0800203 spin_lock(&dlm->spinlock);
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700204 spin_lock(&res->spinlock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800205 }
206
Kurt Hackelba2bf212006-12-01 14:47:20 -0800207 if (!list_empty(&res->purge)) {
Sunil Mushran8e17d162010-11-19 15:06:49 -0800208 mlog(0, "%s: Removing res %.*s from purgelist, master %d\n",
209 dlm->name, res->lockname.len, res->lockname.name, master);
Kurt Hackelba2bf212006-12-01 14:47:20 -0800210 list_del_init(&res->purge);
211 dlm_lockres_put(res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800212 dlm->purge_count--;
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700213 }
214
piaojun309e9192016-08-02 14:02:16 -0700215 if (!master && ret == DLM_DEREF_RESPONSE_INPROG) {
216 mlog(0, "%s: deref %.*s in progress\n",
xuejiufei842b90b2016-03-15 14:53:11 -0700217 dlm->name, res->lockname.len, res->lockname.name);
218 spin_unlock(&res->spinlock);
219 return;
220 }
221
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700222 if (!__dlm_lockres_unused(res)) {
Sunil Mushran8e17d162010-11-19 15:06:49 -0800223 mlog(ML_ERROR, "%s: res %.*s in use after deref\n",
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700224 dlm->name, res->lockname.len, res->lockname.name);
225 __dlm_print_one_lock_resource(res);
226 BUG();
227 }
Wengang Wang83e32d92009-09-03 15:56:33 +0800228
Sunil Mushrane9f0b6a2011-07-24 10:27:54 -0700229 __dlm_unhash_lockres(dlm, res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800230
Yiwen Jiangf57a22d2015-09-04 15:44:37 -0700231 spin_lock(&dlm->track_lock);
232 if (!list_empty(&res->tracking))
233 list_del_init(&res->tracking);
234 else {
235 mlog(ML_ERROR, "Resource %.*s not on the Tracking list\n",
236 res->lockname.len, res->lockname.name);
237 __dlm_print_one_lock_resource(res);
238 }
239 spin_unlock(&dlm->track_lock);
240
Kurt Hackelba2bf212006-12-01 14:47:20 -0800241 /* lockres is not in the hash now. drop the flag and wake up
242 * any processes waiting in dlm_get_lock_resource. */
243 if (!master) {
Kurt Hackelba2bf212006-12-01 14:47:20 -0800244 res->state &= ~DLM_LOCK_RES_DROPPING_REF;
245 spin_unlock(&res->spinlock);
246 wake_up(&res->wq);
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700247 } else
248 spin_unlock(&res->spinlock);
Kurt Hackel8b219802006-05-01 11:16:45 -0700249}
250
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800251static void dlm_run_purge_list(struct dlm_ctxt *dlm,
252 int purge_now)
253{
254 unsigned int run_max, unused;
255 unsigned long purge_jiffies;
256 struct dlm_lock_resource *lockres;
257
258 spin_lock(&dlm->spinlock);
259 run_max = dlm->purge_count;
260
261 while(run_max && !list_empty(&dlm->purge_list)) {
262 run_max--;
263
264 lockres = list_entry(dlm->purge_list.next,
265 struct dlm_lock_resource, purge);
266
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800267 spin_lock(&lockres->spinlock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800268
269 purge_jiffies = lockres->last_used +
270 msecs_to_jiffies(DLM_PURGE_INTERVAL_MS);
271
272 /* Make sure that we want to be processing this guy at
273 * this time. */
274 if (!purge_now && time_after(purge_jiffies, jiffies)) {
275 /* Since resources are added to the purge list
276 * in tail order, we can stop at the first
277 * unpurgable resource -- anyone added after
278 * him will have a greater last_used value */
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700279 spin_unlock(&lockres->spinlock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800280 break;
281 }
282
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700283 /* Status of the lockres *might* change so double
284 * check. If the lockres is unused, holding the dlm
285 * spinlock will prevent people from getting and more
286 * refs on it. */
287 unused = __dlm_lockres_unused(lockres);
288 if (!unused ||
Xue jiufeiac4fef42014-06-23 13:22:09 -0700289 (lockres->state & DLM_LOCK_RES_MIGRATING) ||
290 (lockres->inflight_assert_workers != 0)) {
Sunil Mushran8e17d162010-11-19 15:06:49 -0800291 mlog(0, "%s: res %.*s is in use or being remastered, "
Xue jiufeiac4fef42014-06-23 13:22:09 -0700292 "used %d, state %d, assert master workers %u\n",
293 dlm->name, lockres->lockname.len,
294 lockres->lockname.name,
295 !unused, lockres->state,
296 lockres->inflight_assert_workers);
Xue jiufeia270c6d2014-06-23 13:22:08 -0700297 list_move_tail(&lockres->purge, &dlm->purge_list);
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700298 spin_unlock(&lockres->spinlock);
299 continue;
300 }
301
Sunil Mushran78062cb2007-03-22 17:01:07 -0700302 dlm_lockres_get(lockres);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800303
Srinivas Eeda7beaf242010-07-19 16:04:12 -0700304 dlm_purge_lockres(dlm, lockres);
Sunil Mushran78062cb2007-03-22 17:01:07 -0700305
Sunil Mushran3fca0892007-03-12 13:24:34 -0700306 dlm_lockres_put(lockres);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800307
308 /* Avoid adding any scheduling latencies */
309 cond_resched_lock(&dlm->spinlock);
310 }
311
312 spin_unlock(&dlm->spinlock);
313}
314
315static void dlm_shuffle_lists(struct dlm_ctxt *dlm,
316 struct dlm_lock_resource *res)
317{
318 struct dlm_lock *lock, *target;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800319 int can_grant = 1;
320
Sunil Mushran8e17d162010-11-19 15:06:49 -0800321 /*
322 * Because this function is called with the lockres
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800323 * spinlock, and because we know that it is not migrating/
324 * recovering/in-progress, it is fine to reserve asts and
Sunil Mushran8e17d162010-11-19 15:06:49 -0800325 * basts right before queueing them all throughout
326 */
Wengang Wangd9ef7522010-05-17 20:20:44 +0800327 assert_spin_locked(&dlm->ast_lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800328 assert_spin_locked(&res->spinlock);
329 BUG_ON((res->state & (DLM_LOCK_RES_MIGRATING|
330 DLM_LOCK_RES_RECOVERING|
331 DLM_LOCK_RES_IN_PROGRESS)));
332
333converting:
334 if (list_empty(&res->converting))
335 goto blocked;
Sunil Mushran8e17d162010-11-19 15:06:49 -0800336 mlog(0, "%s: res %.*s has locks on the convert queue\n", dlm->name,
337 res->lockname.len, res->lockname.name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800338
339 target = list_entry(res->converting.next, struct dlm_lock, list);
340 if (target->ml.convert_type == LKM_IVMODE) {
Sunil Mushran8e17d162010-11-19 15:06:49 -0800341 mlog(ML_ERROR, "%s: res %.*s converting lock to invalid mode\n",
342 dlm->name, res->lockname.len, res->lockname.name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800343 BUG();
344 }
Dong Fangdf53cd32013-09-11 14:19:50 -0700345 list_for_each_entry(lock, &res->granted, list) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800346 if (lock==target)
347 continue;
348 if (!dlm_lock_compatible(lock->ml.type,
349 target->ml.convert_type)) {
350 can_grant = 0;
351 /* queue the BAST if not already */
352 if (lock->ml.highest_blocked == LKM_IVMODE) {
353 __dlm_lockres_reserve_ast(res);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800354 __dlm_queue_bast(dlm, lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800355 }
356 /* update the highest_blocked if needed */
357 if (lock->ml.highest_blocked < target->ml.convert_type)
358 lock->ml.highest_blocked =
359 target->ml.convert_type;
360 }
361 }
Dong Fangdf53cd32013-09-11 14:19:50 -0700362
363 list_for_each_entry(lock, &res->converting, list) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800364 if (lock==target)
365 continue;
366 if (!dlm_lock_compatible(lock->ml.type,
367 target->ml.convert_type)) {
368 can_grant = 0;
369 if (lock->ml.highest_blocked == LKM_IVMODE) {
370 __dlm_lockres_reserve_ast(res);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800371 __dlm_queue_bast(dlm, lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800372 }
373 if (lock->ml.highest_blocked < target->ml.convert_type)
374 lock->ml.highest_blocked =
375 target->ml.convert_type;
376 }
377 }
378
379 /* we can convert the lock */
380 if (can_grant) {
381 spin_lock(&target->spinlock);
382 BUG_ON(target->ml.highest_blocked != LKM_IVMODE);
383
Sunil Mushran8e17d162010-11-19 15:06:49 -0800384 mlog(0, "%s: res %.*s, AST for Converting lock %u:%llu, type "
385 "%d => %d, node %u\n", dlm->name, res->lockname.len,
386 res->lockname.name,
387 dlm_get_lock_cookie_node(be64_to_cpu(target->ml.cookie)),
388 dlm_get_lock_cookie_seq(be64_to_cpu(target->ml.cookie)),
389 target->ml.type,
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800390 target->ml.convert_type, target->ml.node);
391
392 target->ml.type = target->ml.convert_type;
393 target->ml.convert_type = LKM_IVMODE;
Akinobu Mitaf1166292006-06-26 00:24:46 -0700394 list_move_tail(&target->list, &res->granted);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800395
396 BUG_ON(!target->lksb);
397 target->lksb->status = DLM_NORMAL;
398
399 spin_unlock(&target->spinlock);
400
401 __dlm_lockres_reserve_ast(res);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800402 __dlm_queue_ast(dlm, target);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800403 /* go back and check for more */
404 goto converting;
405 }
406
407blocked:
408 if (list_empty(&res->blocked))
409 goto leave;
410 target = list_entry(res->blocked.next, struct dlm_lock, list);
411
Dong Fangdf53cd32013-09-11 14:19:50 -0700412 list_for_each_entry(lock, &res->granted, list) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800413 if (lock==target)
414 continue;
415 if (!dlm_lock_compatible(lock->ml.type, target->ml.type)) {
416 can_grant = 0;
417 if (lock->ml.highest_blocked == LKM_IVMODE) {
418 __dlm_lockres_reserve_ast(res);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800419 __dlm_queue_bast(dlm, lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800420 }
421 if (lock->ml.highest_blocked < target->ml.type)
422 lock->ml.highest_blocked = target->ml.type;
423 }
424 }
425
Dong Fangdf53cd32013-09-11 14:19:50 -0700426 list_for_each_entry(lock, &res->converting, list) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800427 if (lock==target)
428 continue;
429 if (!dlm_lock_compatible(lock->ml.type, target->ml.type)) {
430 can_grant = 0;
431 if (lock->ml.highest_blocked == LKM_IVMODE) {
432 __dlm_lockres_reserve_ast(res);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800433 __dlm_queue_bast(dlm, lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800434 }
435 if (lock->ml.highest_blocked < target->ml.type)
436 lock->ml.highest_blocked = target->ml.type;
437 }
438 }
439
440 /* we can grant the blocked lock (only
441 * possible if converting list empty) */
442 if (can_grant) {
443 spin_lock(&target->spinlock);
444 BUG_ON(target->ml.highest_blocked != LKM_IVMODE);
445
Sunil Mushran8e17d162010-11-19 15:06:49 -0800446 mlog(0, "%s: res %.*s, AST for Blocked lock %u:%llu, type %d, "
447 "node %u\n", dlm->name, res->lockname.len,
448 res->lockname.name,
449 dlm_get_lock_cookie_node(be64_to_cpu(target->ml.cookie)),
450 dlm_get_lock_cookie_seq(be64_to_cpu(target->ml.cookie)),
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800451 target->ml.type, target->ml.node);
452
Sunil Mushran8e17d162010-11-19 15:06:49 -0800453 /* target->ml.type is already correct */
Akinobu Mitaf1166292006-06-26 00:24:46 -0700454 list_move_tail(&target->list, &res->granted);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800455
456 BUG_ON(!target->lksb);
457 target->lksb->status = DLM_NORMAL;
458
459 spin_unlock(&target->spinlock);
460
461 __dlm_lockres_reserve_ast(res);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800462 __dlm_queue_ast(dlm, target);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800463 /* go back and check for more */
464 goto converting;
465 }
466
467leave:
468 return;
469}
470
471/* must have NO locks when calling this with res !=NULL * */
472void dlm_kick_thread(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
473{
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800474 if (res) {
475 spin_lock(&dlm->spinlock);
476 spin_lock(&res->spinlock);
477 __dlm_dirty_lockres(dlm, res);
478 spin_unlock(&res->spinlock);
479 spin_unlock(&dlm->spinlock);
480 }
481 wake_up(&dlm->dlm_thread_wq);
482}
483
484void __dlm_dirty_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
485{
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800486 assert_spin_locked(&dlm->spinlock);
487 assert_spin_locked(&res->spinlock);
488
489 /* don't shuffle secondary queues */
Kurt Hackelddc09c82007-01-05 15:00:17 -0800490 if ((res->owner == dlm->node_num)) {
491 if (res->state & (DLM_LOCK_RES_MIGRATING |
492 DLM_LOCK_RES_BLOCK_DIRTY))
493 return;
494
495 if (list_empty(&res->dirty)) {
496 /* ref for dirty_list */
497 dlm_lockres_get(res);
498 list_add_tail(&res->dirty, &dlm->dirty_list);
499 res->state |= DLM_LOCK_RES_DIRTY;
500 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800501 }
Sunil Mushran8e17d162010-11-19 15:06:49 -0800502
503 mlog(0, "%s: res %.*s\n", dlm->name, res->lockname.len,
504 res->lockname.name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800505}
506
507
508/* Launch the NM thread for the mounted volume */
509int dlm_launch_thread(struct dlm_ctxt *dlm)
510{
Sunil Mushran8e17d162010-11-19 15:06:49 -0800511 mlog(0, "Starting dlm_thread...\n");
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800512
Joseph Qi5afc44e2015-11-05 18:44:13 -0800513 dlm->dlm_thread_task = kthread_run(dlm_thread, dlm, "dlm-%s",
514 dlm->name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800515 if (IS_ERR(dlm->dlm_thread_task)) {
516 mlog_errno(PTR_ERR(dlm->dlm_thread_task));
517 dlm->dlm_thread_task = NULL;
518 return -EINVAL;
519 }
520
521 return 0;
522}
523
524void dlm_complete_thread(struct dlm_ctxt *dlm)
525{
526 if (dlm->dlm_thread_task) {
Sunil Mushran8e17d162010-11-19 15:06:49 -0800527 mlog(ML_KTHREAD, "Waiting for dlm thread to exit\n");
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800528 kthread_stop(dlm->dlm_thread_task);
529 dlm->dlm_thread_task = NULL;
530 }
531}
532
533static int dlm_dirty_list_empty(struct dlm_ctxt *dlm)
534{
535 int empty;
536
537 spin_lock(&dlm->spinlock);
538 empty = list_empty(&dlm->dirty_list);
539 spin_unlock(&dlm->spinlock);
540
541 return empty;
542}
543
544static void dlm_flush_asts(struct dlm_ctxt *dlm)
545{
546 int ret;
547 struct dlm_lock *lock;
548 struct dlm_lock_resource *res;
549 u8 hi;
550
551 spin_lock(&dlm->ast_lock);
552 while (!list_empty(&dlm->pending_asts)) {
553 lock = list_entry(dlm->pending_asts.next,
554 struct dlm_lock, ast_list);
555 /* get an extra ref on lock */
556 dlm_lock_get(lock);
557 res = lock->lockres;
Sunil Mushran8e17d162010-11-19 15:06:49 -0800558 mlog(0, "%s: res %.*s, Flush AST for lock %u:%llu, type %d, "
559 "node %u\n", dlm->name, res->lockname.len,
560 res->lockname.name,
561 dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
562 dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)),
563 lock->ml.type, lock->ml.node);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800564
565 BUG_ON(!lock->ast_pending);
566
567 /* remove from list (including ref) */
568 list_del_init(&lock->ast_list);
569 dlm_lock_put(lock);
570 spin_unlock(&dlm->ast_lock);
571
572 if (lock->ml.node != dlm->node_num) {
573 ret = dlm_do_remote_ast(dlm, res, lock);
574 if (ret < 0)
575 mlog_errno(ret);
576 } else
577 dlm_do_local_ast(dlm, res, lock);
578
579 spin_lock(&dlm->ast_lock);
580
581 /* possible that another ast was queued while
582 * we were delivering the last one */
583 if (!list_empty(&lock->ast_list)) {
Sunil Mushran8e17d162010-11-19 15:06:49 -0800584 mlog(0, "%s: res %.*s, AST queued while flushing last "
585 "one\n", dlm->name, res->lockname.len,
586 res->lockname.name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800587 } else
588 lock->ast_pending = 0;
589
590 /* drop the extra ref.
591 * this may drop it completely. */
592 dlm_lock_put(lock);
593 dlm_lockres_release_ast(dlm, res);
594 }
595
596 while (!list_empty(&dlm->pending_basts)) {
597 lock = list_entry(dlm->pending_basts.next,
598 struct dlm_lock, bast_list);
599 /* get an extra ref on lock */
600 dlm_lock_get(lock);
601 res = lock->lockres;
602
603 BUG_ON(!lock->bast_pending);
604
605 /* get the highest blocked lock, and reset */
606 spin_lock(&lock->spinlock);
607 BUG_ON(lock->ml.highest_blocked <= LKM_IVMODE);
608 hi = lock->ml.highest_blocked;
609 lock->ml.highest_blocked = LKM_IVMODE;
610 spin_unlock(&lock->spinlock);
611
612 /* remove from list (including ref) */
613 list_del_init(&lock->bast_list);
614 dlm_lock_put(lock);
615 spin_unlock(&dlm->ast_lock);
616
Sunil Mushran8e17d162010-11-19 15:06:49 -0800617 mlog(0, "%s: res %.*s, Flush BAST for lock %u:%llu, "
618 "blocked %d, node %u\n",
619 dlm->name, res->lockname.len, res->lockname.name,
620 dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
621 dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)),
622 hi, lock->ml.node);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800623
624 if (lock->ml.node != dlm->node_num) {
625 ret = dlm_send_proxy_bast(dlm, res, lock, hi);
626 if (ret < 0)
627 mlog_errno(ret);
628 } else
629 dlm_do_local_bast(dlm, res, lock, hi);
630
631 spin_lock(&dlm->ast_lock);
632
633 /* possible that another bast was queued while
634 * we were delivering the last one */
635 if (!list_empty(&lock->bast_list)) {
Sunil Mushran8e17d162010-11-19 15:06:49 -0800636 mlog(0, "%s: res %.*s, BAST queued while flushing last "
637 "one\n", dlm->name, res->lockname.len,
638 res->lockname.name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800639 } else
640 lock->bast_pending = 0;
641
642 /* drop the extra ref.
643 * this may drop it completely. */
644 dlm_lock_put(lock);
645 dlm_lockres_release_ast(dlm, res);
646 }
647 wake_up(&dlm->ast_wq);
648 spin_unlock(&dlm->ast_lock);
649}
650
651
652#define DLM_THREAD_TIMEOUT_MS (4 * 1000)
653#define DLM_THREAD_MAX_DIRTY 100
654#define DLM_THREAD_MAX_ASTS 10
655
656static int dlm_thread(void *data)
657{
658 struct dlm_lock_resource *res;
659 struct dlm_ctxt *dlm = data;
660 unsigned long timeout = msecs_to_jiffies(DLM_THREAD_TIMEOUT_MS);
661
662 mlog(0, "dlm thread running for %s...\n", dlm->name);
663
664 while (!kthread_should_stop()) {
665 int n = DLM_THREAD_MAX_DIRTY;
666
667 /* dlm_shutting_down is very point-in-time, but that
668 * doesn't matter as we'll just loop back around if we
669 * get false on the leading edge of a state
670 * transition. */
671 dlm_run_purge_list(dlm, dlm_shutting_down(dlm));
672
673 /* We really don't want to hold dlm->spinlock while
674 * calling dlm_shuffle_lists on each lockres that
675 * needs to have its queues adjusted and AST/BASTs
676 * run. So let's pull each entry off the dirty_list
677 * and drop dlm->spinlock ASAP. Once off the list,
678 * res->spinlock needs to be taken again to protect
679 * the queues while calling dlm_shuffle_lists. */
680 spin_lock(&dlm->spinlock);
681 while (!list_empty(&dlm->dirty_list)) {
682 int delay = 0;
683 res = list_entry(dlm->dirty_list.next,
684 struct dlm_lock_resource, dirty);
685
686 /* peel a lockres off, remove it from the list,
687 * unset the dirty flag and drop the dlm lock */
688 BUG_ON(!res);
689 dlm_lockres_get(res);
690
691 spin_lock(&res->spinlock);
Kurt Hackelddc09c82007-01-05 15:00:17 -0800692 /* We clear the DLM_LOCK_RES_DIRTY state once we shuffle lists below */
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800693 list_del_init(&res->dirty);
694 spin_unlock(&res->spinlock);
695 spin_unlock(&dlm->spinlock);
Kurt Hackel6ff06a92006-05-01 11:51:45 -0700696 /* Drop dirty_list ref */
697 dlm_lockres_put(res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800698
699 /* lockres can be re-dirtied/re-added to the
700 * dirty_list in this gap, but that is ok */
701
Wengang Wangd9ef7522010-05-17 20:20:44 +0800702 spin_lock(&dlm->ast_lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800703 spin_lock(&res->spinlock);
704 if (res->owner != dlm->node_num) {
705 __dlm_print_one_lock_resource(res);
Sunil Mushran8e17d162010-11-19 15:06:49 -0800706 mlog(ML_ERROR, "%s: inprog %d, mig %d, reco %d,"
707 " dirty %d\n", dlm->name,
708 !!(res->state & DLM_LOCK_RES_IN_PROGRESS),
709 !!(res->state & DLM_LOCK_RES_MIGRATING),
710 !!(res->state & DLM_LOCK_RES_RECOVERING),
711 !!(res->state & DLM_LOCK_RES_DIRTY));
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800712 }
713 BUG_ON(res->owner != dlm->node_num);
714
715 /* it is now ok to move lockreses in these states
716 * to the dirty list, assuming that they will only be
717 * dirty for a short while. */
Kurt Hackelddc09c82007-01-05 15:00:17 -0800718 BUG_ON(res->state & DLM_LOCK_RES_MIGRATING);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800719 if (res->state & (DLM_LOCK_RES_IN_PROGRESS |
Jiufei Xue814ce692016-03-15 14:53:20 -0700720 DLM_LOCK_RES_RECOVERING |
721 DLM_LOCK_RES_RECOVERY_WAITING)) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800722 /* move it to the tail and keep going */
Kurt Hackelddc09c82007-01-05 15:00:17 -0800723 res->state &= ~DLM_LOCK_RES_DIRTY;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800724 spin_unlock(&res->spinlock);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800725 spin_unlock(&dlm->ast_lock);
Sunil Mushran8e17d162010-11-19 15:06:49 -0800726 mlog(0, "%s: res %.*s, inprogress, delay list "
727 "shuffle, state %d\n", dlm->name,
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800728 res->lockname.len, res->lockname.name,
729 res->state);
730 delay = 1;
731 goto in_progress;
732 }
733
734 /* at this point the lockres is not migrating/
735 * recovering/in-progress. we have the lockres
736 * spinlock and do NOT have the dlm lock.
737 * safe to reserve/queue asts and run the lists. */
738
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800739 /* called while holding lockres lock */
740 dlm_shuffle_lists(dlm, res);
Kurt Hackelddc09c82007-01-05 15:00:17 -0800741 res->state &= ~DLM_LOCK_RES_DIRTY;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800742 spin_unlock(&res->spinlock);
Wengang Wangd9ef7522010-05-17 20:20:44 +0800743 spin_unlock(&dlm->ast_lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800744
745 dlm_lockres_calc_usage(dlm, res);
746
747in_progress:
748
749 spin_lock(&dlm->spinlock);
750 /* if the lock was in-progress, stick
751 * it on the back of the list */
752 if (delay) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800753 spin_lock(&res->spinlock);
Kurt Hackelddc09c82007-01-05 15:00:17 -0800754 __dlm_dirty_lockres(dlm, res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800755 spin_unlock(&res->spinlock);
756 }
757 dlm_lockres_put(res);
758
759 /* unlikely, but we may need to give time to
760 * other tasks */
761 if (!--n) {
Sunil Mushran8e17d162010-11-19 15:06:49 -0800762 mlog(0, "%s: Throttling dlm thread\n",
763 dlm->name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800764 break;
765 }
766 }
767
768 spin_unlock(&dlm->spinlock);
769 dlm_flush_asts(dlm);
770
771 /* yield and continue right away if there is more work to do */
772 if (!n) {
Kurt Hackelf85cd472006-05-01 14:27:41 -0700773 cond_resched();
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800774 continue;
775 }
776
777 wait_event_interruptible_timeout(dlm->dlm_thread_wq,
778 !dlm_dirty_list_empty(dlm) ||
779 kthread_should_stop(),
780 timeout);
781 }
782
783 mlog(0, "quitting DLM thread\n");
784 return 0;
785}