xref: /openbmc/linux/fs/ocfs2/dlm/dlmthread.c (revision b5c7e7ec)
1328970deSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2fa60ce2cSMasahiro Yamada /*
36714d8e8SKurt Hackel  * dlmthread.c
46714d8e8SKurt Hackel  *
56714d8e8SKurt Hackel  * standalone DLM module
66714d8e8SKurt Hackel  *
76714d8e8SKurt Hackel  * Copyright (C) 2004 Oracle.  All rights reserved.
86714d8e8SKurt Hackel  */
96714d8e8SKurt Hackel 
106714d8e8SKurt Hackel 
116714d8e8SKurt Hackel #include <linux/module.h>
126714d8e8SKurt Hackel #include <linux/fs.h>
136714d8e8SKurt Hackel #include <linux/types.h>
146714d8e8SKurt Hackel #include <linux/highmem.h>
156714d8e8SKurt Hackel #include <linux/init.h>
166714d8e8SKurt Hackel #include <linux/sysctl.h>
176714d8e8SKurt Hackel #include <linux/random.h>
186714d8e8SKurt Hackel #include <linux/blkdev.h>
196714d8e8SKurt Hackel #include <linux/socket.h>
206714d8e8SKurt Hackel #include <linux/inet.h>
216714d8e8SKurt Hackel #include <linux/timer.h>
226714d8e8SKurt Hackel #include <linux/kthread.h>
238d79d088SKurt Hackel #include <linux/delay.h>
246714d8e8SKurt Hackel 
256714d8e8SKurt Hackel 
26ca322fb6SMasahiro Yamada #include "../cluster/heartbeat.h"
27ca322fb6SMasahiro Yamada #include "../cluster/nodemanager.h"
28ca322fb6SMasahiro Yamada #include "../cluster/tcp.h"
296714d8e8SKurt Hackel 
306714d8e8SKurt Hackel #include "dlmapi.h"
316714d8e8SKurt Hackel #include "dlmcommon.h"
326714d8e8SKurt Hackel #include "dlmdomain.h"
336714d8e8SKurt Hackel 
346714d8e8SKurt Hackel #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_THREAD)
35ca322fb6SMasahiro Yamada #include "../cluster/masklog.h"
366714d8e8SKurt Hackel 
376714d8e8SKurt Hackel static int dlm_thread(void *data);
386714d8e8SKurt Hackel static void dlm_flush_asts(struct dlm_ctxt *dlm);
396714d8e8SKurt Hackel 
406714d8e8SKurt Hackel /* will exit holding res->spinlock, but may drop in function */
416714d8e8SKurt Hackel /* waits until flags are cleared on res->state */
__dlm_wait_on_lockres_flags(struct dlm_lock_resource * res,int flags)426714d8e8SKurt Hackel void __dlm_wait_on_lockres_flags(struct dlm_lock_resource *res, int flags)
436714d8e8SKurt Hackel {
446714d8e8SKurt Hackel 	DECLARE_WAITQUEUE(wait, current);
456714d8e8SKurt Hackel 
466714d8e8SKurt Hackel 	assert_spin_locked(&res->spinlock);
476714d8e8SKurt Hackel 
486714d8e8SKurt Hackel 	add_wait_queue(&res->wq, &wait);
496714d8e8SKurt Hackel repeat:
506714d8e8SKurt Hackel 	set_current_state(TASK_UNINTERRUPTIBLE);
516714d8e8SKurt Hackel 	if (res->state & flags) {
526714d8e8SKurt Hackel 		spin_unlock(&res->spinlock);
536714d8e8SKurt Hackel 		schedule();
546714d8e8SKurt Hackel 		spin_lock(&res->spinlock);
556714d8e8SKurt Hackel 		goto repeat;
566714d8e8SKurt Hackel 	}
576714d8e8SKurt Hackel 	remove_wait_queue(&res->wq, &wait);
585c2c9d38SMilind Arun Choudhary 	__set_current_state(TASK_RUNNING);
596714d8e8SKurt Hackel }
606714d8e8SKurt Hackel 
__dlm_lockres_has_locks(struct dlm_lock_resource * res)61ba2bf218SKurt Hackel int __dlm_lockres_has_locks(struct dlm_lock_resource *res)
626714d8e8SKurt Hackel {
636714d8e8SKurt Hackel 	if (list_empty(&res->granted) &&
646714d8e8SKurt Hackel 	    list_empty(&res->converting) &&
65ba2bf218SKurt Hackel 	    list_empty(&res->blocked))
66ba2bf218SKurt Hackel 		return 0;
676714d8e8SKurt Hackel 	return 1;
68ba2bf218SKurt Hackel }
69ba2bf218SKurt Hackel 
70ba2bf218SKurt Hackel /* "unused": the lockres has no locks, is not on the dirty list,
71ba2bf218SKurt Hackel  * has no inflight locks (in the gap between mastery and acquiring
72ba2bf218SKurt Hackel  * the first lock), and has no bits in its refmap.
73ba2bf218SKurt Hackel  * truly ready to be freed. */
__dlm_lockres_unused(struct dlm_lock_resource * res)74ba2bf218SKurt Hackel int __dlm_lockres_unused(struct dlm_lock_resource *res)
75ba2bf218SKurt Hackel {
76a524812bSWengang Wang 	int bit;
77a524812bSWengang Wang 
78ff0a522eSSunil Mushran 	assert_spin_locked(&res->spinlock);
79ff0a522eSSunil Mushran 
80a524812bSWengang Wang 	if (__dlm_lockres_has_locks(res))
81a524812bSWengang Wang 		return 0;
82a524812bSWengang Wang 
83ff0a522eSSunil Mushran 	/* Locks are in the process of being created */
84ff0a522eSSunil Mushran 	if (res->inflight_locks)
85ff0a522eSSunil Mushran 		return 0;
86ff0a522eSSunil Mushran 
87a524812bSWengang Wang 	if (!list_empty(&res->dirty) || res->state & DLM_LOCK_RES_DIRTY)
88a524812bSWengang Wang 		return 0;
89a524812bSWengang Wang 
90814ce694SJiufei Xue 	if (res->state & (DLM_LOCK_RES_RECOVERING|
91814ce694SJiufei Xue 			DLM_LOCK_RES_RECOVERY_WAITING))
92a524812bSWengang Wang 		return 0;
93a524812bSWengang Wang 
94ff0a522eSSunil Mushran 	/* Another node has this resource with this node as the master */
95*b5c7e7ecSYury Norov 	bit = find_first_bit(res->refmap, O2NM_MAX_NODES);
96a524812bSWengang Wang 	if (bit < O2NM_MAX_NODES)
97a524812bSWengang Wang 		return 0;
98a524812bSWengang Wang 
99ba2bf218SKurt Hackel 	return 1;
100ba2bf218SKurt Hackel }
1016714d8e8SKurt Hackel 
1026714d8e8SKurt Hackel 
1036714d8e8SKurt Hackel /* Call whenever you may have added or deleted something from one of
1046714d8e8SKurt Hackel  * the lockres queue's. This will figure out whether it belongs on the
1056714d8e8SKurt Hackel  * unused list or not and does the appropriate thing. */
__dlm_lockres_calc_usage(struct dlm_ctxt * dlm,struct dlm_lock_resource * res)1066714d8e8SKurt Hackel void __dlm_lockres_calc_usage(struct dlm_ctxt *dlm,
1076714d8e8SKurt Hackel 			      struct dlm_lock_resource *res)
1086714d8e8SKurt Hackel {
1096714d8e8SKurt Hackel 	assert_spin_locked(&dlm->spinlock);
1106714d8e8SKurt Hackel 	assert_spin_locked(&res->spinlock);
1116714d8e8SKurt Hackel 
1126714d8e8SKurt Hackel 	if (__dlm_lockres_unused(res)){
1136714d8e8SKurt Hackel 		if (list_empty(&res->purge)) {
1148e17d16fSSunil Mushran 			mlog(0, "%s: Adding res %.*s to purge list\n",
1158e17d16fSSunil Mushran 			     dlm->name, res->lockname.len, res->lockname.name);
1166714d8e8SKurt Hackel 
1176714d8e8SKurt Hackel 			res->last_used = jiffies;
118ba2bf218SKurt Hackel 			dlm_lockres_get(res);
1196714d8e8SKurt Hackel 			list_add_tail(&res->purge, &dlm->purge_list);
1206714d8e8SKurt Hackel 			dlm->purge_count++;
1216714d8e8SKurt Hackel 		}
1226714d8e8SKurt Hackel 	} else if (!list_empty(&res->purge)) {
1238e17d16fSSunil Mushran 		mlog(0, "%s: Removing res %.*s from purge list\n",
1248e17d16fSSunil Mushran 		     dlm->name, res->lockname.len, res->lockname.name);
1256714d8e8SKurt Hackel 
1266714d8e8SKurt Hackel 		list_del_init(&res->purge);
127ba2bf218SKurt Hackel 		dlm_lockres_put(res);
1286714d8e8SKurt Hackel 		dlm->purge_count--;
1296714d8e8SKurt Hackel 	}
1306714d8e8SKurt Hackel }
1316714d8e8SKurt Hackel 
dlm_lockres_calc_usage(struct dlm_ctxt * dlm,struct dlm_lock_resource * res)1326714d8e8SKurt Hackel void dlm_lockres_calc_usage(struct dlm_ctxt *dlm,
1336714d8e8SKurt Hackel 			    struct dlm_lock_resource *res)
1346714d8e8SKurt Hackel {
1356714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
1366714d8e8SKurt Hackel 	spin_lock(&res->spinlock);
1376714d8e8SKurt Hackel 
1386714d8e8SKurt Hackel 	__dlm_lockres_calc_usage(dlm, res);
1396714d8e8SKurt Hackel 
1406714d8e8SKurt Hackel 	spin_unlock(&res->spinlock);
1416714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
1426714d8e8SKurt Hackel }
1436714d8e8SKurt Hackel 
144ee8f7fcbSpiaojun /*
145ee8f7fcbSpiaojun  * Do the real purge work:
146ee8f7fcbSpiaojun  *     unhash the lockres, and
147ee8f7fcbSpiaojun  *     clear flag DLM_LOCK_RES_DROPPING_REF.
148ee8f7fcbSpiaojun  * It requires dlm and lockres spinlock to be taken.
149ee8f7fcbSpiaojun  */
__dlm_do_purge_lockres(struct dlm_ctxt * dlm,struct dlm_lock_resource * res)150ee8f7fcbSpiaojun void __dlm_do_purge_lockres(struct dlm_ctxt *dlm,
151ee8f7fcbSpiaojun 		struct dlm_lock_resource *res)
152ee8f7fcbSpiaojun {
153ee8f7fcbSpiaojun 	assert_spin_locked(&dlm->spinlock);
154ee8f7fcbSpiaojun 	assert_spin_locked(&res->spinlock);
155ee8f7fcbSpiaojun 
156ee8f7fcbSpiaojun 	if (!list_empty(&res->purge)) {
157ee8f7fcbSpiaojun 		mlog(0, "%s: Removing res %.*s from purgelist\n",
158ee8f7fcbSpiaojun 		     dlm->name, res->lockname.len, res->lockname.name);
159ee8f7fcbSpiaojun 		list_del_init(&res->purge);
160ee8f7fcbSpiaojun 		dlm_lockres_put(res);
161ee8f7fcbSpiaojun 		dlm->purge_count--;
162ee8f7fcbSpiaojun 	}
163ee8f7fcbSpiaojun 
164ee8f7fcbSpiaojun 	if (!__dlm_lockres_unused(res)) {
165ee8f7fcbSpiaojun 		mlog(ML_ERROR, "%s: res %.*s in use after deref\n",
166ee8f7fcbSpiaojun 		     dlm->name, res->lockname.len, res->lockname.name);
167ee8f7fcbSpiaojun 		__dlm_print_one_lock_resource(res);
168ee8f7fcbSpiaojun 		BUG();
169ee8f7fcbSpiaojun 	}
170ee8f7fcbSpiaojun 
171ee8f7fcbSpiaojun 	__dlm_unhash_lockres(dlm, res);
172ee8f7fcbSpiaojun 
173ee8f7fcbSpiaojun 	spin_lock(&dlm->track_lock);
174ee8f7fcbSpiaojun 	if (!list_empty(&res->tracking))
175ee8f7fcbSpiaojun 		list_del_init(&res->tracking);
176ee8f7fcbSpiaojun 	else {
177ee8f7fcbSpiaojun 		mlog(ML_ERROR, "%s: Resource %.*s not on the Tracking list\n",
178ee8f7fcbSpiaojun 		     dlm->name, res->lockname.len, res->lockname.name);
179ee8f7fcbSpiaojun 		__dlm_print_one_lock_resource(res);
180ee8f7fcbSpiaojun 	}
181ee8f7fcbSpiaojun 	spin_unlock(&dlm->track_lock);
182ee8f7fcbSpiaojun 
183ee8f7fcbSpiaojun 	/*
184ee8f7fcbSpiaojun 	 * lockres is not in the hash now. drop the flag and wake up
185ee8f7fcbSpiaojun 	 * any processes waiting in dlm_get_lock_resource.
186ee8f7fcbSpiaojun 	 */
187ee8f7fcbSpiaojun 	res->state &= ~DLM_LOCK_RES_DROPPING_REF;
188ee8f7fcbSpiaojun }
189ee8f7fcbSpiaojun 
dlm_purge_lockres(struct dlm_ctxt * dlm,struct dlm_lock_resource * res)1907beaf243SSrinivas Eeda static void dlm_purge_lockres(struct dlm_ctxt *dlm,
191faf0ec9fSAdrian Bunk 			     struct dlm_lock_resource *res)
1926714d8e8SKurt Hackel {
1936714d8e8SKurt Hackel 	int master;
194ba2bf218SKurt Hackel 	int ret = 0;
1956714d8e8SKurt Hackel 
1967beaf243SSrinivas Eeda 	assert_spin_locked(&dlm->spinlock);
1977beaf243SSrinivas Eeda 	assert_spin_locked(&res->spinlock);
198516b7e52SSunil Mushran 
199ba2bf218SKurt Hackel 	master = (res->owner == dlm->node_num);
200516b7e52SSunil Mushran 
2018e17d16fSSunil Mushran 	mlog(0, "%s: Purging res %.*s, master %d\n", dlm->name,
2028e17d16fSSunil Mushran 	     res->lockname.len, res->lockname.name, master);
203ba2bf218SKurt Hackel 
204ba2bf218SKurt Hackel 	if (!master) {
205309e9191Spiaojun 		if (res->state & DLM_LOCK_RES_DROPPING_REF) {
206ee8f7fcbSpiaojun 			mlog(ML_NOTICE, "%s: res %.*s already in DLM_LOCK_RES_DROPPING_REF state\n",
207ee8f7fcbSpiaojun 				dlm->name, res->lockname.len, res->lockname.name);
208309e9191Spiaojun 			spin_unlock(&res->spinlock);
209309e9191Spiaojun 			return;
210309e9191Spiaojun 		}
211309e9191Spiaojun 
2127beaf243SSrinivas Eeda 		res->state |= DLM_LOCK_RES_DROPPING_REF;
213c824c3c7SSunil Mushran 		/* drop spinlock...  retake below */
2147beaf243SSrinivas Eeda 		spin_unlock(&res->spinlock);
215c824c3c7SSunil Mushran 		spin_unlock(&dlm->spinlock);
216c824c3c7SSunil Mushran 
2173b8118cfSKurt Hackel 		spin_lock(&res->spinlock);
2183b8118cfSKurt Hackel 		/* This ensures that clear refmap is sent after the set */
2197dc102b7SSunil Mushran 		__dlm_wait_on_lockres_flags(res, DLM_LOCK_RES_SETREF_INPROG);
2203b8118cfSKurt Hackel 		spin_unlock(&res->spinlock);
221c824c3c7SSunil Mushran 
222ba2bf218SKurt Hackel 		/* clear our bit from the master's refmap, ignore errors */
223ba2bf218SKurt Hackel 		ret = dlm_drop_lockres_ref(dlm, res);
224ba2bf218SKurt Hackel 		if (ret < 0) {
225ba2bf218SKurt Hackel 			if (!dlm_is_host_down(ret))
2266714d8e8SKurt Hackel 				BUG();
2276714d8e8SKurt Hackel 		}
2286714d8e8SKurt Hackel 		spin_lock(&dlm->spinlock);
2297beaf243SSrinivas Eeda 		spin_lock(&res->spinlock);
230ba2bf218SKurt Hackel 	}
2316714d8e8SKurt Hackel 
232ba2bf218SKurt Hackel 	if (!list_empty(&res->purge)) {
2338e17d16fSSunil Mushran 		mlog(0, "%s: Removing res %.*s from purgelist, master %d\n",
2348e17d16fSSunil Mushran 		     dlm->name, res->lockname.len, res->lockname.name, master);
235ba2bf218SKurt Hackel 		list_del_init(&res->purge);
236ba2bf218SKurt Hackel 		dlm_lockres_put(res);
2376714d8e8SKurt Hackel 		dlm->purge_count--;
2387beaf243SSrinivas Eeda 	}
2397beaf243SSrinivas Eeda 
240309e9191Spiaojun 	if (!master && ret == DLM_DEREF_RESPONSE_INPROG) {
241309e9191Spiaojun 		mlog(0, "%s: deref %.*s in progress\n",
242842b90b6Sxuejiufei 			dlm->name, res->lockname.len, res->lockname.name);
243842b90b6Sxuejiufei 		spin_unlock(&res->spinlock);
244842b90b6Sxuejiufei 		return;
245842b90b6Sxuejiufei 	}
246842b90b6Sxuejiufei 
2477beaf243SSrinivas Eeda 	if (!__dlm_lockres_unused(res)) {
2488e17d16fSSunil Mushran 		mlog(ML_ERROR, "%s: res %.*s in use after deref\n",
2497beaf243SSrinivas Eeda 		     dlm->name, res->lockname.len, res->lockname.name);
2507beaf243SSrinivas Eeda 		__dlm_print_one_lock_resource(res);
2517beaf243SSrinivas Eeda 		BUG();
2527beaf243SSrinivas Eeda 	}
25383e32d90SWengang Wang 
254e9f0b6a6SSunil Mushran 	__dlm_unhash_lockres(dlm, res);
255ba2bf218SKurt Hackel 
256f57a22ddSYiwen Jiang 	spin_lock(&dlm->track_lock);
257f57a22ddSYiwen Jiang 	if (!list_empty(&res->tracking))
258f57a22ddSYiwen Jiang 		list_del_init(&res->tracking);
259f57a22ddSYiwen Jiang 	else {
260f57a22ddSYiwen Jiang 		mlog(ML_ERROR, "Resource %.*s not on the Tracking list\n",
261f57a22ddSYiwen Jiang 				res->lockname.len, res->lockname.name);
262f57a22ddSYiwen Jiang 		__dlm_print_one_lock_resource(res);
263f57a22ddSYiwen Jiang 	}
264f57a22ddSYiwen Jiang 	spin_unlock(&dlm->track_lock);
265f57a22ddSYiwen Jiang 
266ba2bf218SKurt Hackel 	/* lockres is not in the hash now.  drop the flag and wake up
267ba2bf218SKurt Hackel 	 * any processes waiting in dlm_get_lock_resource. */
268ba2bf218SKurt Hackel 	if (!master) {
269ba2bf218SKurt Hackel 		res->state &= ~DLM_LOCK_RES_DROPPING_REF;
270ba2bf218SKurt Hackel 		spin_unlock(&res->spinlock);
271ba2bf218SKurt Hackel 		wake_up(&res->wq);
2727beaf243SSrinivas Eeda 	} else
2737beaf243SSrinivas Eeda 		spin_unlock(&res->spinlock);
2748b219809SKurt Hackel }
2758b219809SKurt Hackel 
dlm_run_purge_list(struct dlm_ctxt * dlm,int purge_now)2766714d8e8SKurt Hackel static void dlm_run_purge_list(struct dlm_ctxt *dlm,
2776714d8e8SKurt Hackel 			       int purge_now)
2786714d8e8SKurt Hackel {
2796714d8e8SKurt Hackel 	unsigned int run_max, unused;
2806714d8e8SKurt Hackel 	unsigned long purge_jiffies;
2816714d8e8SKurt Hackel 	struct dlm_lock_resource *lockres;
2826714d8e8SKurt Hackel 
2836714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
2846714d8e8SKurt Hackel 	run_max = dlm->purge_count;
2856714d8e8SKurt Hackel 
2866714d8e8SKurt Hackel 	while(run_max && !list_empty(&dlm->purge_list)) {
2876714d8e8SKurt Hackel 		run_max--;
2886714d8e8SKurt Hackel 
2896714d8e8SKurt Hackel 		lockres = list_entry(dlm->purge_list.next,
2906714d8e8SKurt Hackel 				     struct dlm_lock_resource, purge);
2916714d8e8SKurt Hackel 
2926714d8e8SKurt Hackel 		spin_lock(&lockres->spinlock);
2936714d8e8SKurt Hackel 
2946714d8e8SKurt Hackel 		purge_jiffies = lockres->last_used +
2956714d8e8SKurt Hackel 			msecs_to_jiffies(DLM_PURGE_INTERVAL_MS);
2966714d8e8SKurt Hackel 
2976714d8e8SKurt Hackel 		/* Make sure that we want to be processing this guy at
2986714d8e8SKurt Hackel 		 * this time. */
2996714d8e8SKurt Hackel 		if (!purge_now && time_after(purge_jiffies, jiffies)) {
3006714d8e8SKurt Hackel 			/* Since resources are added to the purge list
3016714d8e8SKurt Hackel 			 * in tail order, we can stop at the first
3026714d8e8SKurt Hackel 			 * unpurgable resource -- anyone added after
3036714d8e8SKurt Hackel 			 * him will have a greater last_used value */
3047beaf243SSrinivas Eeda 			spin_unlock(&lockres->spinlock);
3056714d8e8SKurt Hackel 			break;
3066714d8e8SKurt Hackel 		}
3076714d8e8SKurt Hackel 
3087beaf243SSrinivas Eeda 		/* Status of the lockres *might* change so double
3097beaf243SSrinivas Eeda 		 * check. If the lockres is unused, holding the dlm
3107beaf243SSrinivas Eeda 		 * spinlock will prevent people from getting and more
3117beaf243SSrinivas Eeda 		 * refs on it. */
3127beaf243SSrinivas Eeda 		unused = __dlm_lockres_unused(lockres);
3137beaf243SSrinivas Eeda 		if (!unused ||
314ac4fef4dSXue jiufei 		    (lockres->state & DLM_LOCK_RES_MIGRATING) ||
315ac4fef4dSXue jiufei 		    (lockres->inflight_assert_workers != 0)) {
3168e17d16fSSunil Mushran 			mlog(0, "%s: res %.*s is in use or being remastered, "
317ac4fef4dSXue jiufei 			     "used %d, state %d, assert master workers %u\n",
318ac4fef4dSXue jiufei 			     dlm->name, lockres->lockname.len,
319ac4fef4dSXue jiufei 			     lockres->lockname.name,
320ac4fef4dSXue jiufei 			     !unused, lockres->state,
321ac4fef4dSXue jiufei 			     lockres->inflight_assert_workers);
322a270c6d3SXue jiufei 			list_move_tail(&lockres->purge, &dlm->purge_list);
3237beaf243SSrinivas Eeda 			spin_unlock(&lockres->spinlock);
3247beaf243SSrinivas Eeda 			continue;
3257beaf243SSrinivas Eeda 		}
3267beaf243SSrinivas Eeda 
32778062cb2SSunil Mushran 		dlm_lockres_get(lockres);
3286714d8e8SKurt Hackel 
3297beaf243SSrinivas Eeda 		dlm_purge_lockres(dlm, lockres);
33078062cb2SSunil Mushran 
3313fca0894SSunil Mushran 		dlm_lockres_put(lockres);
3326714d8e8SKurt Hackel 
3336714d8e8SKurt Hackel 		/* Avoid adding any scheduling latencies */
3346714d8e8SKurt Hackel 		cond_resched_lock(&dlm->spinlock);
3356714d8e8SKurt Hackel 	}
3366714d8e8SKurt Hackel 
3376714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
3386714d8e8SKurt Hackel }
3396714d8e8SKurt Hackel 
dlm_shuffle_lists(struct dlm_ctxt * dlm,struct dlm_lock_resource * res)3406714d8e8SKurt Hackel static void dlm_shuffle_lists(struct dlm_ctxt *dlm,
3416714d8e8SKurt Hackel 			      struct dlm_lock_resource *res)
3426714d8e8SKurt Hackel {
3436714d8e8SKurt Hackel 	struct dlm_lock *lock, *target;
3446714d8e8SKurt Hackel 	int can_grant = 1;
3456714d8e8SKurt Hackel 
3468e17d16fSSunil Mushran 	/*
3478e17d16fSSunil Mushran 	 * Because this function is called with the lockres
3486714d8e8SKurt Hackel 	 * spinlock, and because we know that it is not migrating/
3496714d8e8SKurt Hackel 	 * recovering/in-progress, it is fine to reserve asts and
3508e17d16fSSunil Mushran 	 * basts right before queueing them all throughout
3518e17d16fSSunil Mushran 	 */
352d9ef7522SWengang Wang 	assert_spin_locked(&dlm->ast_lock);
3536714d8e8SKurt Hackel 	assert_spin_locked(&res->spinlock);
3546714d8e8SKurt Hackel 	BUG_ON((res->state & (DLM_LOCK_RES_MIGRATING|
3556714d8e8SKurt Hackel 			      DLM_LOCK_RES_RECOVERING|
3566714d8e8SKurt Hackel 			      DLM_LOCK_RES_IN_PROGRESS)));
3576714d8e8SKurt Hackel 
3586714d8e8SKurt Hackel converting:
3596714d8e8SKurt Hackel 	if (list_empty(&res->converting))
3606714d8e8SKurt Hackel 		goto blocked;
3618e17d16fSSunil Mushran 	mlog(0, "%s: res %.*s has locks on the convert queue\n", dlm->name,
3628e17d16fSSunil Mushran 	     res->lockname.len, res->lockname.name);
3636714d8e8SKurt Hackel 
3646714d8e8SKurt Hackel 	target = list_entry(res->converting.next, struct dlm_lock, list);
3656714d8e8SKurt Hackel 	if (target->ml.convert_type == LKM_IVMODE) {
3668e17d16fSSunil Mushran 		mlog(ML_ERROR, "%s: res %.*s converting lock to invalid mode\n",
3678e17d16fSSunil Mushran 		     dlm->name, res->lockname.len, res->lockname.name);
3686714d8e8SKurt Hackel 		BUG();
3696714d8e8SKurt Hackel 	}
370df53cd3bSDong Fang 	list_for_each_entry(lock, &res->granted, list) {
3716714d8e8SKurt Hackel 		if (lock==target)
3726714d8e8SKurt Hackel 			continue;
3736714d8e8SKurt Hackel 		if (!dlm_lock_compatible(lock->ml.type,
3746714d8e8SKurt Hackel 					 target->ml.convert_type)) {
3756714d8e8SKurt Hackel 			can_grant = 0;
3766714d8e8SKurt Hackel 			/* queue the BAST if not already */
3776714d8e8SKurt Hackel 			if (lock->ml.highest_blocked == LKM_IVMODE) {
3786714d8e8SKurt Hackel 				__dlm_lockres_reserve_ast(res);
379d9ef7522SWengang Wang 				__dlm_queue_bast(dlm, lock);
3806714d8e8SKurt Hackel 			}
3816714d8e8SKurt Hackel 			/* update the highest_blocked if needed */
3826714d8e8SKurt Hackel 			if (lock->ml.highest_blocked < target->ml.convert_type)
3836714d8e8SKurt Hackel 				lock->ml.highest_blocked =
3846714d8e8SKurt Hackel 					target->ml.convert_type;
3856714d8e8SKurt Hackel 		}
3866714d8e8SKurt Hackel 	}
387df53cd3bSDong Fang 
388df53cd3bSDong Fang 	list_for_each_entry(lock, &res->converting, list) {
3896714d8e8SKurt Hackel 		if (lock==target)
3906714d8e8SKurt Hackel 			continue;
3916714d8e8SKurt Hackel 		if (!dlm_lock_compatible(lock->ml.type,
3926714d8e8SKurt Hackel 					 target->ml.convert_type)) {
3936714d8e8SKurt Hackel 			can_grant = 0;
3946714d8e8SKurt Hackel 			if (lock->ml.highest_blocked == LKM_IVMODE) {
3956714d8e8SKurt Hackel 				__dlm_lockres_reserve_ast(res);
396d9ef7522SWengang Wang 				__dlm_queue_bast(dlm, lock);
3976714d8e8SKurt Hackel 			}
3986714d8e8SKurt Hackel 			if (lock->ml.highest_blocked < target->ml.convert_type)
3996714d8e8SKurt Hackel 				lock->ml.highest_blocked =
4006714d8e8SKurt Hackel 					target->ml.convert_type;
4016714d8e8SKurt Hackel 		}
4026714d8e8SKurt Hackel 	}
4036714d8e8SKurt Hackel 
4046714d8e8SKurt Hackel 	/* we can convert the lock */
4056714d8e8SKurt Hackel 	if (can_grant) {
4066714d8e8SKurt Hackel 		spin_lock(&target->spinlock);
4076714d8e8SKurt Hackel 		BUG_ON(target->ml.highest_blocked != LKM_IVMODE);
4086714d8e8SKurt Hackel 
4098e17d16fSSunil Mushran 		mlog(0, "%s: res %.*s, AST for Converting lock %u:%llu, type "
4108e17d16fSSunil Mushran 		     "%d => %d, node %u\n", dlm->name, res->lockname.len,
4118e17d16fSSunil Mushran 		     res->lockname.name,
4128e17d16fSSunil Mushran 		     dlm_get_lock_cookie_node(be64_to_cpu(target->ml.cookie)),
4138e17d16fSSunil Mushran 		     dlm_get_lock_cookie_seq(be64_to_cpu(target->ml.cookie)),
4148e17d16fSSunil Mushran 		     target->ml.type,
4156714d8e8SKurt Hackel 		     target->ml.convert_type, target->ml.node);
4166714d8e8SKurt Hackel 
4176714d8e8SKurt Hackel 		target->ml.type = target->ml.convert_type;
4186714d8e8SKurt Hackel 		target->ml.convert_type = LKM_IVMODE;
419f116629dSAkinobu Mita 		list_move_tail(&target->list, &res->granted);
4206714d8e8SKurt Hackel 
4216714d8e8SKurt Hackel 		BUG_ON(!target->lksb);
4226714d8e8SKurt Hackel 		target->lksb->status = DLM_NORMAL;
4236714d8e8SKurt Hackel 
4246714d8e8SKurt Hackel 		spin_unlock(&target->spinlock);
4256714d8e8SKurt Hackel 
4266714d8e8SKurt Hackel 		__dlm_lockres_reserve_ast(res);
427d9ef7522SWengang Wang 		__dlm_queue_ast(dlm, target);
4286714d8e8SKurt Hackel 		/* go back and check for more */
4296714d8e8SKurt Hackel 		goto converting;
4306714d8e8SKurt Hackel 	}
4316714d8e8SKurt Hackel 
4326714d8e8SKurt Hackel blocked:
4336714d8e8SKurt Hackel 	if (list_empty(&res->blocked))
4346714d8e8SKurt Hackel 		goto leave;
4356714d8e8SKurt Hackel 	target = list_entry(res->blocked.next, struct dlm_lock, list);
4366714d8e8SKurt Hackel 
437df53cd3bSDong Fang 	list_for_each_entry(lock, &res->granted, list) {
4386714d8e8SKurt Hackel 		if (lock==target)
4396714d8e8SKurt Hackel 			continue;
4406714d8e8SKurt Hackel 		if (!dlm_lock_compatible(lock->ml.type, target->ml.type)) {
4416714d8e8SKurt Hackel 			can_grant = 0;
4426714d8e8SKurt Hackel 			if (lock->ml.highest_blocked == LKM_IVMODE) {
4436714d8e8SKurt Hackel 				__dlm_lockres_reserve_ast(res);
444d9ef7522SWengang Wang 				__dlm_queue_bast(dlm, lock);
4456714d8e8SKurt Hackel 			}
4466714d8e8SKurt Hackel 			if (lock->ml.highest_blocked < target->ml.type)
4476714d8e8SKurt Hackel 				lock->ml.highest_blocked = target->ml.type;
4486714d8e8SKurt Hackel 		}
4496714d8e8SKurt Hackel 	}
4506714d8e8SKurt Hackel 
451df53cd3bSDong Fang 	list_for_each_entry(lock, &res->converting, list) {
4526714d8e8SKurt Hackel 		if (lock==target)
4536714d8e8SKurt Hackel 			continue;
4546714d8e8SKurt Hackel 		if (!dlm_lock_compatible(lock->ml.type, target->ml.type)) {
4556714d8e8SKurt Hackel 			can_grant = 0;
4566714d8e8SKurt Hackel 			if (lock->ml.highest_blocked == LKM_IVMODE) {
4576714d8e8SKurt Hackel 				__dlm_lockres_reserve_ast(res);
458d9ef7522SWengang Wang 				__dlm_queue_bast(dlm, lock);
4596714d8e8SKurt Hackel 			}
4606714d8e8SKurt Hackel 			if (lock->ml.highest_blocked < target->ml.type)
4616714d8e8SKurt Hackel 				lock->ml.highest_blocked = target->ml.type;
4626714d8e8SKurt Hackel 		}
4636714d8e8SKurt Hackel 	}
4646714d8e8SKurt Hackel 
4656714d8e8SKurt Hackel 	/* we can grant the blocked lock (only
4666714d8e8SKurt Hackel 	 * possible if converting list empty) */
4676714d8e8SKurt Hackel 	if (can_grant) {
4686714d8e8SKurt Hackel 		spin_lock(&target->spinlock);
4696714d8e8SKurt Hackel 		BUG_ON(target->ml.highest_blocked != LKM_IVMODE);
4706714d8e8SKurt Hackel 
4718e17d16fSSunil Mushran 		mlog(0, "%s: res %.*s, AST for Blocked lock %u:%llu, type %d, "
4728e17d16fSSunil Mushran 		     "node %u\n", dlm->name, res->lockname.len,
4738e17d16fSSunil Mushran 		     res->lockname.name,
4748e17d16fSSunil Mushran 		     dlm_get_lock_cookie_node(be64_to_cpu(target->ml.cookie)),
4758e17d16fSSunil Mushran 		     dlm_get_lock_cookie_seq(be64_to_cpu(target->ml.cookie)),
4766714d8e8SKurt Hackel 		     target->ml.type, target->ml.node);
4776714d8e8SKurt Hackel 
4788e17d16fSSunil Mushran 		/* target->ml.type is already correct */
479f116629dSAkinobu Mita 		list_move_tail(&target->list, &res->granted);
4806714d8e8SKurt Hackel 
4816714d8e8SKurt Hackel 		BUG_ON(!target->lksb);
4826714d8e8SKurt Hackel 		target->lksb->status = DLM_NORMAL;
4836714d8e8SKurt Hackel 
4846714d8e8SKurt Hackel 		spin_unlock(&target->spinlock);
4856714d8e8SKurt Hackel 
4866714d8e8SKurt Hackel 		__dlm_lockres_reserve_ast(res);
487d9ef7522SWengang Wang 		__dlm_queue_ast(dlm, target);
4886714d8e8SKurt Hackel 		/* go back and check for more */
4896714d8e8SKurt Hackel 		goto converting;
4906714d8e8SKurt Hackel 	}
4916714d8e8SKurt Hackel 
4926714d8e8SKurt Hackel leave:
4936714d8e8SKurt Hackel 	return;
4946714d8e8SKurt Hackel }
4956714d8e8SKurt Hackel 
4966714d8e8SKurt Hackel /* must have NO locks when calling this with res !=NULL * */
dlm_kick_thread(struct dlm_ctxt * dlm,struct dlm_lock_resource * res)4976714d8e8SKurt Hackel void dlm_kick_thread(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
4986714d8e8SKurt Hackel {
4996714d8e8SKurt Hackel 	if (res) {
5006714d8e8SKurt Hackel 		spin_lock(&dlm->spinlock);
5016714d8e8SKurt Hackel 		spin_lock(&res->spinlock);
5026714d8e8SKurt Hackel 		__dlm_dirty_lockres(dlm, res);
5036714d8e8SKurt Hackel 		spin_unlock(&res->spinlock);
5046714d8e8SKurt Hackel 		spin_unlock(&dlm->spinlock);
5056714d8e8SKurt Hackel 	}
5066714d8e8SKurt Hackel 	wake_up(&dlm->dlm_thread_wq);
5076714d8e8SKurt Hackel }
5086714d8e8SKurt Hackel 
__dlm_dirty_lockres(struct dlm_ctxt * dlm,struct dlm_lock_resource * res)5096714d8e8SKurt Hackel void __dlm_dirty_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
5106714d8e8SKurt Hackel {
5116714d8e8SKurt Hackel 	assert_spin_locked(&dlm->spinlock);
5126714d8e8SKurt Hackel 	assert_spin_locked(&res->spinlock);
5136714d8e8SKurt Hackel 
5146714d8e8SKurt Hackel 	/* don't shuffle secondary queues */
51532c1b90dSNathan Chancellor 	if (res->owner == dlm->node_num) {
516ddc09c8dSKurt Hackel 		if (res->state & (DLM_LOCK_RES_MIGRATING |
517ddc09c8dSKurt Hackel 				  DLM_LOCK_RES_BLOCK_DIRTY))
518ddc09c8dSKurt Hackel 		    return;
519ddc09c8dSKurt Hackel 
520ddc09c8dSKurt Hackel 		if (list_empty(&res->dirty)) {
5216ff06a93SKurt Hackel 			/* ref for dirty_list */
5226ff06a93SKurt Hackel 			dlm_lockres_get(res);
5236714d8e8SKurt Hackel 			list_add_tail(&res->dirty, &dlm->dirty_list);
5246714d8e8SKurt Hackel 			res->state |= DLM_LOCK_RES_DIRTY;
5256714d8e8SKurt Hackel 		}
5266714d8e8SKurt Hackel 	}
5278e17d16fSSunil Mushran 
5288e17d16fSSunil Mushran 	mlog(0, "%s: res %.*s\n", dlm->name, res->lockname.len,
5298e17d16fSSunil Mushran 	     res->lockname.name);
530ddc09c8dSKurt Hackel }
5316714d8e8SKurt Hackel 
5326714d8e8SKurt Hackel 
5336714d8e8SKurt Hackel /* Launch the NM thread for the mounted volume */
dlm_launch_thread(struct dlm_ctxt * dlm)5346714d8e8SKurt Hackel int dlm_launch_thread(struct dlm_ctxt *dlm)
5356714d8e8SKurt Hackel {
5368e17d16fSSunil Mushran 	mlog(0, "Starting dlm_thread...\n");
5376714d8e8SKurt Hackel 
5385afc44e2SJoseph Qi 	dlm->dlm_thread_task = kthread_run(dlm_thread, dlm, "dlm-%s",
5395afc44e2SJoseph Qi 			dlm->name);
5406714d8e8SKurt Hackel 	if (IS_ERR(dlm->dlm_thread_task)) {
5416714d8e8SKurt Hackel 		mlog_errno(PTR_ERR(dlm->dlm_thread_task));
5426714d8e8SKurt Hackel 		dlm->dlm_thread_task = NULL;
5436714d8e8SKurt Hackel 		return -EINVAL;
5446714d8e8SKurt Hackel 	}
5456714d8e8SKurt Hackel 
5466714d8e8SKurt Hackel 	return 0;
5476714d8e8SKurt Hackel }
5486714d8e8SKurt Hackel 
dlm_complete_thread(struct dlm_ctxt * dlm)5496714d8e8SKurt Hackel void dlm_complete_thread(struct dlm_ctxt *dlm)
5506714d8e8SKurt Hackel {
5516714d8e8SKurt Hackel 	if (dlm->dlm_thread_task) {
5528e17d16fSSunil Mushran 		mlog(ML_KTHREAD, "Waiting for dlm thread to exit\n");
5536714d8e8SKurt Hackel 		kthread_stop(dlm->dlm_thread_task);
5546714d8e8SKurt Hackel 		dlm->dlm_thread_task = NULL;
5556714d8e8SKurt Hackel 	}
5566714d8e8SKurt Hackel }
5576714d8e8SKurt Hackel 
dlm_dirty_list_empty(struct dlm_ctxt * dlm)5586714d8e8SKurt Hackel static int dlm_dirty_list_empty(struct dlm_ctxt *dlm)
5596714d8e8SKurt Hackel {
5606714d8e8SKurt Hackel 	int empty;
5616714d8e8SKurt Hackel 
5626714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
5636714d8e8SKurt Hackel 	empty = list_empty(&dlm->dirty_list);
5646714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
5656714d8e8SKurt Hackel 
5666714d8e8SKurt Hackel 	return empty;
5676714d8e8SKurt Hackel }
5686714d8e8SKurt Hackel 
dlm_flush_asts(struct dlm_ctxt * dlm)5696714d8e8SKurt Hackel static void dlm_flush_asts(struct dlm_ctxt *dlm)
5706714d8e8SKurt Hackel {
5716714d8e8SKurt Hackel 	int ret;
5726714d8e8SKurt Hackel 	struct dlm_lock *lock;
5736714d8e8SKurt Hackel 	struct dlm_lock_resource *res;
5746714d8e8SKurt Hackel 	u8 hi;
5756714d8e8SKurt Hackel 
5766714d8e8SKurt Hackel 	spin_lock(&dlm->ast_lock);
5776714d8e8SKurt Hackel 	while (!list_empty(&dlm->pending_asts)) {
5786714d8e8SKurt Hackel 		lock = list_entry(dlm->pending_asts.next,
5796714d8e8SKurt Hackel 				  struct dlm_lock, ast_list);
5806714d8e8SKurt Hackel 		/* get an extra ref on lock */
5816714d8e8SKurt Hackel 		dlm_lock_get(lock);
5826714d8e8SKurt Hackel 		res = lock->lockres;
5838e17d16fSSunil Mushran 		mlog(0, "%s: res %.*s, Flush AST for lock %u:%llu, type %d, "
5848e17d16fSSunil Mushran 		     "node %u\n", dlm->name, res->lockname.len,
5858e17d16fSSunil Mushran 		     res->lockname.name,
5868e17d16fSSunil Mushran 		     dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
5878e17d16fSSunil Mushran 		     dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)),
5888e17d16fSSunil Mushran 		     lock->ml.type, lock->ml.node);
5896714d8e8SKurt Hackel 
5906714d8e8SKurt Hackel 		BUG_ON(!lock->ast_pending);
5916714d8e8SKurt Hackel 
5926714d8e8SKurt Hackel 		/* remove from list (including ref) */
5936714d8e8SKurt Hackel 		list_del_init(&lock->ast_list);
5946714d8e8SKurt Hackel 		dlm_lock_put(lock);
5956714d8e8SKurt Hackel 		spin_unlock(&dlm->ast_lock);
5966714d8e8SKurt Hackel 
5976714d8e8SKurt Hackel 		if (lock->ml.node != dlm->node_num) {
5986714d8e8SKurt Hackel 			ret = dlm_do_remote_ast(dlm, res, lock);
5996714d8e8SKurt Hackel 			if (ret < 0)
6006714d8e8SKurt Hackel 				mlog_errno(ret);
6016714d8e8SKurt Hackel 		} else
6026714d8e8SKurt Hackel 			dlm_do_local_ast(dlm, res, lock);
6036714d8e8SKurt Hackel 
6046714d8e8SKurt Hackel 		spin_lock(&dlm->ast_lock);
6056714d8e8SKurt Hackel 
6066714d8e8SKurt Hackel 		/* possible that another ast was queued while
6076714d8e8SKurt Hackel 		 * we were delivering the last one */
6086714d8e8SKurt Hackel 		if (!list_empty(&lock->ast_list)) {
6098e17d16fSSunil Mushran 			mlog(0, "%s: res %.*s, AST queued while flushing last "
6108e17d16fSSunil Mushran 			     "one\n", dlm->name, res->lockname.len,
6118e17d16fSSunil Mushran 			     res->lockname.name);
6126714d8e8SKurt Hackel 		} else
6136714d8e8SKurt Hackel 			lock->ast_pending = 0;
6146714d8e8SKurt Hackel 
6156714d8e8SKurt Hackel 		/* drop the extra ref.
6166714d8e8SKurt Hackel 		 * this may drop it completely. */
6176714d8e8SKurt Hackel 		dlm_lock_put(lock);
6186714d8e8SKurt Hackel 		dlm_lockres_release_ast(dlm, res);
6196714d8e8SKurt Hackel 	}
6206714d8e8SKurt Hackel 
6216714d8e8SKurt Hackel 	while (!list_empty(&dlm->pending_basts)) {
6226714d8e8SKurt Hackel 		lock = list_entry(dlm->pending_basts.next,
6236714d8e8SKurt Hackel 				  struct dlm_lock, bast_list);
6246714d8e8SKurt Hackel 		/* get an extra ref on lock */
6256714d8e8SKurt Hackel 		dlm_lock_get(lock);
6266714d8e8SKurt Hackel 		res = lock->lockres;
6276714d8e8SKurt Hackel 
6286714d8e8SKurt Hackel 		BUG_ON(!lock->bast_pending);
6296714d8e8SKurt Hackel 
6306714d8e8SKurt Hackel 		/* get the highest blocked lock, and reset */
6316714d8e8SKurt Hackel 		spin_lock(&lock->spinlock);
6326714d8e8SKurt Hackel 		BUG_ON(lock->ml.highest_blocked <= LKM_IVMODE);
6336714d8e8SKurt Hackel 		hi = lock->ml.highest_blocked;
6346714d8e8SKurt Hackel 		lock->ml.highest_blocked = LKM_IVMODE;
6356714d8e8SKurt Hackel 		spin_unlock(&lock->spinlock);
6366714d8e8SKurt Hackel 
6376714d8e8SKurt Hackel 		/* remove from list (including ref) */
6386714d8e8SKurt Hackel 		list_del_init(&lock->bast_list);
6396714d8e8SKurt Hackel 		dlm_lock_put(lock);
6406714d8e8SKurt Hackel 		spin_unlock(&dlm->ast_lock);
6416714d8e8SKurt Hackel 
6428e17d16fSSunil Mushran 		mlog(0, "%s: res %.*s, Flush BAST for lock %u:%llu, "
6438e17d16fSSunil Mushran 		     "blocked %d, node %u\n",
6448e17d16fSSunil Mushran 		     dlm->name, res->lockname.len, res->lockname.name,
6458e17d16fSSunil Mushran 		     dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
6468e17d16fSSunil Mushran 		     dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)),
6478e17d16fSSunil Mushran 		     hi, lock->ml.node);
6486714d8e8SKurt Hackel 
6496714d8e8SKurt Hackel 		if (lock->ml.node != dlm->node_num) {
6506714d8e8SKurt Hackel 			ret = dlm_send_proxy_bast(dlm, res, lock, hi);
6516714d8e8SKurt Hackel 			if (ret < 0)
6526714d8e8SKurt Hackel 				mlog_errno(ret);
6536714d8e8SKurt Hackel 		} else
6546714d8e8SKurt Hackel 			dlm_do_local_bast(dlm, res, lock, hi);
6556714d8e8SKurt Hackel 
6566714d8e8SKurt Hackel 		spin_lock(&dlm->ast_lock);
6576714d8e8SKurt Hackel 
6586714d8e8SKurt Hackel 		/* possible that another bast was queued while
6596714d8e8SKurt Hackel 		 * we were delivering the last one */
6606714d8e8SKurt Hackel 		if (!list_empty(&lock->bast_list)) {
6618e17d16fSSunil Mushran 			mlog(0, "%s: res %.*s, BAST queued while flushing last "
6628e17d16fSSunil Mushran 			     "one\n", dlm->name, res->lockname.len,
6638e17d16fSSunil Mushran 			     res->lockname.name);
6646714d8e8SKurt Hackel 		} else
6656714d8e8SKurt Hackel 			lock->bast_pending = 0;
6666714d8e8SKurt Hackel 
6676714d8e8SKurt Hackel 		/* drop the extra ref.
6686714d8e8SKurt Hackel 		 * this may drop it completely. */
6696714d8e8SKurt Hackel 		dlm_lock_put(lock);
6706714d8e8SKurt Hackel 		dlm_lockres_release_ast(dlm, res);
6716714d8e8SKurt Hackel 	}
6726714d8e8SKurt Hackel 	wake_up(&dlm->ast_wq);
6736714d8e8SKurt Hackel 	spin_unlock(&dlm->ast_lock);
6746714d8e8SKurt Hackel }
6756714d8e8SKurt Hackel 
6766714d8e8SKurt Hackel 
6776714d8e8SKurt Hackel #define DLM_THREAD_TIMEOUT_MS (4 * 1000)
6786714d8e8SKurt Hackel #define DLM_THREAD_MAX_DIRTY  100
6796714d8e8SKurt Hackel 
dlm_thread(void * data)6806714d8e8SKurt Hackel static int dlm_thread(void *data)
6816714d8e8SKurt Hackel {
6826714d8e8SKurt Hackel 	struct dlm_lock_resource *res;
6836714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
6846714d8e8SKurt Hackel 	unsigned long timeout = msecs_to_jiffies(DLM_THREAD_TIMEOUT_MS);
6856714d8e8SKurt Hackel 
6866714d8e8SKurt Hackel 	mlog(0, "dlm thread running for %s...\n", dlm->name);
6876714d8e8SKurt Hackel 
6886714d8e8SKurt Hackel 	while (!kthread_should_stop()) {
6896714d8e8SKurt Hackel 		int n = DLM_THREAD_MAX_DIRTY;
6906714d8e8SKurt Hackel 
6916714d8e8SKurt Hackel 		/* dlm_shutting_down is very point-in-time, but that
6926714d8e8SKurt Hackel 		 * doesn't matter as we'll just loop back around if we
6936714d8e8SKurt Hackel 		 * get false on the leading edge of a state
6946714d8e8SKurt Hackel 		 * transition. */
6956714d8e8SKurt Hackel 		dlm_run_purge_list(dlm, dlm_shutting_down(dlm));
6966714d8e8SKurt Hackel 
6976714d8e8SKurt Hackel 		/* We really don't want to hold dlm->spinlock while
6986714d8e8SKurt Hackel 		 * calling dlm_shuffle_lists on each lockres that
6996714d8e8SKurt Hackel 		 * needs to have its queues adjusted and AST/BASTs
7006714d8e8SKurt Hackel 		 * run.  So let's pull each entry off the dirty_list
7016714d8e8SKurt Hackel 		 * and drop dlm->spinlock ASAP.  Once off the list,
7026714d8e8SKurt Hackel 		 * res->spinlock needs to be taken again to protect
7036714d8e8SKurt Hackel 		 * the queues while calling dlm_shuffle_lists.  */
7046714d8e8SKurt Hackel 		spin_lock(&dlm->spinlock);
7056714d8e8SKurt Hackel 		while (!list_empty(&dlm->dirty_list)) {
7066714d8e8SKurt Hackel 			int delay = 0;
7076714d8e8SKurt Hackel 			res = list_entry(dlm->dirty_list.next,
7086714d8e8SKurt Hackel 					 struct dlm_lock_resource, dirty);
7096714d8e8SKurt Hackel 
7106714d8e8SKurt Hackel 			/* peel a lockres off, remove it from the list,
7116714d8e8SKurt Hackel 			 * unset the dirty flag and drop the dlm lock */
7126714d8e8SKurt Hackel 			BUG_ON(!res);
7136714d8e8SKurt Hackel 			dlm_lockres_get(res);
7146714d8e8SKurt Hackel 
7156714d8e8SKurt Hackel 			spin_lock(&res->spinlock);
716ddc09c8dSKurt Hackel 			/* We clear the DLM_LOCK_RES_DIRTY state once we shuffle lists below */
7176714d8e8SKurt Hackel 			list_del_init(&res->dirty);
7186714d8e8SKurt Hackel 			spin_unlock(&res->spinlock);
7196714d8e8SKurt Hackel 			spin_unlock(&dlm->spinlock);
7206ff06a93SKurt Hackel 			/* Drop dirty_list ref */
7216ff06a93SKurt Hackel 			dlm_lockres_put(res);
7226714d8e8SKurt Hackel 
7236714d8e8SKurt Hackel 		 	/* lockres can be re-dirtied/re-added to the
7246714d8e8SKurt Hackel 			 * dirty_list in this gap, but that is ok */
7256714d8e8SKurt Hackel 
726d9ef7522SWengang Wang 			spin_lock(&dlm->ast_lock);
7276714d8e8SKurt Hackel 			spin_lock(&res->spinlock);
7286714d8e8SKurt Hackel 			if (res->owner != dlm->node_num) {
7296714d8e8SKurt Hackel 				__dlm_print_one_lock_resource(res);
7308e17d16fSSunil Mushran 				mlog(ML_ERROR, "%s: inprog %d, mig %d, reco %d,"
7318e17d16fSSunil Mushran 				     " dirty %d\n", dlm->name,
7328e17d16fSSunil Mushran 				     !!(res->state & DLM_LOCK_RES_IN_PROGRESS),
7338e17d16fSSunil Mushran 				     !!(res->state & DLM_LOCK_RES_MIGRATING),
7348e17d16fSSunil Mushran 				     !!(res->state & DLM_LOCK_RES_RECOVERING),
7358e17d16fSSunil Mushran 				     !!(res->state & DLM_LOCK_RES_DIRTY));
7366714d8e8SKurt Hackel 			}
7376714d8e8SKurt Hackel 			BUG_ON(res->owner != dlm->node_num);
7386714d8e8SKurt Hackel 
7396714d8e8SKurt Hackel 			/* it is now ok to move lockreses in these states
7406714d8e8SKurt Hackel 			 * to the dirty list, assuming that they will only be
7416714d8e8SKurt Hackel 			 * dirty for a short while. */
742ddc09c8dSKurt Hackel 			BUG_ON(res->state & DLM_LOCK_RES_MIGRATING);
7436714d8e8SKurt Hackel 			if (res->state & (DLM_LOCK_RES_IN_PROGRESS |
744814ce694SJiufei Xue 					  DLM_LOCK_RES_RECOVERING |
745814ce694SJiufei Xue 					  DLM_LOCK_RES_RECOVERY_WAITING)) {
7466714d8e8SKurt Hackel 				/* move it to the tail and keep going */
747ddc09c8dSKurt Hackel 				res->state &= ~DLM_LOCK_RES_DIRTY;
7486714d8e8SKurt Hackel 				spin_unlock(&res->spinlock);
749d9ef7522SWengang Wang 				spin_unlock(&dlm->ast_lock);
7508e17d16fSSunil Mushran 				mlog(0, "%s: res %.*s, inprogress, delay list "
7518e17d16fSSunil Mushran 				     "shuffle, state %d\n", dlm->name,
7526714d8e8SKurt Hackel 				     res->lockname.len, res->lockname.name,
7536714d8e8SKurt Hackel 				     res->state);
7546714d8e8SKurt Hackel 				delay = 1;
7556714d8e8SKurt Hackel 				goto in_progress;
7566714d8e8SKurt Hackel 			}
7576714d8e8SKurt Hackel 
7586714d8e8SKurt Hackel 			/* at this point the lockres is not migrating/
7596714d8e8SKurt Hackel 			 * recovering/in-progress.  we have the lockres
7606714d8e8SKurt Hackel 			 * spinlock and do NOT have the dlm lock.
7616714d8e8SKurt Hackel 			 * safe to reserve/queue asts and run the lists. */
7626714d8e8SKurt Hackel 
7636714d8e8SKurt Hackel 			/* called while holding lockres lock */
7646714d8e8SKurt Hackel 			dlm_shuffle_lists(dlm, res);
765ddc09c8dSKurt Hackel 			res->state &= ~DLM_LOCK_RES_DIRTY;
7666714d8e8SKurt Hackel 			spin_unlock(&res->spinlock);
767d9ef7522SWengang Wang 			spin_unlock(&dlm->ast_lock);
7686714d8e8SKurt Hackel 
7696714d8e8SKurt Hackel 			dlm_lockres_calc_usage(dlm, res);
7706714d8e8SKurt Hackel 
7716714d8e8SKurt Hackel in_progress:
7726714d8e8SKurt Hackel 
7736714d8e8SKurt Hackel 			spin_lock(&dlm->spinlock);
7746714d8e8SKurt Hackel 			/* if the lock was in-progress, stick
7756714d8e8SKurt Hackel 			 * it on the back of the list */
7766714d8e8SKurt Hackel 			if (delay) {
7776714d8e8SKurt Hackel 				spin_lock(&res->spinlock);
778ddc09c8dSKurt Hackel 				__dlm_dirty_lockres(dlm, res);
7796714d8e8SKurt Hackel 				spin_unlock(&res->spinlock);
7806714d8e8SKurt Hackel 			}
7816714d8e8SKurt Hackel 			dlm_lockres_put(res);
7826714d8e8SKurt Hackel 
7836714d8e8SKurt Hackel 			/* unlikely, but we may need to give time to
7846714d8e8SKurt Hackel 			 * other tasks */
7856714d8e8SKurt Hackel 			if (!--n) {
7868e17d16fSSunil Mushran 				mlog(0, "%s: Throttling dlm thread\n",
7878e17d16fSSunil Mushran 				     dlm->name);
7886714d8e8SKurt Hackel 				break;
7896714d8e8SKurt Hackel 			}
7906714d8e8SKurt Hackel 		}
7916714d8e8SKurt Hackel 
7926714d8e8SKurt Hackel 		spin_unlock(&dlm->spinlock);
7936714d8e8SKurt Hackel 		dlm_flush_asts(dlm);
7946714d8e8SKurt Hackel 
7956714d8e8SKurt Hackel 		/* yield and continue right away if there is more work to do */
7966714d8e8SKurt Hackel 		if (!n) {
797f85cd47aSKurt Hackel 			cond_resched();
7986714d8e8SKurt Hackel 			continue;
7996714d8e8SKurt Hackel 		}
8006714d8e8SKurt Hackel 
8016714d8e8SKurt Hackel 		wait_event_interruptible_timeout(dlm->dlm_thread_wq,
8026714d8e8SKurt Hackel 						 !dlm_dirty_list_empty(dlm) ||
8036714d8e8SKurt Hackel 						 kthread_should_stop(),
8046714d8e8SKurt Hackel 						 timeout);
8056714d8e8SKurt Hackel 	}
8066714d8e8SKurt Hackel 
8076714d8e8SKurt Hackel 	mlog(0, "quitting DLM thread\n");
8086714d8e8SKurt Hackel 	return 0;
8096714d8e8SKurt Hackel }
810