xref: /openbmc/linux/fs/ocfs2/dlm/dlmthread.c (revision ca322fb6)
1328970deSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
26714d8e8SKurt Hackel /* -*- mode: c; c-basic-offset: 8; -*-
36714d8e8SKurt Hackel  * vim: noexpandtab sw=8 ts=8 sts=0:
46714d8e8SKurt Hackel  *
56714d8e8SKurt Hackel  * dlmthread.c
66714d8e8SKurt Hackel  *
76714d8e8SKurt Hackel  * standalone DLM module
86714d8e8SKurt Hackel  *
96714d8e8SKurt Hackel  * Copyright (C) 2004 Oracle.  All rights reserved.
106714d8e8SKurt Hackel  */
116714d8e8SKurt Hackel 
126714d8e8SKurt Hackel 
136714d8e8SKurt Hackel #include <linux/module.h>
146714d8e8SKurt Hackel #include <linux/fs.h>
156714d8e8SKurt Hackel #include <linux/types.h>
166714d8e8SKurt Hackel #include <linux/highmem.h>
176714d8e8SKurt Hackel #include <linux/init.h>
186714d8e8SKurt Hackel #include <linux/sysctl.h>
196714d8e8SKurt Hackel #include <linux/random.h>
206714d8e8SKurt Hackel #include <linux/blkdev.h>
216714d8e8SKurt Hackel #include <linux/socket.h>
226714d8e8SKurt Hackel #include <linux/inet.h>
236714d8e8SKurt Hackel #include <linux/timer.h>
246714d8e8SKurt Hackel #include <linux/kthread.h>
258d79d088SKurt Hackel #include <linux/delay.h>
266714d8e8SKurt Hackel 
276714d8e8SKurt Hackel 
28ca322fb6SMasahiro Yamada #include "../cluster/heartbeat.h"
29ca322fb6SMasahiro Yamada #include "../cluster/nodemanager.h"
30ca322fb6SMasahiro Yamada #include "../cluster/tcp.h"
316714d8e8SKurt Hackel 
326714d8e8SKurt Hackel #include "dlmapi.h"
336714d8e8SKurt Hackel #include "dlmcommon.h"
346714d8e8SKurt Hackel #include "dlmdomain.h"
356714d8e8SKurt Hackel 
366714d8e8SKurt Hackel #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_THREAD)
37ca322fb6SMasahiro Yamada #include "../cluster/masklog.h"
386714d8e8SKurt Hackel 
396714d8e8SKurt Hackel static int dlm_thread(void *data);
406714d8e8SKurt Hackel static void dlm_flush_asts(struct dlm_ctxt *dlm);
416714d8e8SKurt Hackel 
426714d8e8SKurt Hackel #define dlm_lock_is_remote(dlm, lock)     ((lock)->ml.node != (dlm)->node_num)
436714d8e8SKurt Hackel 
446714d8e8SKurt Hackel /* will exit holding res->spinlock, but may drop in function */
456714d8e8SKurt Hackel /* waits until flags are cleared on res->state */
466714d8e8SKurt Hackel void __dlm_wait_on_lockres_flags(struct dlm_lock_resource *res, int flags)
476714d8e8SKurt Hackel {
486714d8e8SKurt Hackel 	DECLARE_WAITQUEUE(wait, current);
496714d8e8SKurt Hackel 
506714d8e8SKurt Hackel 	assert_spin_locked(&res->spinlock);
516714d8e8SKurt Hackel 
526714d8e8SKurt Hackel 	add_wait_queue(&res->wq, &wait);
536714d8e8SKurt Hackel repeat:
546714d8e8SKurt Hackel 	set_current_state(TASK_UNINTERRUPTIBLE);
556714d8e8SKurt Hackel 	if (res->state & flags) {
566714d8e8SKurt Hackel 		spin_unlock(&res->spinlock);
576714d8e8SKurt Hackel 		schedule();
586714d8e8SKurt Hackel 		spin_lock(&res->spinlock);
596714d8e8SKurt Hackel 		goto repeat;
606714d8e8SKurt Hackel 	}
616714d8e8SKurt Hackel 	remove_wait_queue(&res->wq, &wait);
625c2c9d38SMilind Arun Choudhary 	__set_current_state(TASK_RUNNING);
636714d8e8SKurt Hackel }
646714d8e8SKurt Hackel 
65ba2bf218SKurt Hackel int __dlm_lockres_has_locks(struct dlm_lock_resource *res)
666714d8e8SKurt Hackel {
676714d8e8SKurt Hackel 	if (list_empty(&res->granted) &&
686714d8e8SKurt Hackel 	    list_empty(&res->converting) &&
69ba2bf218SKurt Hackel 	    list_empty(&res->blocked))
70ba2bf218SKurt Hackel 		return 0;
716714d8e8SKurt Hackel 	return 1;
72ba2bf218SKurt Hackel }
73ba2bf218SKurt Hackel 
74ba2bf218SKurt Hackel /* "unused": the lockres has no locks, is not on the dirty list,
75ba2bf218SKurt Hackel  * has no inflight locks (in the gap between mastery and acquiring
76ba2bf218SKurt Hackel  * the first lock), and has no bits in its refmap.
77ba2bf218SKurt Hackel  * truly ready to be freed. */
78ba2bf218SKurt Hackel int __dlm_lockres_unused(struct dlm_lock_resource *res)
79ba2bf218SKurt Hackel {
80a524812bSWengang Wang 	int bit;
81a524812bSWengang Wang 
82ff0a522eSSunil Mushran 	assert_spin_locked(&res->spinlock);
83ff0a522eSSunil Mushran 
84a524812bSWengang Wang 	if (__dlm_lockres_has_locks(res))
85a524812bSWengang Wang 		return 0;
86a524812bSWengang Wang 
87ff0a522eSSunil Mushran 	/* Locks are in the process of being created */
88ff0a522eSSunil Mushran 	if (res->inflight_locks)
89ff0a522eSSunil Mushran 		return 0;
90ff0a522eSSunil Mushran 
91a524812bSWengang Wang 	if (!list_empty(&res->dirty) || res->state & DLM_LOCK_RES_DIRTY)
92a524812bSWengang Wang 		return 0;
93a524812bSWengang Wang 
94814ce694SJiufei Xue 	if (res->state & (DLM_LOCK_RES_RECOVERING|
95814ce694SJiufei Xue 			DLM_LOCK_RES_RECOVERY_WAITING))
96a524812bSWengang Wang 		return 0;
97a524812bSWengang Wang 
98ff0a522eSSunil Mushran 	/* Another node has this resource with this node as the master */
99a524812bSWengang Wang 	bit = find_next_bit(res->refmap, O2NM_MAX_NODES, 0);
100a524812bSWengang Wang 	if (bit < O2NM_MAX_NODES)
101a524812bSWengang Wang 		return 0;
102a524812bSWengang Wang 
103ba2bf218SKurt Hackel 	return 1;
104ba2bf218SKurt Hackel }
1056714d8e8SKurt Hackel 
1066714d8e8SKurt Hackel 
1076714d8e8SKurt Hackel /* Call whenever you may have added or deleted something from one of
1086714d8e8SKurt Hackel  * the lockres queue's. This will figure out whether it belongs on the
1096714d8e8SKurt Hackel  * unused list or not and does the appropriate thing. */
1106714d8e8SKurt Hackel void __dlm_lockres_calc_usage(struct dlm_ctxt *dlm,
1116714d8e8SKurt Hackel 			      struct dlm_lock_resource *res)
1126714d8e8SKurt Hackel {
1136714d8e8SKurt Hackel 	assert_spin_locked(&dlm->spinlock);
1146714d8e8SKurt Hackel 	assert_spin_locked(&res->spinlock);
1156714d8e8SKurt Hackel 
1166714d8e8SKurt Hackel 	if (__dlm_lockres_unused(res)){
1176714d8e8SKurt Hackel 		if (list_empty(&res->purge)) {
1188e17d16fSSunil Mushran 			mlog(0, "%s: Adding res %.*s to purge list\n",
1198e17d16fSSunil Mushran 			     dlm->name, res->lockname.len, res->lockname.name);
1206714d8e8SKurt Hackel 
1216714d8e8SKurt Hackel 			res->last_used = jiffies;
122ba2bf218SKurt Hackel 			dlm_lockres_get(res);
1236714d8e8SKurt Hackel 			list_add_tail(&res->purge, &dlm->purge_list);
1246714d8e8SKurt Hackel 			dlm->purge_count++;
1256714d8e8SKurt Hackel 		}
1266714d8e8SKurt Hackel 	} else if (!list_empty(&res->purge)) {
1278e17d16fSSunil Mushran 		mlog(0, "%s: Removing res %.*s from purge list\n",
1288e17d16fSSunil Mushran 		     dlm->name, res->lockname.len, res->lockname.name);
1296714d8e8SKurt Hackel 
1306714d8e8SKurt Hackel 		list_del_init(&res->purge);
131ba2bf218SKurt Hackel 		dlm_lockres_put(res);
1326714d8e8SKurt Hackel 		dlm->purge_count--;
1336714d8e8SKurt Hackel 	}
1346714d8e8SKurt Hackel }
1356714d8e8SKurt Hackel 
1366714d8e8SKurt Hackel void dlm_lockres_calc_usage(struct dlm_ctxt *dlm,
1376714d8e8SKurt Hackel 			    struct dlm_lock_resource *res)
1386714d8e8SKurt Hackel {
1396714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
1406714d8e8SKurt Hackel 	spin_lock(&res->spinlock);
1416714d8e8SKurt Hackel 
1426714d8e8SKurt Hackel 	__dlm_lockres_calc_usage(dlm, res);
1436714d8e8SKurt Hackel 
1446714d8e8SKurt Hackel 	spin_unlock(&res->spinlock);
1456714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
1466714d8e8SKurt Hackel }
1476714d8e8SKurt Hackel 
148ee8f7fcbSpiaojun /*
149ee8f7fcbSpiaojun  * Do the real purge work:
150ee8f7fcbSpiaojun  *     unhash the lockres, and
151ee8f7fcbSpiaojun  *     clear flag DLM_LOCK_RES_DROPPING_REF.
152ee8f7fcbSpiaojun  * It requires dlm and lockres spinlock to be taken.
153ee8f7fcbSpiaojun  */
154ee8f7fcbSpiaojun void __dlm_do_purge_lockres(struct dlm_ctxt *dlm,
155ee8f7fcbSpiaojun 		struct dlm_lock_resource *res)
156ee8f7fcbSpiaojun {
157ee8f7fcbSpiaojun 	assert_spin_locked(&dlm->spinlock);
158ee8f7fcbSpiaojun 	assert_spin_locked(&res->spinlock);
159ee8f7fcbSpiaojun 
160ee8f7fcbSpiaojun 	if (!list_empty(&res->purge)) {
161ee8f7fcbSpiaojun 		mlog(0, "%s: Removing res %.*s from purgelist\n",
162ee8f7fcbSpiaojun 		     dlm->name, res->lockname.len, res->lockname.name);
163ee8f7fcbSpiaojun 		list_del_init(&res->purge);
164ee8f7fcbSpiaojun 		dlm_lockres_put(res);
165ee8f7fcbSpiaojun 		dlm->purge_count--;
166ee8f7fcbSpiaojun 	}
167ee8f7fcbSpiaojun 
168ee8f7fcbSpiaojun 	if (!__dlm_lockres_unused(res)) {
169ee8f7fcbSpiaojun 		mlog(ML_ERROR, "%s: res %.*s in use after deref\n",
170ee8f7fcbSpiaojun 		     dlm->name, res->lockname.len, res->lockname.name);
171ee8f7fcbSpiaojun 		__dlm_print_one_lock_resource(res);
172ee8f7fcbSpiaojun 		BUG();
173ee8f7fcbSpiaojun 	}
174ee8f7fcbSpiaojun 
175ee8f7fcbSpiaojun 	__dlm_unhash_lockres(dlm, res);
176ee8f7fcbSpiaojun 
177ee8f7fcbSpiaojun 	spin_lock(&dlm->track_lock);
178ee8f7fcbSpiaojun 	if (!list_empty(&res->tracking))
179ee8f7fcbSpiaojun 		list_del_init(&res->tracking);
180ee8f7fcbSpiaojun 	else {
181ee8f7fcbSpiaojun 		mlog(ML_ERROR, "%s: Resource %.*s not on the Tracking list\n",
182ee8f7fcbSpiaojun 		     dlm->name, res->lockname.len, res->lockname.name);
183ee8f7fcbSpiaojun 		__dlm_print_one_lock_resource(res);
184ee8f7fcbSpiaojun 	}
185ee8f7fcbSpiaojun 	spin_unlock(&dlm->track_lock);
186ee8f7fcbSpiaojun 
187ee8f7fcbSpiaojun 	/*
188ee8f7fcbSpiaojun 	 * lockres is not in the hash now. drop the flag and wake up
189ee8f7fcbSpiaojun 	 * any processes waiting in dlm_get_lock_resource.
190ee8f7fcbSpiaojun 	 */
191ee8f7fcbSpiaojun 	res->state &= ~DLM_LOCK_RES_DROPPING_REF;
192ee8f7fcbSpiaojun }
193ee8f7fcbSpiaojun 
1947beaf243SSrinivas Eeda static void dlm_purge_lockres(struct dlm_ctxt *dlm,
195faf0ec9fSAdrian Bunk 			     struct dlm_lock_resource *res)
1966714d8e8SKurt Hackel {
1976714d8e8SKurt Hackel 	int master;
198ba2bf218SKurt Hackel 	int ret = 0;
1996714d8e8SKurt Hackel 
2007beaf243SSrinivas Eeda 	assert_spin_locked(&dlm->spinlock);
2017beaf243SSrinivas Eeda 	assert_spin_locked(&res->spinlock);
202516b7e52SSunil Mushran 
203ba2bf218SKurt Hackel 	master = (res->owner == dlm->node_num);
204516b7e52SSunil Mushran 
2058e17d16fSSunil Mushran 	mlog(0, "%s: Purging res %.*s, master %d\n", dlm->name,
2068e17d16fSSunil Mushran 	     res->lockname.len, res->lockname.name, master);
207ba2bf218SKurt Hackel 
208ba2bf218SKurt Hackel 	if (!master) {
209309e9191Spiaojun 		if (res->state & DLM_LOCK_RES_DROPPING_REF) {
210ee8f7fcbSpiaojun 			mlog(ML_NOTICE, "%s: res %.*s already in DLM_LOCK_RES_DROPPING_REF state\n",
211ee8f7fcbSpiaojun 				dlm->name, res->lockname.len, res->lockname.name);
212309e9191Spiaojun 			spin_unlock(&res->spinlock);
213309e9191Spiaojun 			return;
214309e9191Spiaojun 		}
215309e9191Spiaojun 
2167beaf243SSrinivas Eeda 		res->state |= DLM_LOCK_RES_DROPPING_REF;
217c824c3c7SSunil Mushran 		/* drop spinlock...  retake below */
2187beaf243SSrinivas Eeda 		spin_unlock(&res->spinlock);
219c824c3c7SSunil Mushran 		spin_unlock(&dlm->spinlock);
220c824c3c7SSunil Mushran 
2213b8118cfSKurt Hackel 		spin_lock(&res->spinlock);
2223b8118cfSKurt Hackel 		/* This ensures that clear refmap is sent after the set */
2237dc102b7SSunil Mushran 		__dlm_wait_on_lockres_flags(res, DLM_LOCK_RES_SETREF_INPROG);
2243b8118cfSKurt Hackel 		spin_unlock(&res->spinlock);
225c824c3c7SSunil Mushran 
226ba2bf218SKurt Hackel 		/* clear our bit from the master's refmap, ignore errors */
227ba2bf218SKurt Hackel 		ret = dlm_drop_lockres_ref(dlm, res);
228ba2bf218SKurt Hackel 		if (ret < 0) {
229ba2bf218SKurt Hackel 			if (!dlm_is_host_down(ret))
2306714d8e8SKurt Hackel 				BUG();
2316714d8e8SKurt Hackel 		}
2326714d8e8SKurt Hackel 		spin_lock(&dlm->spinlock);
2337beaf243SSrinivas Eeda 		spin_lock(&res->spinlock);
234ba2bf218SKurt Hackel 	}
2356714d8e8SKurt Hackel 
236ba2bf218SKurt Hackel 	if (!list_empty(&res->purge)) {
2378e17d16fSSunil Mushran 		mlog(0, "%s: Removing res %.*s from purgelist, master %d\n",
2388e17d16fSSunil Mushran 		     dlm->name, res->lockname.len, res->lockname.name, master);
239ba2bf218SKurt Hackel 		list_del_init(&res->purge);
240ba2bf218SKurt Hackel 		dlm_lockres_put(res);
2416714d8e8SKurt Hackel 		dlm->purge_count--;
2427beaf243SSrinivas Eeda 	}
2437beaf243SSrinivas Eeda 
244309e9191Spiaojun 	if (!master && ret == DLM_DEREF_RESPONSE_INPROG) {
245309e9191Spiaojun 		mlog(0, "%s: deref %.*s in progress\n",
246842b90b6Sxuejiufei 			dlm->name, res->lockname.len, res->lockname.name);
247842b90b6Sxuejiufei 		spin_unlock(&res->spinlock);
248842b90b6Sxuejiufei 		return;
249842b90b6Sxuejiufei 	}
250842b90b6Sxuejiufei 
2517beaf243SSrinivas Eeda 	if (!__dlm_lockres_unused(res)) {
2528e17d16fSSunil Mushran 		mlog(ML_ERROR, "%s: res %.*s in use after deref\n",
2537beaf243SSrinivas Eeda 		     dlm->name, res->lockname.len, res->lockname.name);
2547beaf243SSrinivas Eeda 		__dlm_print_one_lock_resource(res);
2557beaf243SSrinivas Eeda 		BUG();
2567beaf243SSrinivas Eeda 	}
25783e32d90SWengang Wang 
258e9f0b6a6SSunil Mushran 	__dlm_unhash_lockres(dlm, res);
259ba2bf218SKurt Hackel 
260f57a22ddSYiwen Jiang 	spin_lock(&dlm->track_lock);
261f57a22ddSYiwen Jiang 	if (!list_empty(&res->tracking))
262f57a22ddSYiwen Jiang 		list_del_init(&res->tracking);
263f57a22ddSYiwen Jiang 	else {
264f57a22ddSYiwen Jiang 		mlog(ML_ERROR, "Resource %.*s not on the Tracking list\n",
265f57a22ddSYiwen Jiang 				res->lockname.len, res->lockname.name);
266f57a22ddSYiwen Jiang 		__dlm_print_one_lock_resource(res);
267f57a22ddSYiwen Jiang 	}
268f57a22ddSYiwen Jiang 	spin_unlock(&dlm->track_lock);
269f57a22ddSYiwen Jiang 
270ba2bf218SKurt Hackel 	/* lockres is not in the hash now.  drop the flag and wake up
271ba2bf218SKurt Hackel 	 * any processes waiting in dlm_get_lock_resource. */
272ba2bf218SKurt Hackel 	if (!master) {
273ba2bf218SKurt Hackel 		res->state &= ~DLM_LOCK_RES_DROPPING_REF;
274ba2bf218SKurt Hackel 		spin_unlock(&res->spinlock);
275ba2bf218SKurt Hackel 		wake_up(&res->wq);
2767beaf243SSrinivas Eeda 	} else
2777beaf243SSrinivas Eeda 		spin_unlock(&res->spinlock);
2788b219809SKurt Hackel }
2798b219809SKurt Hackel 
2806714d8e8SKurt Hackel static void dlm_run_purge_list(struct dlm_ctxt *dlm,
2816714d8e8SKurt Hackel 			       int purge_now)
2826714d8e8SKurt Hackel {
2836714d8e8SKurt Hackel 	unsigned int run_max, unused;
2846714d8e8SKurt Hackel 	unsigned long purge_jiffies;
2856714d8e8SKurt Hackel 	struct dlm_lock_resource *lockres;
2866714d8e8SKurt Hackel 
2876714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
2886714d8e8SKurt Hackel 	run_max = dlm->purge_count;
2896714d8e8SKurt Hackel 
2906714d8e8SKurt Hackel 	while(run_max && !list_empty(&dlm->purge_list)) {
2916714d8e8SKurt Hackel 		run_max--;
2926714d8e8SKurt Hackel 
2936714d8e8SKurt Hackel 		lockres = list_entry(dlm->purge_list.next,
2946714d8e8SKurt Hackel 				     struct dlm_lock_resource, purge);
2956714d8e8SKurt Hackel 
2966714d8e8SKurt Hackel 		spin_lock(&lockres->spinlock);
2976714d8e8SKurt Hackel 
2986714d8e8SKurt Hackel 		purge_jiffies = lockres->last_used +
2996714d8e8SKurt Hackel 			msecs_to_jiffies(DLM_PURGE_INTERVAL_MS);
3006714d8e8SKurt Hackel 
3016714d8e8SKurt Hackel 		/* Make sure that we want to be processing this guy at
3026714d8e8SKurt Hackel 		 * this time. */
3036714d8e8SKurt Hackel 		if (!purge_now && time_after(purge_jiffies, jiffies)) {
3046714d8e8SKurt Hackel 			/* Since resources are added to the purge list
3056714d8e8SKurt Hackel 			 * in tail order, we can stop at the first
3066714d8e8SKurt Hackel 			 * unpurgable resource -- anyone added after
3076714d8e8SKurt Hackel 			 * him will have a greater last_used value */
3087beaf243SSrinivas Eeda 			spin_unlock(&lockres->spinlock);
3096714d8e8SKurt Hackel 			break;
3106714d8e8SKurt Hackel 		}
3116714d8e8SKurt Hackel 
3127beaf243SSrinivas Eeda 		/* Status of the lockres *might* change so double
3137beaf243SSrinivas Eeda 		 * check. If the lockres is unused, holding the dlm
3147beaf243SSrinivas Eeda 		 * spinlock will prevent people from getting and more
3157beaf243SSrinivas Eeda 		 * refs on it. */
3167beaf243SSrinivas Eeda 		unused = __dlm_lockres_unused(lockres);
3177beaf243SSrinivas Eeda 		if (!unused ||
318ac4fef4dSXue jiufei 		    (lockres->state & DLM_LOCK_RES_MIGRATING) ||
319ac4fef4dSXue jiufei 		    (lockres->inflight_assert_workers != 0)) {
3208e17d16fSSunil Mushran 			mlog(0, "%s: res %.*s is in use or being remastered, "
321ac4fef4dSXue jiufei 			     "used %d, state %d, assert master workers %u\n",
322ac4fef4dSXue jiufei 			     dlm->name, lockres->lockname.len,
323ac4fef4dSXue jiufei 			     lockres->lockname.name,
324ac4fef4dSXue jiufei 			     !unused, lockres->state,
325ac4fef4dSXue jiufei 			     lockres->inflight_assert_workers);
326a270c6d3SXue jiufei 			list_move_tail(&lockres->purge, &dlm->purge_list);
3277beaf243SSrinivas Eeda 			spin_unlock(&lockres->spinlock);
3287beaf243SSrinivas Eeda 			continue;
3297beaf243SSrinivas Eeda 		}
3307beaf243SSrinivas Eeda 
33178062cb2SSunil Mushran 		dlm_lockres_get(lockres);
3326714d8e8SKurt Hackel 
3337beaf243SSrinivas Eeda 		dlm_purge_lockres(dlm, lockres);
33478062cb2SSunil Mushran 
3353fca0894SSunil Mushran 		dlm_lockres_put(lockres);
3366714d8e8SKurt Hackel 
3376714d8e8SKurt Hackel 		/* Avoid adding any scheduling latencies */
3386714d8e8SKurt Hackel 		cond_resched_lock(&dlm->spinlock);
3396714d8e8SKurt Hackel 	}
3406714d8e8SKurt Hackel 
3416714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
3426714d8e8SKurt Hackel }
3436714d8e8SKurt Hackel 
3446714d8e8SKurt Hackel static void dlm_shuffle_lists(struct dlm_ctxt *dlm,
3456714d8e8SKurt Hackel 			      struct dlm_lock_resource *res)
3466714d8e8SKurt Hackel {
3476714d8e8SKurt Hackel 	struct dlm_lock *lock, *target;
3486714d8e8SKurt Hackel 	int can_grant = 1;
3496714d8e8SKurt Hackel 
3508e17d16fSSunil Mushran 	/*
3518e17d16fSSunil Mushran 	 * Because this function is called with the lockres
3526714d8e8SKurt Hackel 	 * spinlock, and because we know that it is not migrating/
3536714d8e8SKurt Hackel 	 * recovering/in-progress, it is fine to reserve asts and
3548e17d16fSSunil Mushran 	 * basts right before queueing them all throughout
3558e17d16fSSunil Mushran 	 */
356d9ef7522SWengang Wang 	assert_spin_locked(&dlm->ast_lock);
3576714d8e8SKurt Hackel 	assert_spin_locked(&res->spinlock);
3586714d8e8SKurt Hackel 	BUG_ON((res->state & (DLM_LOCK_RES_MIGRATING|
3596714d8e8SKurt Hackel 			      DLM_LOCK_RES_RECOVERING|
3606714d8e8SKurt Hackel 			      DLM_LOCK_RES_IN_PROGRESS)));
3616714d8e8SKurt Hackel 
3626714d8e8SKurt Hackel converting:
3636714d8e8SKurt Hackel 	if (list_empty(&res->converting))
3646714d8e8SKurt Hackel 		goto blocked;
3658e17d16fSSunil Mushran 	mlog(0, "%s: res %.*s has locks on the convert queue\n", dlm->name,
3668e17d16fSSunil Mushran 	     res->lockname.len, res->lockname.name);
3676714d8e8SKurt Hackel 
3686714d8e8SKurt Hackel 	target = list_entry(res->converting.next, struct dlm_lock, list);
3696714d8e8SKurt Hackel 	if (target->ml.convert_type == LKM_IVMODE) {
3708e17d16fSSunil Mushran 		mlog(ML_ERROR, "%s: res %.*s converting lock to invalid mode\n",
3718e17d16fSSunil Mushran 		     dlm->name, res->lockname.len, res->lockname.name);
3726714d8e8SKurt Hackel 		BUG();
3736714d8e8SKurt Hackel 	}
374df53cd3bSDong Fang 	list_for_each_entry(lock, &res->granted, list) {
3756714d8e8SKurt Hackel 		if (lock==target)
3766714d8e8SKurt Hackel 			continue;
3776714d8e8SKurt Hackel 		if (!dlm_lock_compatible(lock->ml.type,
3786714d8e8SKurt Hackel 					 target->ml.convert_type)) {
3796714d8e8SKurt Hackel 			can_grant = 0;
3806714d8e8SKurt Hackel 			/* queue the BAST if not already */
3816714d8e8SKurt Hackel 			if (lock->ml.highest_blocked == LKM_IVMODE) {
3826714d8e8SKurt Hackel 				__dlm_lockres_reserve_ast(res);
383d9ef7522SWengang Wang 				__dlm_queue_bast(dlm, lock);
3846714d8e8SKurt Hackel 			}
3856714d8e8SKurt Hackel 			/* update the highest_blocked if needed */
3866714d8e8SKurt Hackel 			if (lock->ml.highest_blocked < target->ml.convert_type)
3876714d8e8SKurt Hackel 				lock->ml.highest_blocked =
3886714d8e8SKurt Hackel 					target->ml.convert_type;
3896714d8e8SKurt Hackel 		}
3906714d8e8SKurt Hackel 	}
391df53cd3bSDong Fang 
392df53cd3bSDong Fang 	list_for_each_entry(lock, &res->converting, list) {
3936714d8e8SKurt Hackel 		if (lock==target)
3946714d8e8SKurt Hackel 			continue;
3956714d8e8SKurt Hackel 		if (!dlm_lock_compatible(lock->ml.type,
3966714d8e8SKurt Hackel 					 target->ml.convert_type)) {
3976714d8e8SKurt Hackel 			can_grant = 0;
3986714d8e8SKurt Hackel 			if (lock->ml.highest_blocked == LKM_IVMODE) {
3996714d8e8SKurt Hackel 				__dlm_lockres_reserve_ast(res);
400d9ef7522SWengang Wang 				__dlm_queue_bast(dlm, lock);
4016714d8e8SKurt Hackel 			}
4026714d8e8SKurt Hackel 			if (lock->ml.highest_blocked < target->ml.convert_type)
4036714d8e8SKurt Hackel 				lock->ml.highest_blocked =
4046714d8e8SKurt Hackel 					target->ml.convert_type;
4056714d8e8SKurt Hackel 		}
4066714d8e8SKurt Hackel 	}
4076714d8e8SKurt Hackel 
4086714d8e8SKurt Hackel 	/* we can convert the lock */
4096714d8e8SKurt Hackel 	if (can_grant) {
4106714d8e8SKurt Hackel 		spin_lock(&target->spinlock);
4116714d8e8SKurt Hackel 		BUG_ON(target->ml.highest_blocked != LKM_IVMODE);
4126714d8e8SKurt Hackel 
4138e17d16fSSunil Mushran 		mlog(0, "%s: res %.*s, AST for Converting lock %u:%llu, type "
4148e17d16fSSunil Mushran 		     "%d => %d, node %u\n", dlm->name, res->lockname.len,
4158e17d16fSSunil Mushran 		     res->lockname.name,
4168e17d16fSSunil Mushran 		     dlm_get_lock_cookie_node(be64_to_cpu(target->ml.cookie)),
4178e17d16fSSunil Mushran 		     dlm_get_lock_cookie_seq(be64_to_cpu(target->ml.cookie)),
4188e17d16fSSunil Mushran 		     target->ml.type,
4196714d8e8SKurt Hackel 		     target->ml.convert_type, target->ml.node);
4206714d8e8SKurt Hackel 
4216714d8e8SKurt Hackel 		target->ml.type = target->ml.convert_type;
4226714d8e8SKurt Hackel 		target->ml.convert_type = LKM_IVMODE;
423f116629dSAkinobu Mita 		list_move_tail(&target->list, &res->granted);
4246714d8e8SKurt Hackel 
4256714d8e8SKurt Hackel 		BUG_ON(!target->lksb);
4266714d8e8SKurt Hackel 		target->lksb->status = DLM_NORMAL;
4276714d8e8SKurt Hackel 
4286714d8e8SKurt Hackel 		spin_unlock(&target->spinlock);
4296714d8e8SKurt Hackel 
4306714d8e8SKurt Hackel 		__dlm_lockres_reserve_ast(res);
431d9ef7522SWengang Wang 		__dlm_queue_ast(dlm, target);
4326714d8e8SKurt Hackel 		/* go back and check for more */
4336714d8e8SKurt Hackel 		goto converting;
4346714d8e8SKurt Hackel 	}
4356714d8e8SKurt Hackel 
4366714d8e8SKurt Hackel blocked:
4376714d8e8SKurt Hackel 	if (list_empty(&res->blocked))
4386714d8e8SKurt Hackel 		goto leave;
4396714d8e8SKurt Hackel 	target = list_entry(res->blocked.next, struct dlm_lock, list);
4406714d8e8SKurt Hackel 
441df53cd3bSDong Fang 	list_for_each_entry(lock, &res->granted, list) {
4426714d8e8SKurt Hackel 		if (lock==target)
4436714d8e8SKurt Hackel 			continue;
4446714d8e8SKurt Hackel 		if (!dlm_lock_compatible(lock->ml.type, target->ml.type)) {
4456714d8e8SKurt Hackel 			can_grant = 0;
4466714d8e8SKurt Hackel 			if (lock->ml.highest_blocked == LKM_IVMODE) {
4476714d8e8SKurt Hackel 				__dlm_lockres_reserve_ast(res);
448d9ef7522SWengang Wang 				__dlm_queue_bast(dlm, lock);
4496714d8e8SKurt Hackel 			}
4506714d8e8SKurt Hackel 			if (lock->ml.highest_blocked < target->ml.type)
4516714d8e8SKurt Hackel 				lock->ml.highest_blocked = target->ml.type;
4526714d8e8SKurt Hackel 		}
4536714d8e8SKurt Hackel 	}
4546714d8e8SKurt Hackel 
455df53cd3bSDong Fang 	list_for_each_entry(lock, &res->converting, list) {
4566714d8e8SKurt Hackel 		if (lock==target)
4576714d8e8SKurt Hackel 			continue;
4586714d8e8SKurt Hackel 		if (!dlm_lock_compatible(lock->ml.type, target->ml.type)) {
4596714d8e8SKurt Hackel 			can_grant = 0;
4606714d8e8SKurt Hackel 			if (lock->ml.highest_blocked == LKM_IVMODE) {
4616714d8e8SKurt Hackel 				__dlm_lockres_reserve_ast(res);
462d9ef7522SWengang Wang 				__dlm_queue_bast(dlm, lock);
4636714d8e8SKurt Hackel 			}
4646714d8e8SKurt Hackel 			if (lock->ml.highest_blocked < target->ml.type)
4656714d8e8SKurt Hackel 				lock->ml.highest_blocked = target->ml.type;
4666714d8e8SKurt Hackel 		}
4676714d8e8SKurt Hackel 	}
4686714d8e8SKurt Hackel 
4696714d8e8SKurt Hackel 	/* we can grant the blocked lock (only
4706714d8e8SKurt Hackel 	 * possible if converting list empty) */
4716714d8e8SKurt Hackel 	if (can_grant) {
4726714d8e8SKurt Hackel 		spin_lock(&target->spinlock);
4736714d8e8SKurt Hackel 		BUG_ON(target->ml.highest_blocked != LKM_IVMODE);
4746714d8e8SKurt Hackel 
4758e17d16fSSunil Mushran 		mlog(0, "%s: res %.*s, AST for Blocked lock %u:%llu, type %d, "
4768e17d16fSSunil Mushran 		     "node %u\n", dlm->name, res->lockname.len,
4778e17d16fSSunil Mushran 		     res->lockname.name,
4788e17d16fSSunil Mushran 		     dlm_get_lock_cookie_node(be64_to_cpu(target->ml.cookie)),
4798e17d16fSSunil Mushran 		     dlm_get_lock_cookie_seq(be64_to_cpu(target->ml.cookie)),
4806714d8e8SKurt Hackel 		     target->ml.type, target->ml.node);
4816714d8e8SKurt Hackel 
4828e17d16fSSunil Mushran 		/* target->ml.type is already correct */
483f116629dSAkinobu Mita 		list_move_tail(&target->list, &res->granted);
4846714d8e8SKurt Hackel 
4856714d8e8SKurt Hackel 		BUG_ON(!target->lksb);
4866714d8e8SKurt Hackel 		target->lksb->status = DLM_NORMAL;
4876714d8e8SKurt Hackel 
4886714d8e8SKurt Hackel 		spin_unlock(&target->spinlock);
4896714d8e8SKurt Hackel 
4906714d8e8SKurt Hackel 		__dlm_lockres_reserve_ast(res);
491d9ef7522SWengang Wang 		__dlm_queue_ast(dlm, target);
4926714d8e8SKurt Hackel 		/* go back and check for more */
4936714d8e8SKurt Hackel 		goto converting;
4946714d8e8SKurt Hackel 	}
4956714d8e8SKurt Hackel 
4966714d8e8SKurt Hackel leave:
4976714d8e8SKurt Hackel 	return;
4986714d8e8SKurt Hackel }
4996714d8e8SKurt Hackel 
5006714d8e8SKurt Hackel /* must have NO locks when calling this with res !=NULL * */
5016714d8e8SKurt Hackel void dlm_kick_thread(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
5026714d8e8SKurt Hackel {
5036714d8e8SKurt Hackel 	if (res) {
5046714d8e8SKurt Hackel 		spin_lock(&dlm->spinlock);
5056714d8e8SKurt Hackel 		spin_lock(&res->spinlock);
5066714d8e8SKurt Hackel 		__dlm_dirty_lockres(dlm, res);
5076714d8e8SKurt Hackel 		spin_unlock(&res->spinlock);
5086714d8e8SKurt Hackel 		spin_unlock(&dlm->spinlock);
5096714d8e8SKurt Hackel 	}
5106714d8e8SKurt Hackel 	wake_up(&dlm->dlm_thread_wq);
5116714d8e8SKurt Hackel }
5126714d8e8SKurt Hackel 
5136714d8e8SKurt Hackel void __dlm_dirty_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
5146714d8e8SKurt Hackel {
5156714d8e8SKurt Hackel 	assert_spin_locked(&dlm->spinlock);
5166714d8e8SKurt Hackel 	assert_spin_locked(&res->spinlock);
5176714d8e8SKurt Hackel 
5186714d8e8SKurt Hackel 	/* don't shuffle secondary queues */
51932c1b90dSNathan Chancellor 	if (res->owner == dlm->node_num) {
520ddc09c8dSKurt Hackel 		if (res->state & (DLM_LOCK_RES_MIGRATING |
521ddc09c8dSKurt Hackel 				  DLM_LOCK_RES_BLOCK_DIRTY))
522ddc09c8dSKurt Hackel 		    return;
523ddc09c8dSKurt Hackel 
524ddc09c8dSKurt Hackel 		if (list_empty(&res->dirty)) {
5256ff06a93SKurt Hackel 			/* ref for dirty_list */
5266ff06a93SKurt Hackel 			dlm_lockres_get(res);
5276714d8e8SKurt Hackel 			list_add_tail(&res->dirty, &dlm->dirty_list);
5286714d8e8SKurt Hackel 			res->state |= DLM_LOCK_RES_DIRTY;
5296714d8e8SKurt Hackel 		}
5306714d8e8SKurt Hackel 	}
5318e17d16fSSunil Mushran 
5328e17d16fSSunil Mushran 	mlog(0, "%s: res %.*s\n", dlm->name, res->lockname.len,
5338e17d16fSSunil Mushran 	     res->lockname.name);
534ddc09c8dSKurt Hackel }
5356714d8e8SKurt Hackel 
5366714d8e8SKurt Hackel 
5376714d8e8SKurt Hackel /* Launch the NM thread for the mounted volume */
5386714d8e8SKurt Hackel int dlm_launch_thread(struct dlm_ctxt *dlm)
5396714d8e8SKurt Hackel {
5408e17d16fSSunil Mushran 	mlog(0, "Starting dlm_thread...\n");
5416714d8e8SKurt Hackel 
5425afc44e2SJoseph Qi 	dlm->dlm_thread_task = kthread_run(dlm_thread, dlm, "dlm-%s",
5435afc44e2SJoseph Qi 			dlm->name);
5446714d8e8SKurt Hackel 	if (IS_ERR(dlm->dlm_thread_task)) {
5456714d8e8SKurt Hackel 		mlog_errno(PTR_ERR(dlm->dlm_thread_task));
5466714d8e8SKurt Hackel 		dlm->dlm_thread_task = NULL;
5476714d8e8SKurt Hackel 		return -EINVAL;
5486714d8e8SKurt Hackel 	}
5496714d8e8SKurt Hackel 
5506714d8e8SKurt Hackel 	return 0;
5516714d8e8SKurt Hackel }
5526714d8e8SKurt Hackel 
5536714d8e8SKurt Hackel void dlm_complete_thread(struct dlm_ctxt *dlm)
5546714d8e8SKurt Hackel {
5556714d8e8SKurt Hackel 	if (dlm->dlm_thread_task) {
5568e17d16fSSunil Mushran 		mlog(ML_KTHREAD, "Waiting for dlm thread to exit\n");
5576714d8e8SKurt Hackel 		kthread_stop(dlm->dlm_thread_task);
5586714d8e8SKurt Hackel 		dlm->dlm_thread_task = NULL;
5596714d8e8SKurt Hackel 	}
5606714d8e8SKurt Hackel }
5616714d8e8SKurt Hackel 
5626714d8e8SKurt Hackel static int dlm_dirty_list_empty(struct dlm_ctxt *dlm)
5636714d8e8SKurt Hackel {
5646714d8e8SKurt Hackel 	int empty;
5656714d8e8SKurt Hackel 
5666714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
5676714d8e8SKurt Hackel 	empty = list_empty(&dlm->dirty_list);
5686714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
5696714d8e8SKurt Hackel 
5706714d8e8SKurt Hackel 	return empty;
5716714d8e8SKurt Hackel }
5726714d8e8SKurt Hackel 
5736714d8e8SKurt Hackel static void dlm_flush_asts(struct dlm_ctxt *dlm)
5746714d8e8SKurt Hackel {
5756714d8e8SKurt Hackel 	int ret;
5766714d8e8SKurt Hackel 	struct dlm_lock *lock;
5776714d8e8SKurt Hackel 	struct dlm_lock_resource *res;
5786714d8e8SKurt Hackel 	u8 hi;
5796714d8e8SKurt Hackel 
5806714d8e8SKurt Hackel 	spin_lock(&dlm->ast_lock);
5816714d8e8SKurt Hackel 	while (!list_empty(&dlm->pending_asts)) {
5826714d8e8SKurt Hackel 		lock = list_entry(dlm->pending_asts.next,
5836714d8e8SKurt Hackel 				  struct dlm_lock, ast_list);
5846714d8e8SKurt Hackel 		/* get an extra ref on lock */
5856714d8e8SKurt Hackel 		dlm_lock_get(lock);
5866714d8e8SKurt Hackel 		res = lock->lockres;
5878e17d16fSSunil Mushran 		mlog(0, "%s: res %.*s, Flush AST for lock %u:%llu, type %d, "
5888e17d16fSSunil Mushran 		     "node %u\n", dlm->name, res->lockname.len,
5898e17d16fSSunil Mushran 		     res->lockname.name,
5908e17d16fSSunil Mushran 		     dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
5918e17d16fSSunil Mushran 		     dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)),
5928e17d16fSSunil Mushran 		     lock->ml.type, lock->ml.node);
5936714d8e8SKurt Hackel 
5946714d8e8SKurt Hackel 		BUG_ON(!lock->ast_pending);
5956714d8e8SKurt Hackel 
5966714d8e8SKurt Hackel 		/* remove from list (including ref) */
5976714d8e8SKurt Hackel 		list_del_init(&lock->ast_list);
5986714d8e8SKurt Hackel 		dlm_lock_put(lock);
5996714d8e8SKurt Hackel 		spin_unlock(&dlm->ast_lock);
6006714d8e8SKurt Hackel 
6016714d8e8SKurt Hackel 		if (lock->ml.node != dlm->node_num) {
6026714d8e8SKurt Hackel 			ret = dlm_do_remote_ast(dlm, res, lock);
6036714d8e8SKurt Hackel 			if (ret < 0)
6046714d8e8SKurt Hackel 				mlog_errno(ret);
6056714d8e8SKurt Hackel 		} else
6066714d8e8SKurt Hackel 			dlm_do_local_ast(dlm, res, lock);
6076714d8e8SKurt Hackel 
6086714d8e8SKurt Hackel 		spin_lock(&dlm->ast_lock);
6096714d8e8SKurt Hackel 
6106714d8e8SKurt Hackel 		/* possible that another ast was queued while
6116714d8e8SKurt Hackel 		 * we were delivering the last one */
6126714d8e8SKurt Hackel 		if (!list_empty(&lock->ast_list)) {
6138e17d16fSSunil Mushran 			mlog(0, "%s: res %.*s, AST queued while flushing last "
6148e17d16fSSunil Mushran 			     "one\n", dlm->name, res->lockname.len,
6158e17d16fSSunil Mushran 			     res->lockname.name);
6166714d8e8SKurt Hackel 		} else
6176714d8e8SKurt Hackel 			lock->ast_pending = 0;
6186714d8e8SKurt Hackel 
6196714d8e8SKurt Hackel 		/* drop the extra ref.
6206714d8e8SKurt Hackel 		 * this may drop it completely. */
6216714d8e8SKurt Hackel 		dlm_lock_put(lock);
6226714d8e8SKurt Hackel 		dlm_lockres_release_ast(dlm, res);
6236714d8e8SKurt Hackel 	}
6246714d8e8SKurt Hackel 
6256714d8e8SKurt Hackel 	while (!list_empty(&dlm->pending_basts)) {
6266714d8e8SKurt Hackel 		lock = list_entry(dlm->pending_basts.next,
6276714d8e8SKurt Hackel 				  struct dlm_lock, bast_list);
6286714d8e8SKurt Hackel 		/* get an extra ref on lock */
6296714d8e8SKurt Hackel 		dlm_lock_get(lock);
6306714d8e8SKurt Hackel 		res = lock->lockres;
6316714d8e8SKurt Hackel 
6326714d8e8SKurt Hackel 		BUG_ON(!lock->bast_pending);
6336714d8e8SKurt Hackel 
6346714d8e8SKurt Hackel 		/* get the highest blocked lock, and reset */
6356714d8e8SKurt Hackel 		spin_lock(&lock->spinlock);
6366714d8e8SKurt Hackel 		BUG_ON(lock->ml.highest_blocked <= LKM_IVMODE);
6376714d8e8SKurt Hackel 		hi = lock->ml.highest_blocked;
6386714d8e8SKurt Hackel 		lock->ml.highest_blocked = LKM_IVMODE;
6396714d8e8SKurt Hackel 		spin_unlock(&lock->spinlock);
6406714d8e8SKurt Hackel 
6416714d8e8SKurt Hackel 		/* remove from list (including ref) */
6426714d8e8SKurt Hackel 		list_del_init(&lock->bast_list);
6436714d8e8SKurt Hackel 		dlm_lock_put(lock);
6446714d8e8SKurt Hackel 		spin_unlock(&dlm->ast_lock);
6456714d8e8SKurt Hackel 
6468e17d16fSSunil Mushran 		mlog(0, "%s: res %.*s, Flush BAST for lock %u:%llu, "
6478e17d16fSSunil Mushran 		     "blocked %d, node %u\n",
6488e17d16fSSunil Mushran 		     dlm->name, res->lockname.len, res->lockname.name,
6498e17d16fSSunil Mushran 		     dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
6508e17d16fSSunil Mushran 		     dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)),
6518e17d16fSSunil Mushran 		     hi, lock->ml.node);
6526714d8e8SKurt Hackel 
6536714d8e8SKurt Hackel 		if (lock->ml.node != dlm->node_num) {
6546714d8e8SKurt Hackel 			ret = dlm_send_proxy_bast(dlm, res, lock, hi);
6556714d8e8SKurt Hackel 			if (ret < 0)
6566714d8e8SKurt Hackel 				mlog_errno(ret);
6576714d8e8SKurt Hackel 		} else
6586714d8e8SKurt Hackel 			dlm_do_local_bast(dlm, res, lock, hi);
6596714d8e8SKurt Hackel 
6606714d8e8SKurt Hackel 		spin_lock(&dlm->ast_lock);
6616714d8e8SKurt Hackel 
6626714d8e8SKurt Hackel 		/* possible that another bast was queued while
6636714d8e8SKurt Hackel 		 * we were delivering the last one */
6646714d8e8SKurt Hackel 		if (!list_empty(&lock->bast_list)) {
6658e17d16fSSunil Mushran 			mlog(0, "%s: res %.*s, BAST queued while flushing last "
6668e17d16fSSunil Mushran 			     "one\n", dlm->name, res->lockname.len,
6678e17d16fSSunil Mushran 			     res->lockname.name);
6686714d8e8SKurt Hackel 		} else
6696714d8e8SKurt Hackel 			lock->bast_pending = 0;
6706714d8e8SKurt Hackel 
6716714d8e8SKurt Hackel 		/* drop the extra ref.
6726714d8e8SKurt Hackel 		 * this may drop it completely. */
6736714d8e8SKurt Hackel 		dlm_lock_put(lock);
6746714d8e8SKurt Hackel 		dlm_lockres_release_ast(dlm, res);
6756714d8e8SKurt Hackel 	}
6766714d8e8SKurt Hackel 	wake_up(&dlm->ast_wq);
6776714d8e8SKurt Hackel 	spin_unlock(&dlm->ast_lock);
6786714d8e8SKurt Hackel }
6796714d8e8SKurt Hackel 
6806714d8e8SKurt Hackel 
6816714d8e8SKurt Hackel #define DLM_THREAD_TIMEOUT_MS (4 * 1000)
6826714d8e8SKurt Hackel #define DLM_THREAD_MAX_DIRTY  100
6836714d8e8SKurt Hackel #define DLM_THREAD_MAX_ASTS   10
6846714d8e8SKurt Hackel 
6856714d8e8SKurt Hackel static int dlm_thread(void *data)
6866714d8e8SKurt Hackel {
6876714d8e8SKurt Hackel 	struct dlm_lock_resource *res;
6886714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
6896714d8e8SKurt Hackel 	unsigned long timeout = msecs_to_jiffies(DLM_THREAD_TIMEOUT_MS);
6906714d8e8SKurt Hackel 
6916714d8e8SKurt Hackel 	mlog(0, "dlm thread running for %s...\n", dlm->name);
6926714d8e8SKurt Hackel 
6936714d8e8SKurt Hackel 	while (!kthread_should_stop()) {
6946714d8e8SKurt Hackel 		int n = DLM_THREAD_MAX_DIRTY;
6956714d8e8SKurt Hackel 
6966714d8e8SKurt Hackel 		/* dlm_shutting_down is very point-in-time, but that
6976714d8e8SKurt Hackel 		 * doesn't matter as we'll just loop back around if we
6986714d8e8SKurt Hackel 		 * get false on the leading edge of a state
6996714d8e8SKurt Hackel 		 * transition. */
7006714d8e8SKurt Hackel 		dlm_run_purge_list(dlm, dlm_shutting_down(dlm));
7016714d8e8SKurt Hackel 
7026714d8e8SKurt Hackel 		/* We really don't want to hold dlm->spinlock while
7036714d8e8SKurt Hackel 		 * calling dlm_shuffle_lists on each lockres that
7046714d8e8SKurt Hackel 		 * needs to have its queues adjusted and AST/BASTs
7056714d8e8SKurt Hackel 		 * run.  So let's pull each entry off the dirty_list
7066714d8e8SKurt Hackel 		 * and drop dlm->spinlock ASAP.  Once off the list,
7076714d8e8SKurt Hackel 		 * res->spinlock needs to be taken again to protect
7086714d8e8SKurt Hackel 		 * the queues while calling dlm_shuffle_lists.  */
7096714d8e8SKurt Hackel 		spin_lock(&dlm->spinlock);
7106714d8e8SKurt Hackel 		while (!list_empty(&dlm->dirty_list)) {
7116714d8e8SKurt Hackel 			int delay = 0;
7126714d8e8SKurt Hackel 			res = list_entry(dlm->dirty_list.next,
7136714d8e8SKurt Hackel 					 struct dlm_lock_resource, dirty);
7146714d8e8SKurt Hackel 
7156714d8e8SKurt Hackel 			/* peel a lockres off, remove it from the list,
7166714d8e8SKurt Hackel 			 * unset the dirty flag and drop the dlm lock */
7176714d8e8SKurt Hackel 			BUG_ON(!res);
7186714d8e8SKurt Hackel 			dlm_lockres_get(res);
7196714d8e8SKurt Hackel 
7206714d8e8SKurt Hackel 			spin_lock(&res->spinlock);
721ddc09c8dSKurt Hackel 			/* We clear the DLM_LOCK_RES_DIRTY state once we shuffle lists below */
7226714d8e8SKurt Hackel 			list_del_init(&res->dirty);
7236714d8e8SKurt Hackel 			spin_unlock(&res->spinlock);
7246714d8e8SKurt Hackel 			spin_unlock(&dlm->spinlock);
7256ff06a93SKurt Hackel 			/* Drop dirty_list ref */
7266ff06a93SKurt Hackel 			dlm_lockres_put(res);
7276714d8e8SKurt Hackel 
7286714d8e8SKurt Hackel 		 	/* lockres can be re-dirtied/re-added to the
7296714d8e8SKurt Hackel 			 * dirty_list in this gap, but that is ok */
7306714d8e8SKurt Hackel 
731d9ef7522SWengang Wang 			spin_lock(&dlm->ast_lock);
7326714d8e8SKurt Hackel 			spin_lock(&res->spinlock);
7336714d8e8SKurt Hackel 			if (res->owner != dlm->node_num) {
7346714d8e8SKurt Hackel 				__dlm_print_one_lock_resource(res);
7358e17d16fSSunil Mushran 				mlog(ML_ERROR, "%s: inprog %d, mig %d, reco %d,"
7368e17d16fSSunil Mushran 				     " dirty %d\n", dlm->name,
7378e17d16fSSunil Mushran 				     !!(res->state & DLM_LOCK_RES_IN_PROGRESS),
7388e17d16fSSunil Mushran 				     !!(res->state & DLM_LOCK_RES_MIGRATING),
7398e17d16fSSunil Mushran 				     !!(res->state & DLM_LOCK_RES_RECOVERING),
7408e17d16fSSunil Mushran 				     !!(res->state & DLM_LOCK_RES_DIRTY));
7416714d8e8SKurt Hackel 			}
7426714d8e8SKurt Hackel 			BUG_ON(res->owner != dlm->node_num);
7436714d8e8SKurt Hackel 
7446714d8e8SKurt Hackel 			/* it is now ok to move lockreses in these states
7456714d8e8SKurt Hackel 			 * to the dirty list, assuming that they will only be
7466714d8e8SKurt Hackel 			 * dirty for a short while. */
747ddc09c8dSKurt Hackel 			BUG_ON(res->state & DLM_LOCK_RES_MIGRATING);
7486714d8e8SKurt Hackel 			if (res->state & (DLM_LOCK_RES_IN_PROGRESS |
749814ce694SJiufei Xue 					  DLM_LOCK_RES_RECOVERING |
750814ce694SJiufei Xue 					  DLM_LOCK_RES_RECOVERY_WAITING)) {
7516714d8e8SKurt Hackel 				/* move it to the tail and keep going */
752ddc09c8dSKurt Hackel 				res->state &= ~DLM_LOCK_RES_DIRTY;
7536714d8e8SKurt Hackel 				spin_unlock(&res->spinlock);
754d9ef7522SWengang Wang 				spin_unlock(&dlm->ast_lock);
7558e17d16fSSunil Mushran 				mlog(0, "%s: res %.*s, inprogress, delay list "
7568e17d16fSSunil Mushran 				     "shuffle, state %d\n", dlm->name,
7576714d8e8SKurt Hackel 				     res->lockname.len, res->lockname.name,
7586714d8e8SKurt Hackel 				     res->state);
7596714d8e8SKurt Hackel 				delay = 1;
7606714d8e8SKurt Hackel 				goto in_progress;
7616714d8e8SKurt Hackel 			}
7626714d8e8SKurt Hackel 
7636714d8e8SKurt Hackel 			/* at this point the lockres is not migrating/
7646714d8e8SKurt Hackel 			 * recovering/in-progress.  we have the lockres
7656714d8e8SKurt Hackel 			 * spinlock and do NOT have the dlm lock.
7666714d8e8SKurt Hackel 			 * safe to reserve/queue asts and run the lists. */
7676714d8e8SKurt Hackel 
7686714d8e8SKurt Hackel 			/* called while holding lockres lock */
7696714d8e8SKurt Hackel 			dlm_shuffle_lists(dlm, res);
770ddc09c8dSKurt Hackel 			res->state &= ~DLM_LOCK_RES_DIRTY;
7716714d8e8SKurt Hackel 			spin_unlock(&res->spinlock);
772d9ef7522SWengang Wang 			spin_unlock(&dlm->ast_lock);
7736714d8e8SKurt Hackel 
7746714d8e8SKurt Hackel 			dlm_lockres_calc_usage(dlm, res);
7756714d8e8SKurt Hackel 
7766714d8e8SKurt Hackel in_progress:
7776714d8e8SKurt Hackel 
7786714d8e8SKurt Hackel 			spin_lock(&dlm->spinlock);
7796714d8e8SKurt Hackel 			/* if the lock was in-progress, stick
7806714d8e8SKurt Hackel 			 * it on the back of the list */
7816714d8e8SKurt Hackel 			if (delay) {
7826714d8e8SKurt Hackel 				spin_lock(&res->spinlock);
783ddc09c8dSKurt Hackel 				__dlm_dirty_lockres(dlm, res);
7846714d8e8SKurt Hackel 				spin_unlock(&res->spinlock);
7856714d8e8SKurt Hackel 			}
7866714d8e8SKurt Hackel 			dlm_lockres_put(res);
7876714d8e8SKurt Hackel 
7886714d8e8SKurt Hackel 			/* unlikely, but we may need to give time to
7896714d8e8SKurt Hackel 			 * other tasks */
7906714d8e8SKurt Hackel 			if (!--n) {
7918e17d16fSSunil Mushran 				mlog(0, "%s: Throttling dlm thread\n",
7928e17d16fSSunil Mushran 				     dlm->name);
7936714d8e8SKurt Hackel 				break;
7946714d8e8SKurt Hackel 			}
7956714d8e8SKurt Hackel 		}
7966714d8e8SKurt Hackel 
7976714d8e8SKurt Hackel 		spin_unlock(&dlm->spinlock);
7986714d8e8SKurt Hackel 		dlm_flush_asts(dlm);
7996714d8e8SKurt Hackel 
8006714d8e8SKurt Hackel 		/* yield and continue right away if there is more work to do */
8016714d8e8SKurt Hackel 		if (!n) {
802f85cd47aSKurt Hackel 			cond_resched();
8036714d8e8SKurt Hackel 			continue;
8046714d8e8SKurt Hackel 		}
8056714d8e8SKurt Hackel 
8066714d8e8SKurt Hackel 		wait_event_interruptible_timeout(dlm->dlm_thread_wq,
8076714d8e8SKurt Hackel 						 !dlm_dirty_list_empty(dlm) ||
8086714d8e8SKurt Hackel 						 kthread_should_stop(),
8096714d8e8SKurt Hackel 						 timeout);
8106714d8e8SKurt Hackel 	}
8116714d8e8SKurt Hackel 
8126714d8e8SKurt Hackel 	mlog(0, "quitting DLM thread\n");
8136714d8e8SKurt Hackel 	return 0;
8146714d8e8SKurt Hackel }
815