xref: /openbmc/linux/fs/ocfs2/dlm/dlmthread.c (revision 309e9191)
16714d8e8SKurt Hackel /* -*- mode: c; c-basic-offset: 8; -*-
26714d8e8SKurt Hackel  * vim: noexpandtab sw=8 ts=8 sts=0:
36714d8e8SKurt Hackel  *
46714d8e8SKurt Hackel  * dlmthread.c
56714d8e8SKurt Hackel  *
66714d8e8SKurt Hackel  * standalone DLM module
76714d8e8SKurt Hackel  *
86714d8e8SKurt Hackel  * Copyright (C) 2004 Oracle.  All rights reserved.
96714d8e8SKurt Hackel  *
106714d8e8SKurt Hackel  * This program is free software; you can redistribute it and/or
116714d8e8SKurt Hackel  * modify it under the terms of the GNU General Public
126714d8e8SKurt Hackel  * License as published by the Free Software Foundation; either
136714d8e8SKurt Hackel  * version 2 of the License, or (at your option) any later version.
146714d8e8SKurt Hackel  *
156714d8e8SKurt Hackel  * This program is distributed in the hope that it will be useful,
166714d8e8SKurt Hackel  * but WITHOUT ANY WARRANTY; without even the implied warranty of
176714d8e8SKurt Hackel  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
186714d8e8SKurt Hackel  * General Public License for more details.
196714d8e8SKurt Hackel  *
206714d8e8SKurt Hackel  * You should have received a copy of the GNU General Public
216714d8e8SKurt Hackel  * License along with this program; if not, write to the
226714d8e8SKurt Hackel  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
236714d8e8SKurt Hackel  * Boston, MA 021110-1307, USA.
246714d8e8SKurt Hackel  *
256714d8e8SKurt Hackel  */
266714d8e8SKurt Hackel 
276714d8e8SKurt Hackel 
286714d8e8SKurt Hackel #include <linux/module.h>
296714d8e8SKurt Hackel #include <linux/fs.h>
306714d8e8SKurt Hackel #include <linux/types.h>
316714d8e8SKurt Hackel #include <linux/highmem.h>
326714d8e8SKurt Hackel #include <linux/init.h>
336714d8e8SKurt Hackel #include <linux/sysctl.h>
346714d8e8SKurt Hackel #include <linux/random.h>
356714d8e8SKurt Hackel #include <linux/blkdev.h>
366714d8e8SKurt Hackel #include <linux/socket.h>
376714d8e8SKurt Hackel #include <linux/inet.h>
386714d8e8SKurt Hackel #include <linux/timer.h>
396714d8e8SKurt Hackel #include <linux/kthread.h>
408d79d088SKurt Hackel #include <linux/delay.h>
416714d8e8SKurt Hackel 
426714d8e8SKurt Hackel 
436714d8e8SKurt Hackel #include "cluster/heartbeat.h"
446714d8e8SKurt Hackel #include "cluster/nodemanager.h"
456714d8e8SKurt Hackel #include "cluster/tcp.h"
466714d8e8SKurt Hackel 
476714d8e8SKurt Hackel #include "dlmapi.h"
486714d8e8SKurt Hackel #include "dlmcommon.h"
496714d8e8SKurt Hackel #include "dlmdomain.h"
506714d8e8SKurt Hackel 
516714d8e8SKurt Hackel #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_THREAD)
526714d8e8SKurt Hackel #include "cluster/masklog.h"
536714d8e8SKurt Hackel 
546714d8e8SKurt Hackel static int dlm_thread(void *data);
556714d8e8SKurt Hackel static void dlm_flush_asts(struct dlm_ctxt *dlm);
566714d8e8SKurt Hackel 
576714d8e8SKurt Hackel #define dlm_lock_is_remote(dlm, lock)     ((lock)->ml.node != (dlm)->node_num)
586714d8e8SKurt Hackel 
596714d8e8SKurt Hackel /* will exit holding res->spinlock, but may drop in function */
606714d8e8SKurt Hackel /* waits until flags are cleared on res->state */
616714d8e8SKurt Hackel void __dlm_wait_on_lockres_flags(struct dlm_lock_resource *res, int flags)
626714d8e8SKurt Hackel {
636714d8e8SKurt Hackel 	DECLARE_WAITQUEUE(wait, current);
646714d8e8SKurt Hackel 
656714d8e8SKurt Hackel 	assert_spin_locked(&res->spinlock);
666714d8e8SKurt Hackel 
676714d8e8SKurt Hackel 	add_wait_queue(&res->wq, &wait);
686714d8e8SKurt Hackel repeat:
696714d8e8SKurt Hackel 	set_current_state(TASK_UNINTERRUPTIBLE);
706714d8e8SKurt Hackel 	if (res->state & flags) {
716714d8e8SKurt Hackel 		spin_unlock(&res->spinlock);
726714d8e8SKurt Hackel 		schedule();
736714d8e8SKurt Hackel 		spin_lock(&res->spinlock);
746714d8e8SKurt Hackel 		goto repeat;
756714d8e8SKurt Hackel 	}
766714d8e8SKurt Hackel 	remove_wait_queue(&res->wq, &wait);
775c2c9d38SMilind Arun Choudhary 	__set_current_state(TASK_RUNNING);
786714d8e8SKurt Hackel }
796714d8e8SKurt Hackel 
80ba2bf218SKurt Hackel int __dlm_lockres_has_locks(struct dlm_lock_resource *res)
816714d8e8SKurt Hackel {
826714d8e8SKurt Hackel 	if (list_empty(&res->granted) &&
836714d8e8SKurt Hackel 	    list_empty(&res->converting) &&
84ba2bf218SKurt Hackel 	    list_empty(&res->blocked))
85ba2bf218SKurt Hackel 		return 0;
866714d8e8SKurt Hackel 	return 1;
87ba2bf218SKurt Hackel }
88ba2bf218SKurt Hackel 
89ba2bf218SKurt Hackel /* "unused": the lockres has no locks, is not on the dirty list,
90ba2bf218SKurt Hackel  * has no inflight locks (in the gap between mastery and acquiring
91ba2bf218SKurt Hackel  * the first lock), and has no bits in its refmap.
92ba2bf218SKurt Hackel  * truly ready to be freed. */
93ba2bf218SKurt Hackel int __dlm_lockres_unused(struct dlm_lock_resource *res)
94ba2bf218SKurt Hackel {
95a524812bSWengang Wang 	int bit;
96a524812bSWengang Wang 
97ff0a522eSSunil Mushran 	assert_spin_locked(&res->spinlock);
98ff0a522eSSunil Mushran 
99a524812bSWengang Wang 	if (__dlm_lockres_has_locks(res))
100a524812bSWengang Wang 		return 0;
101a524812bSWengang Wang 
102ff0a522eSSunil Mushran 	/* Locks are in the process of being created */
103ff0a522eSSunil Mushran 	if (res->inflight_locks)
104ff0a522eSSunil Mushran 		return 0;
105ff0a522eSSunil Mushran 
106a524812bSWengang Wang 	if (!list_empty(&res->dirty) || res->state & DLM_LOCK_RES_DIRTY)
107a524812bSWengang Wang 		return 0;
108a524812bSWengang Wang 
109814ce694SJiufei Xue 	if (res->state & (DLM_LOCK_RES_RECOVERING|
110814ce694SJiufei Xue 			DLM_LOCK_RES_RECOVERY_WAITING))
111a524812bSWengang Wang 		return 0;
112a524812bSWengang Wang 
113ff0a522eSSunil Mushran 	/* Another node has this resource with this node as the master */
114a524812bSWengang Wang 	bit = find_next_bit(res->refmap, O2NM_MAX_NODES, 0);
115a524812bSWengang Wang 	if (bit < O2NM_MAX_NODES)
116a524812bSWengang Wang 		return 0;
117a524812bSWengang Wang 
118ba2bf218SKurt Hackel 	return 1;
119ba2bf218SKurt Hackel }
1206714d8e8SKurt Hackel 
1216714d8e8SKurt Hackel 
1226714d8e8SKurt Hackel /* Call whenever you may have added or deleted something from one of
1236714d8e8SKurt Hackel  * the lockres queue's. This will figure out whether it belongs on the
1246714d8e8SKurt Hackel  * unused list or not and does the appropriate thing. */
1256714d8e8SKurt Hackel void __dlm_lockres_calc_usage(struct dlm_ctxt *dlm,
1266714d8e8SKurt Hackel 			      struct dlm_lock_resource *res)
1276714d8e8SKurt Hackel {
1286714d8e8SKurt Hackel 	assert_spin_locked(&dlm->spinlock);
1296714d8e8SKurt Hackel 	assert_spin_locked(&res->spinlock);
1306714d8e8SKurt Hackel 
1316714d8e8SKurt Hackel 	if (__dlm_lockres_unused(res)){
1326714d8e8SKurt Hackel 		if (list_empty(&res->purge)) {
1338e17d16fSSunil Mushran 			mlog(0, "%s: Adding res %.*s to purge list\n",
1348e17d16fSSunil Mushran 			     dlm->name, res->lockname.len, res->lockname.name);
1356714d8e8SKurt Hackel 
1366714d8e8SKurt Hackel 			res->last_used = jiffies;
137ba2bf218SKurt Hackel 			dlm_lockres_get(res);
1386714d8e8SKurt Hackel 			list_add_tail(&res->purge, &dlm->purge_list);
1396714d8e8SKurt Hackel 			dlm->purge_count++;
1406714d8e8SKurt Hackel 		}
1416714d8e8SKurt Hackel 	} else if (!list_empty(&res->purge)) {
1428e17d16fSSunil Mushran 		mlog(0, "%s: Removing res %.*s from purge list\n",
1438e17d16fSSunil Mushran 		     dlm->name, res->lockname.len, res->lockname.name);
1446714d8e8SKurt Hackel 
1456714d8e8SKurt Hackel 		list_del_init(&res->purge);
146ba2bf218SKurt Hackel 		dlm_lockres_put(res);
1476714d8e8SKurt Hackel 		dlm->purge_count--;
1486714d8e8SKurt Hackel 	}
1496714d8e8SKurt Hackel }
1506714d8e8SKurt Hackel 
1516714d8e8SKurt Hackel void dlm_lockres_calc_usage(struct dlm_ctxt *dlm,
1526714d8e8SKurt Hackel 			    struct dlm_lock_resource *res)
1536714d8e8SKurt Hackel {
1546714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
1556714d8e8SKurt Hackel 	spin_lock(&res->spinlock);
1566714d8e8SKurt Hackel 
1576714d8e8SKurt Hackel 	__dlm_lockres_calc_usage(dlm, res);
1586714d8e8SKurt Hackel 
1596714d8e8SKurt Hackel 	spin_unlock(&res->spinlock);
1606714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
1616714d8e8SKurt Hackel }
1626714d8e8SKurt Hackel 
1637beaf243SSrinivas Eeda static void dlm_purge_lockres(struct dlm_ctxt *dlm,
164faf0ec9fSAdrian Bunk 			     struct dlm_lock_resource *res)
1656714d8e8SKurt Hackel {
1666714d8e8SKurt Hackel 	int master;
167ba2bf218SKurt Hackel 	int ret = 0;
1686714d8e8SKurt Hackel 
1697beaf243SSrinivas Eeda 	assert_spin_locked(&dlm->spinlock);
1707beaf243SSrinivas Eeda 	assert_spin_locked(&res->spinlock);
171516b7e52SSunil Mushran 
172ba2bf218SKurt Hackel 	master = (res->owner == dlm->node_num);
173516b7e52SSunil Mushran 
1748e17d16fSSunil Mushran 	mlog(0, "%s: Purging res %.*s, master %d\n", dlm->name,
1758e17d16fSSunil Mushran 	     res->lockname.len, res->lockname.name, master);
176ba2bf218SKurt Hackel 
177ba2bf218SKurt Hackel 	if (!master) {
178309e9191Spiaojun 		if (res->state & DLM_LOCK_RES_DROPPING_REF) {
179309e9191Spiaojun 			mlog(ML_NOTICE, "%s: res %.*s already in "
180309e9191Spiaojun 				"DLM_LOCK_RES_DROPPING_REF state\n",
181309e9191Spiaojun 				dlm->name, res->lockname.len,
182309e9191Spiaojun 				res->lockname.name);
183309e9191Spiaojun 			spin_unlock(&res->spinlock);
184309e9191Spiaojun 			return;
185309e9191Spiaojun 		}
186309e9191Spiaojun 
1877beaf243SSrinivas Eeda 		res->state |= DLM_LOCK_RES_DROPPING_REF;
188c824c3c7SSunil Mushran 		/* drop spinlock...  retake below */
1897beaf243SSrinivas Eeda 		spin_unlock(&res->spinlock);
190c824c3c7SSunil Mushran 		spin_unlock(&dlm->spinlock);
191c824c3c7SSunil Mushran 
1923b8118cfSKurt Hackel 		spin_lock(&res->spinlock);
1933b8118cfSKurt Hackel 		/* This ensures that clear refmap is sent after the set */
1947dc102b7SSunil Mushran 		__dlm_wait_on_lockres_flags(res, DLM_LOCK_RES_SETREF_INPROG);
1953b8118cfSKurt Hackel 		spin_unlock(&res->spinlock);
196c824c3c7SSunil Mushran 
197ba2bf218SKurt Hackel 		/* clear our bit from the master's refmap, ignore errors */
198ba2bf218SKurt Hackel 		ret = dlm_drop_lockres_ref(dlm, res);
199ba2bf218SKurt Hackel 		if (ret < 0) {
200ba2bf218SKurt Hackel 			if (!dlm_is_host_down(ret))
2016714d8e8SKurt Hackel 				BUG();
2026714d8e8SKurt Hackel 		}
2036714d8e8SKurt Hackel 		spin_lock(&dlm->spinlock);
2047beaf243SSrinivas Eeda 		spin_lock(&res->spinlock);
205ba2bf218SKurt Hackel 	}
2066714d8e8SKurt Hackel 
207ba2bf218SKurt Hackel 	if (!list_empty(&res->purge)) {
2088e17d16fSSunil Mushran 		mlog(0, "%s: Removing res %.*s from purgelist, master %d\n",
2098e17d16fSSunil Mushran 		     dlm->name, res->lockname.len, res->lockname.name, master);
210ba2bf218SKurt Hackel 		list_del_init(&res->purge);
211ba2bf218SKurt Hackel 		dlm_lockres_put(res);
2126714d8e8SKurt Hackel 		dlm->purge_count--;
2137beaf243SSrinivas Eeda 	}
2147beaf243SSrinivas Eeda 
215309e9191Spiaojun 	if (!master && ret == DLM_DEREF_RESPONSE_INPROG) {
216309e9191Spiaojun 		mlog(0, "%s: deref %.*s in progress\n",
217842b90b6Sxuejiufei 			dlm->name, res->lockname.len, res->lockname.name);
218842b90b6Sxuejiufei 		spin_unlock(&res->spinlock);
219842b90b6Sxuejiufei 		return;
220842b90b6Sxuejiufei 	}
221842b90b6Sxuejiufei 
2227beaf243SSrinivas Eeda 	if (!__dlm_lockres_unused(res)) {
2238e17d16fSSunil Mushran 		mlog(ML_ERROR, "%s: res %.*s in use after deref\n",
2247beaf243SSrinivas Eeda 		     dlm->name, res->lockname.len, res->lockname.name);
2257beaf243SSrinivas Eeda 		__dlm_print_one_lock_resource(res);
2267beaf243SSrinivas Eeda 		BUG();
2277beaf243SSrinivas Eeda 	}
22883e32d90SWengang Wang 
229e9f0b6a6SSunil Mushran 	__dlm_unhash_lockres(dlm, res);
230ba2bf218SKurt Hackel 
231f57a22ddSYiwen Jiang 	spin_lock(&dlm->track_lock);
232f57a22ddSYiwen Jiang 	if (!list_empty(&res->tracking))
233f57a22ddSYiwen Jiang 		list_del_init(&res->tracking);
234f57a22ddSYiwen Jiang 	else {
235f57a22ddSYiwen Jiang 		mlog(ML_ERROR, "Resource %.*s not on the Tracking list\n",
236f57a22ddSYiwen Jiang 				res->lockname.len, res->lockname.name);
237f57a22ddSYiwen Jiang 		__dlm_print_one_lock_resource(res);
238f57a22ddSYiwen Jiang 	}
239f57a22ddSYiwen Jiang 	spin_unlock(&dlm->track_lock);
240f57a22ddSYiwen Jiang 
241ba2bf218SKurt Hackel 	/* lockres is not in the hash now.  drop the flag and wake up
242ba2bf218SKurt Hackel 	 * any processes waiting in dlm_get_lock_resource. */
243ba2bf218SKurt Hackel 	if (!master) {
244ba2bf218SKurt Hackel 		res->state &= ~DLM_LOCK_RES_DROPPING_REF;
245ba2bf218SKurt Hackel 		spin_unlock(&res->spinlock);
246ba2bf218SKurt Hackel 		wake_up(&res->wq);
2477beaf243SSrinivas Eeda 	} else
2487beaf243SSrinivas Eeda 		spin_unlock(&res->spinlock);
2498b219809SKurt Hackel }
2508b219809SKurt Hackel 
2516714d8e8SKurt Hackel static void dlm_run_purge_list(struct dlm_ctxt *dlm,
2526714d8e8SKurt Hackel 			       int purge_now)
2536714d8e8SKurt Hackel {
2546714d8e8SKurt Hackel 	unsigned int run_max, unused;
2556714d8e8SKurt Hackel 	unsigned long purge_jiffies;
2566714d8e8SKurt Hackel 	struct dlm_lock_resource *lockres;
2576714d8e8SKurt Hackel 
2586714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
2596714d8e8SKurt Hackel 	run_max = dlm->purge_count;
2606714d8e8SKurt Hackel 
2616714d8e8SKurt Hackel 	while(run_max && !list_empty(&dlm->purge_list)) {
2626714d8e8SKurt Hackel 		run_max--;
2636714d8e8SKurt Hackel 
2646714d8e8SKurt Hackel 		lockres = list_entry(dlm->purge_list.next,
2656714d8e8SKurt Hackel 				     struct dlm_lock_resource, purge);
2666714d8e8SKurt Hackel 
2676714d8e8SKurt Hackel 		spin_lock(&lockres->spinlock);
2686714d8e8SKurt Hackel 
2696714d8e8SKurt Hackel 		purge_jiffies = lockres->last_used +
2706714d8e8SKurt Hackel 			msecs_to_jiffies(DLM_PURGE_INTERVAL_MS);
2716714d8e8SKurt Hackel 
2726714d8e8SKurt Hackel 		/* Make sure that we want to be processing this guy at
2736714d8e8SKurt Hackel 		 * this time. */
2746714d8e8SKurt Hackel 		if (!purge_now && time_after(purge_jiffies, jiffies)) {
2756714d8e8SKurt Hackel 			/* Since resources are added to the purge list
2766714d8e8SKurt Hackel 			 * in tail order, we can stop at the first
2776714d8e8SKurt Hackel 			 * unpurgable resource -- anyone added after
2786714d8e8SKurt Hackel 			 * him will have a greater last_used value */
2797beaf243SSrinivas Eeda 			spin_unlock(&lockres->spinlock);
2806714d8e8SKurt Hackel 			break;
2816714d8e8SKurt Hackel 		}
2826714d8e8SKurt Hackel 
2837beaf243SSrinivas Eeda 		/* Status of the lockres *might* change so double
2847beaf243SSrinivas Eeda 		 * check. If the lockres is unused, holding the dlm
2857beaf243SSrinivas Eeda 		 * spinlock will prevent people from getting and more
2867beaf243SSrinivas Eeda 		 * refs on it. */
2877beaf243SSrinivas Eeda 		unused = __dlm_lockres_unused(lockres);
2887beaf243SSrinivas Eeda 		if (!unused ||
289ac4fef4dSXue jiufei 		    (lockres->state & DLM_LOCK_RES_MIGRATING) ||
290ac4fef4dSXue jiufei 		    (lockres->inflight_assert_workers != 0)) {
2918e17d16fSSunil Mushran 			mlog(0, "%s: res %.*s is in use or being remastered, "
292ac4fef4dSXue jiufei 			     "used %d, state %d, assert master workers %u\n",
293ac4fef4dSXue jiufei 			     dlm->name, lockres->lockname.len,
294ac4fef4dSXue jiufei 			     lockres->lockname.name,
295ac4fef4dSXue jiufei 			     !unused, lockres->state,
296ac4fef4dSXue jiufei 			     lockres->inflight_assert_workers);
297a270c6d3SXue jiufei 			list_move_tail(&lockres->purge, &dlm->purge_list);
2987beaf243SSrinivas Eeda 			spin_unlock(&lockres->spinlock);
2997beaf243SSrinivas Eeda 			continue;
3007beaf243SSrinivas Eeda 		}
3017beaf243SSrinivas Eeda 
30278062cb2SSunil Mushran 		dlm_lockres_get(lockres);
3036714d8e8SKurt Hackel 
3047beaf243SSrinivas Eeda 		dlm_purge_lockres(dlm, lockres);
30578062cb2SSunil Mushran 
3063fca0894SSunil Mushran 		dlm_lockres_put(lockres);
3076714d8e8SKurt Hackel 
3086714d8e8SKurt Hackel 		/* Avoid adding any scheduling latencies */
3096714d8e8SKurt Hackel 		cond_resched_lock(&dlm->spinlock);
3106714d8e8SKurt Hackel 	}
3116714d8e8SKurt Hackel 
3126714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
3136714d8e8SKurt Hackel }
3146714d8e8SKurt Hackel 
3156714d8e8SKurt Hackel static void dlm_shuffle_lists(struct dlm_ctxt *dlm,
3166714d8e8SKurt Hackel 			      struct dlm_lock_resource *res)
3176714d8e8SKurt Hackel {
3186714d8e8SKurt Hackel 	struct dlm_lock *lock, *target;
3196714d8e8SKurt Hackel 	int can_grant = 1;
3206714d8e8SKurt Hackel 
3218e17d16fSSunil Mushran 	/*
3228e17d16fSSunil Mushran 	 * Because this function is called with the lockres
3236714d8e8SKurt Hackel 	 * spinlock, and because we know that it is not migrating/
3246714d8e8SKurt Hackel 	 * recovering/in-progress, it is fine to reserve asts and
3258e17d16fSSunil Mushran 	 * basts right before queueing them all throughout
3268e17d16fSSunil Mushran 	 */
327d9ef7522SWengang Wang 	assert_spin_locked(&dlm->ast_lock);
3286714d8e8SKurt Hackel 	assert_spin_locked(&res->spinlock);
3296714d8e8SKurt Hackel 	BUG_ON((res->state & (DLM_LOCK_RES_MIGRATING|
3306714d8e8SKurt Hackel 			      DLM_LOCK_RES_RECOVERING|
3316714d8e8SKurt Hackel 			      DLM_LOCK_RES_IN_PROGRESS)));
3326714d8e8SKurt Hackel 
3336714d8e8SKurt Hackel converting:
3346714d8e8SKurt Hackel 	if (list_empty(&res->converting))
3356714d8e8SKurt Hackel 		goto blocked;
3368e17d16fSSunil Mushran 	mlog(0, "%s: res %.*s has locks on the convert queue\n", dlm->name,
3378e17d16fSSunil Mushran 	     res->lockname.len, res->lockname.name);
3386714d8e8SKurt Hackel 
3396714d8e8SKurt Hackel 	target = list_entry(res->converting.next, struct dlm_lock, list);
3406714d8e8SKurt Hackel 	if (target->ml.convert_type == LKM_IVMODE) {
3418e17d16fSSunil Mushran 		mlog(ML_ERROR, "%s: res %.*s converting lock to invalid mode\n",
3428e17d16fSSunil Mushran 		     dlm->name, res->lockname.len, res->lockname.name);
3436714d8e8SKurt Hackel 		BUG();
3446714d8e8SKurt Hackel 	}
345df53cd3bSDong Fang 	list_for_each_entry(lock, &res->granted, list) {
3466714d8e8SKurt Hackel 		if (lock==target)
3476714d8e8SKurt Hackel 			continue;
3486714d8e8SKurt Hackel 		if (!dlm_lock_compatible(lock->ml.type,
3496714d8e8SKurt Hackel 					 target->ml.convert_type)) {
3506714d8e8SKurt Hackel 			can_grant = 0;
3516714d8e8SKurt Hackel 			/* queue the BAST if not already */
3526714d8e8SKurt Hackel 			if (lock->ml.highest_blocked == LKM_IVMODE) {
3536714d8e8SKurt Hackel 				__dlm_lockres_reserve_ast(res);
354d9ef7522SWengang Wang 				__dlm_queue_bast(dlm, lock);
3556714d8e8SKurt Hackel 			}
3566714d8e8SKurt Hackel 			/* update the highest_blocked if needed */
3576714d8e8SKurt Hackel 			if (lock->ml.highest_blocked < target->ml.convert_type)
3586714d8e8SKurt Hackel 				lock->ml.highest_blocked =
3596714d8e8SKurt Hackel 					target->ml.convert_type;
3606714d8e8SKurt Hackel 		}
3616714d8e8SKurt Hackel 	}
362df53cd3bSDong Fang 
363df53cd3bSDong Fang 	list_for_each_entry(lock, &res->converting, list) {
3646714d8e8SKurt Hackel 		if (lock==target)
3656714d8e8SKurt Hackel 			continue;
3666714d8e8SKurt Hackel 		if (!dlm_lock_compatible(lock->ml.type,
3676714d8e8SKurt Hackel 					 target->ml.convert_type)) {
3686714d8e8SKurt Hackel 			can_grant = 0;
3696714d8e8SKurt Hackel 			if (lock->ml.highest_blocked == LKM_IVMODE) {
3706714d8e8SKurt Hackel 				__dlm_lockres_reserve_ast(res);
371d9ef7522SWengang Wang 				__dlm_queue_bast(dlm, lock);
3726714d8e8SKurt Hackel 			}
3736714d8e8SKurt Hackel 			if (lock->ml.highest_blocked < target->ml.convert_type)
3746714d8e8SKurt Hackel 				lock->ml.highest_blocked =
3756714d8e8SKurt Hackel 					target->ml.convert_type;
3766714d8e8SKurt Hackel 		}
3776714d8e8SKurt Hackel 	}
3786714d8e8SKurt Hackel 
3796714d8e8SKurt Hackel 	/* we can convert the lock */
3806714d8e8SKurt Hackel 	if (can_grant) {
3816714d8e8SKurt Hackel 		spin_lock(&target->spinlock);
3826714d8e8SKurt Hackel 		BUG_ON(target->ml.highest_blocked != LKM_IVMODE);
3836714d8e8SKurt Hackel 
3848e17d16fSSunil Mushran 		mlog(0, "%s: res %.*s, AST for Converting lock %u:%llu, type "
3858e17d16fSSunil Mushran 		     "%d => %d, node %u\n", dlm->name, res->lockname.len,
3868e17d16fSSunil Mushran 		     res->lockname.name,
3878e17d16fSSunil Mushran 		     dlm_get_lock_cookie_node(be64_to_cpu(target->ml.cookie)),
3888e17d16fSSunil Mushran 		     dlm_get_lock_cookie_seq(be64_to_cpu(target->ml.cookie)),
3898e17d16fSSunil Mushran 		     target->ml.type,
3906714d8e8SKurt Hackel 		     target->ml.convert_type, target->ml.node);
3916714d8e8SKurt Hackel 
3926714d8e8SKurt Hackel 		target->ml.type = target->ml.convert_type;
3936714d8e8SKurt Hackel 		target->ml.convert_type = LKM_IVMODE;
394f116629dSAkinobu Mita 		list_move_tail(&target->list, &res->granted);
3956714d8e8SKurt Hackel 
3966714d8e8SKurt Hackel 		BUG_ON(!target->lksb);
3976714d8e8SKurt Hackel 		target->lksb->status = DLM_NORMAL;
3986714d8e8SKurt Hackel 
3996714d8e8SKurt Hackel 		spin_unlock(&target->spinlock);
4006714d8e8SKurt Hackel 
4016714d8e8SKurt Hackel 		__dlm_lockres_reserve_ast(res);
402d9ef7522SWengang Wang 		__dlm_queue_ast(dlm, target);
4036714d8e8SKurt Hackel 		/* go back and check for more */
4046714d8e8SKurt Hackel 		goto converting;
4056714d8e8SKurt Hackel 	}
4066714d8e8SKurt Hackel 
4076714d8e8SKurt Hackel blocked:
4086714d8e8SKurt Hackel 	if (list_empty(&res->blocked))
4096714d8e8SKurt Hackel 		goto leave;
4106714d8e8SKurt Hackel 	target = list_entry(res->blocked.next, struct dlm_lock, list);
4116714d8e8SKurt Hackel 
412df53cd3bSDong Fang 	list_for_each_entry(lock, &res->granted, list) {
4136714d8e8SKurt Hackel 		if (lock==target)
4146714d8e8SKurt Hackel 			continue;
4156714d8e8SKurt Hackel 		if (!dlm_lock_compatible(lock->ml.type, target->ml.type)) {
4166714d8e8SKurt Hackel 			can_grant = 0;
4176714d8e8SKurt Hackel 			if (lock->ml.highest_blocked == LKM_IVMODE) {
4186714d8e8SKurt Hackel 				__dlm_lockres_reserve_ast(res);
419d9ef7522SWengang Wang 				__dlm_queue_bast(dlm, lock);
4206714d8e8SKurt Hackel 			}
4216714d8e8SKurt Hackel 			if (lock->ml.highest_blocked < target->ml.type)
4226714d8e8SKurt Hackel 				lock->ml.highest_blocked = target->ml.type;
4236714d8e8SKurt Hackel 		}
4246714d8e8SKurt Hackel 	}
4256714d8e8SKurt Hackel 
426df53cd3bSDong Fang 	list_for_each_entry(lock, &res->converting, list) {
4276714d8e8SKurt Hackel 		if (lock==target)
4286714d8e8SKurt Hackel 			continue;
4296714d8e8SKurt Hackel 		if (!dlm_lock_compatible(lock->ml.type, target->ml.type)) {
4306714d8e8SKurt Hackel 			can_grant = 0;
4316714d8e8SKurt Hackel 			if (lock->ml.highest_blocked == LKM_IVMODE) {
4326714d8e8SKurt Hackel 				__dlm_lockres_reserve_ast(res);
433d9ef7522SWengang Wang 				__dlm_queue_bast(dlm, lock);
4346714d8e8SKurt Hackel 			}
4356714d8e8SKurt Hackel 			if (lock->ml.highest_blocked < target->ml.type)
4366714d8e8SKurt Hackel 				lock->ml.highest_blocked = target->ml.type;
4376714d8e8SKurt Hackel 		}
4386714d8e8SKurt Hackel 	}
4396714d8e8SKurt Hackel 
4406714d8e8SKurt Hackel 	/* we can grant the blocked lock (only
4416714d8e8SKurt Hackel 	 * possible if converting list empty) */
4426714d8e8SKurt Hackel 	if (can_grant) {
4436714d8e8SKurt Hackel 		spin_lock(&target->spinlock);
4446714d8e8SKurt Hackel 		BUG_ON(target->ml.highest_blocked != LKM_IVMODE);
4456714d8e8SKurt Hackel 
4468e17d16fSSunil Mushran 		mlog(0, "%s: res %.*s, AST for Blocked lock %u:%llu, type %d, "
4478e17d16fSSunil Mushran 		     "node %u\n", dlm->name, res->lockname.len,
4488e17d16fSSunil Mushran 		     res->lockname.name,
4498e17d16fSSunil Mushran 		     dlm_get_lock_cookie_node(be64_to_cpu(target->ml.cookie)),
4508e17d16fSSunil Mushran 		     dlm_get_lock_cookie_seq(be64_to_cpu(target->ml.cookie)),
4516714d8e8SKurt Hackel 		     target->ml.type, target->ml.node);
4526714d8e8SKurt Hackel 
4538e17d16fSSunil Mushran 		/* target->ml.type is already correct */
454f116629dSAkinobu Mita 		list_move_tail(&target->list, &res->granted);
4556714d8e8SKurt Hackel 
4566714d8e8SKurt Hackel 		BUG_ON(!target->lksb);
4576714d8e8SKurt Hackel 		target->lksb->status = DLM_NORMAL;
4586714d8e8SKurt Hackel 
4596714d8e8SKurt Hackel 		spin_unlock(&target->spinlock);
4606714d8e8SKurt Hackel 
4616714d8e8SKurt Hackel 		__dlm_lockres_reserve_ast(res);
462d9ef7522SWengang Wang 		__dlm_queue_ast(dlm, target);
4636714d8e8SKurt Hackel 		/* go back and check for more */
4646714d8e8SKurt Hackel 		goto converting;
4656714d8e8SKurt Hackel 	}
4666714d8e8SKurt Hackel 
4676714d8e8SKurt Hackel leave:
4686714d8e8SKurt Hackel 	return;
4696714d8e8SKurt Hackel }
4706714d8e8SKurt Hackel 
4716714d8e8SKurt Hackel /* must have NO locks when calling this with res !=NULL * */
4726714d8e8SKurt Hackel void dlm_kick_thread(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
4736714d8e8SKurt Hackel {
4746714d8e8SKurt Hackel 	if (res) {
4756714d8e8SKurt Hackel 		spin_lock(&dlm->spinlock);
4766714d8e8SKurt Hackel 		spin_lock(&res->spinlock);
4776714d8e8SKurt Hackel 		__dlm_dirty_lockres(dlm, res);
4786714d8e8SKurt Hackel 		spin_unlock(&res->spinlock);
4796714d8e8SKurt Hackel 		spin_unlock(&dlm->spinlock);
4806714d8e8SKurt Hackel 	}
4816714d8e8SKurt Hackel 	wake_up(&dlm->dlm_thread_wq);
4826714d8e8SKurt Hackel }
4836714d8e8SKurt Hackel 
4846714d8e8SKurt Hackel void __dlm_dirty_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
4856714d8e8SKurt Hackel {
4866714d8e8SKurt Hackel 	assert_spin_locked(&dlm->spinlock);
4876714d8e8SKurt Hackel 	assert_spin_locked(&res->spinlock);
4886714d8e8SKurt Hackel 
4896714d8e8SKurt Hackel 	/* don't shuffle secondary queues */
490ddc09c8dSKurt Hackel 	if ((res->owner == dlm->node_num)) {
491ddc09c8dSKurt Hackel 		if (res->state & (DLM_LOCK_RES_MIGRATING |
492ddc09c8dSKurt Hackel 				  DLM_LOCK_RES_BLOCK_DIRTY))
493ddc09c8dSKurt Hackel 		    return;
494ddc09c8dSKurt Hackel 
495ddc09c8dSKurt Hackel 		if (list_empty(&res->dirty)) {
4966ff06a93SKurt Hackel 			/* ref for dirty_list */
4976ff06a93SKurt Hackel 			dlm_lockres_get(res);
4986714d8e8SKurt Hackel 			list_add_tail(&res->dirty, &dlm->dirty_list);
4996714d8e8SKurt Hackel 			res->state |= DLM_LOCK_RES_DIRTY;
5006714d8e8SKurt Hackel 		}
5016714d8e8SKurt Hackel 	}
5028e17d16fSSunil Mushran 
5038e17d16fSSunil Mushran 	mlog(0, "%s: res %.*s\n", dlm->name, res->lockname.len,
5048e17d16fSSunil Mushran 	     res->lockname.name);
505ddc09c8dSKurt Hackel }
5066714d8e8SKurt Hackel 
5076714d8e8SKurt Hackel 
5086714d8e8SKurt Hackel /* Launch the NM thread for the mounted volume */
5096714d8e8SKurt Hackel int dlm_launch_thread(struct dlm_ctxt *dlm)
5106714d8e8SKurt Hackel {
5118e17d16fSSunil Mushran 	mlog(0, "Starting dlm_thread...\n");
5126714d8e8SKurt Hackel 
5135afc44e2SJoseph Qi 	dlm->dlm_thread_task = kthread_run(dlm_thread, dlm, "dlm-%s",
5145afc44e2SJoseph Qi 			dlm->name);
5156714d8e8SKurt Hackel 	if (IS_ERR(dlm->dlm_thread_task)) {
5166714d8e8SKurt Hackel 		mlog_errno(PTR_ERR(dlm->dlm_thread_task));
5176714d8e8SKurt Hackel 		dlm->dlm_thread_task = NULL;
5186714d8e8SKurt Hackel 		return -EINVAL;
5196714d8e8SKurt Hackel 	}
5206714d8e8SKurt Hackel 
5216714d8e8SKurt Hackel 	return 0;
5226714d8e8SKurt Hackel }
5236714d8e8SKurt Hackel 
5246714d8e8SKurt Hackel void dlm_complete_thread(struct dlm_ctxt *dlm)
5256714d8e8SKurt Hackel {
5266714d8e8SKurt Hackel 	if (dlm->dlm_thread_task) {
5278e17d16fSSunil Mushran 		mlog(ML_KTHREAD, "Waiting for dlm thread to exit\n");
5286714d8e8SKurt Hackel 		kthread_stop(dlm->dlm_thread_task);
5296714d8e8SKurt Hackel 		dlm->dlm_thread_task = NULL;
5306714d8e8SKurt Hackel 	}
5316714d8e8SKurt Hackel }
5326714d8e8SKurt Hackel 
5336714d8e8SKurt Hackel static int dlm_dirty_list_empty(struct dlm_ctxt *dlm)
5346714d8e8SKurt Hackel {
5356714d8e8SKurt Hackel 	int empty;
5366714d8e8SKurt Hackel 
5376714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
5386714d8e8SKurt Hackel 	empty = list_empty(&dlm->dirty_list);
5396714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
5406714d8e8SKurt Hackel 
5416714d8e8SKurt Hackel 	return empty;
5426714d8e8SKurt Hackel }
5436714d8e8SKurt Hackel 
5446714d8e8SKurt Hackel static void dlm_flush_asts(struct dlm_ctxt *dlm)
5456714d8e8SKurt Hackel {
5466714d8e8SKurt Hackel 	int ret;
5476714d8e8SKurt Hackel 	struct dlm_lock *lock;
5486714d8e8SKurt Hackel 	struct dlm_lock_resource *res;
5496714d8e8SKurt Hackel 	u8 hi;
5506714d8e8SKurt Hackel 
5516714d8e8SKurt Hackel 	spin_lock(&dlm->ast_lock);
5526714d8e8SKurt Hackel 	while (!list_empty(&dlm->pending_asts)) {
5536714d8e8SKurt Hackel 		lock = list_entry(dlm->pending_asts.next,
5546714d8e8SKurt Hackel 				  struct dlm_lock, ast_list);
5556714d8e8SKurt Hackel 		/* get an extra ref on lock */
5566714d8e8SKurt Hackel 		dlm_lock_get(lock);
5576714d8e8SKurt Hackel 		res = lock->lockres;
5588e17d16fSSunil Mushran 		mlog(0, "%s: res %.*s, Flush AST for lock %u:%llu, type %d, "
5598e17d16fSSunil Mushran 		     "node %u\n", dlm->name, res->lockname.len,
5608e17d16fSSunil Mushran 		     res->lockname.name,
5618e17d16fSSunil Mushran 		     dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
5628e17d16fSSunil Mushran 		     dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)),
5638e17d16fSSunil Mushran 		     lock->ml.type, lock->ml.node);
5646714d8e8SKurt Hackel 
5656714d8e8SKurt Hackel 		BUG_ON(!lock->ast_pending);
5666714d8e8SKurt Hackel 
5676714d8e8SKurt Hackel 		/* remove from list (including ref) */
5686714d8e8SKurt Hackel 		list_del_init(&lock->ast_list);
5696714d8e8SKurt Hackel 		dlm_lock_put(lock);
5706714d8e8SKurt Hackel 		spin_unlock(&dlm->ast_lock);
5716714d8e8SKurt Hackel 
5726714d8e8SKurt Hackel 		if (lock->ml.node != dlm->node_num) {
5736714d8e8SKurt Hackel 			ret = dlm_do_remote_ast(dlm, res, lock);
5746714d8e8SKurt Hackel 			if (ret < 0)
5756714d8e8SKurt Hackel 				mlog_errno(ret);
5766714d8e8SKurt Hackel 		} else
5776714d8e8SKurt Hackel 			dlm_do_local_ast(dlm, res, lock);
5786714d8e8SKurt Hackel 
5796714d8e8SKurt Hackel 		spin_lock(&dlm->ast_lock);
5806714d8e8SKurt Hackel 
5816714d8e8SKurt Hackel 		/* possible that another ast was queued while
5826714d8e8SKurt Hackel 		 * we were delivering the last one */
5836714d8e8SKurt Hackel 		if (!list_empty(&lock->ast_list)) {
5848e17d16fSSunil Mushran 			mlog(0, "%s: res %.*s, AST queued while flushing last "
5858e17d16fSSunil Mushran 			     "one\n", dlm->name, res->lockname.len,
5868e17d16fSSunil Mushran 			     res->lockname.name);
5876714d8e8SKurt Hackel 		} else
5886714d8e8SKurt Hackel 			lock->ast_pending = 0;
5896714d8e8SKurt Hackel 
5906714d8e8SKurt Hackel 		/* drop the extra ref.
5916714d8e8SKurt Hackel 		 * this may drop it completely. */
5926714d8e8SKurt Hackel 		dlm_lock_put(lock);
5936714d8e8SKurt Hackel 		dlm_lockres_release_ast(dlm, res);
5946714d8e8SKurt Hackel 	}
5956714d8e8SKurt Hackel 
5966714d8e8SKurt Hackel 	while (!list_empty(&dlm->pending_basts)) {
5976714d8e8SKurt Hackel 		lock = list_entry(dlm->pending_basts.next,
5986714d8e8SKurt Hackel 				  struct dlm_lock, bast_list);
5996714d8e8SKurt Hackel 		/* get an extra ref on lock */
6006714d8e8SKurt Hackel 		dlm_lock_get(lock);
6016714d8e8SKurt Hackel 		res = lock->lockres;
6026714d8e8SKurt Hackel 
6036714d8e8SKurt Hackel 		BUG_ON(!lock->bast_pending);
6046714d8e8SKurt Hackel 
6056714d8e8SKurt Hackel 		/* get the highest blocked lock, and reset */
6066714d8e8SKurt Hackel 		spin_lock(&lock->spinlock);
6076714d8e8SKurt Hackel 		BUG_ON(lock->ml.highest_blocked <= LKM_IVMODE);
6086714d8e8SKurt Hackel 		hi = lock->ml.highest_blocked;
6096714d8e8SKurt Hackel 		lock->ml.highest_blocked = LKM_IVMODE;
6106714d8e8SKurt Hackel 		spin_unlock(&lock->spinlock);
6116714d8e8SKurt Hackel 
6126714d8e8SKurt Hackel 		/* remove from list (including ref) */
6136714d8e8SKurt Hackel 		list_del_init(&lock->bast_list);
6146714d8e8SKurt Hackel 		dlm_lock_put(lock);
6156714d8e8SKurt Hackel 		spin_unlock(&dlm->ast_lock);
6166714d8e8SKurt Hackel 
6178e17d16fSSunil Mushran 		mlog(0, "%s: res %.*s, Flush BAST for lock %u:%llu, "
6188e17d16fSSunil Mushran 		     "blocked %d, node %u\n",
6198e17d16fSSunil Mushran 		     dlm->name, res->lockname.len, res->lockname.name,
6208e17d16fSSunil Mushran 		     dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
6218e17d16fSSunil Mushran 		     dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)),
6228e17d16fSSunil Mushran 		     hi, lock->ml.node);
6236714d8e8SKurt Hackel 
6246714d8e8SKurt Hackel 		if (lock->ml.node != dlm->node_num) {
6256714d8e8SKurt Hackel 			ret = dlm_send_proxy_bast(dlm, res, lock, hi);
6266714d8e8SKurt Hackel 			if (ret < 0)
6276714d8e8SKurt Hackel 				mlog_errno(ret);
6286714d8e8SKurt Hackel 		} else
6296714d8e8SKurt Hackel 			dlm_do_local_bast(dlm, res, lock, hi);
6306714d8e8SKurt Hackel 
6316714d8e8SKurt Hackel 		spin_lock(&dlm->ast_lock);
6326714d8e8SKurt Hackel 
6336714d8e8SKurt Hackel 		/* possible that another bast was queued while
6346714d8e8SKurt Hackel 		 * we were delivering the last one */
6356714d8e8SKurt Hackel 		if (!list_empty(&lock->bast_list)) {
6368e17d16fSSunil Mushran 			mlog(0, "%s: res %.*s, BAST queued while flushing last "
6378e17d16fSSunil Mushran 			     "one\n", dlm->name, res->lockname.len,
6388e17d16fSSunil Mushran 			     res->lockname.name);
6396714d8e8SKurt Hackel 		} else
6406714d8e8SKurt Hackel 			lock->bast_pending = 0;
6416714d8e8SKurt Hackel 
6426714d8e8SKurt Hackel 		/* drop the extra ref.
6436714d8e8SKurt Hackel 		 * this may drop it completely. */
6446714d8e8SKurt Hackel 		dlm_lock_put(lock);
6456714d8e8SKurt Hackel 		dlm_lockres_release_ast(dlm, res);
6466714d8e8SKurt Hackel 	}
6476714d8e8SKurt Hackel 	wake_up(&dlm->ast_wq);
6486714d8e8SKurt Hackel 	spin_unlock(&dlm->ast_lock);
6496714d8e8SKurt Hackel }
6506714d8e8SKurt Hackel 
6516714d8e8SKurt Hackel 
6526714d8e8SKurt Hackel #define DLM_THREAD_TIMEOUT_MS (4 * 1000)
6536714d8e8SKurt Hackel #define DLM_THREAD_MAX_DIRTY  100
6546714d8e8SKurt Hackel #define DLM_THREAD_MAX_ASTS   10
6556714d8e8SKurt Hackel 
6566714d8e8SKurt Hackel static int dlm_thread(void *data)
6576714d8e8SKurt Hackel {
6586714d8e8SKurt Hackel 	struct dlm_lock_resource *res;
6596714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
6606714d8e8SKurt Hackel 	unsigned long timeout = msecs_to_jiffies(DLM_THREAD_TIMEOUT_MS);
6616714d8e8SKurt Hackel 
6626714d8e8SKurt Hackel 	mlog(0, "dlm thread running for %s...\n", dlm->name);
6636714d8e8SKurt Hackel 
6646714d8e8SKurt Hackel 	while (!kthread_should_stop()) {
6656714d8e8SKurt Hackel 		int n = DLM_THREAD_MAX_DIRTY;
6666714d8e8SKurt Hackel 
6676714d8e8SKurt Hackel 		/* dlm_shutting_down is very point-in-time, but that
6686714d8e8SKurt Hackel 		 * doesn't matter as we'll just loop back around if we
6696714d8e8SKurt Hackel 		 * get false on the leading edge of a state
6706714d8e8SKurt Hackel 		 * transition. */
6716714d8e8SKurt Hackel 		dlm_run_purge_list(dlm, dlm_shutting_down(dlm));
6726714d8e8SKurt Hackel 
6736714d8e8SKurt Hackel 		/* We really don't want to hold dlm->spinlock while
6746714d8e8SKurt Hackel 		 * calling dlm_shuffle_lists on each lockres that
6756714d8e8SKurt Hackel 		 * needs to have its queues adjusted and AST/BASTs
6766714d8e8SKurt Hackel 		 * run.  So let's pull each entry off the dirty_list
6776714d8e8SKurt Hackel 		 * and drop dlm->spinlock ASAP.  Once off the list,
6786714d8e8SKurt Hackel 		 * res->spinlock needs to be taken again to protect
6796714d8e8SKurt Hackel 		 * the queues while calling dlm_shuffle_lists.  */
6806714d8e8SKurt Hackel 		spin_lock(&dlm->spinlock);
6816714d8e8SKurt Hackel 		while (!list_empty(&dlm->dirty_list)) {
6826714d8e8SKurt Hackel 			int delay = 0;
6836714d8e8SKurt Hackel 			res = list_entry(dlm->dirty_list.next,
6846714d8e8SKurt Hackel 					 struct dlm_lock_resource, dirty);
6856714d8e8SKurt Hackel 
6866714d8e8SKurt Hackel 			/* peel a lockres off, remove it from the list,
6876714d8e8SKurt Hackel 			 * unset the dirty flag and drop the dlm lock */
6886714d8e8SKurt Hackel 			BUG_ON(!res);
6896714d8e8SKurt Hackel 			dlm_lockres_get(res);
6906714d8e8SKurt Hackel 
6916714d8e8SKurt Hackel 			spin_lock(&res->spinlock);
692ddc09c8dSKurt Hackel 			/* We clear the DLM_LOCK_RES_DIRTY state once we shuffle lists below */
6936714d8e8SKurt Hackel 			list_del_init(&res->dirty);
6946714d8e8SKurt Hackel 			spin_unlock(&res->spinlock);
6956714d8e8SKurt Hackel 			spin_unlock(&dlm->spinlock);
6966ff06a93SKurt Hackel 			/* Drop dirty_list ref */
6976ff06a93SKurt Hackel 			dlm_lockres_put(res);
6986714d8e8SKurt Hackel 
6996714d8e8SKurt Hackel 		 	/* lockres can be re-dirtied/re-added to the
7006714d8e8SKurt Hackel 			 * dirty_list in this gap, but that is ok */
7016714d8e8SKurt Hackel 
702d9ef7522SWengang Wang 			spin_lock(&dlm->ast_lock);
7036714d8e8SKurt Hackel 			spin_lock(&res->spinlock);
7046714d8e8SKurt Hackel 			if (res->owner != dlm->node_num) {
7056714d8e8SKurt Hackel 				__dlm_print_one_lock_resource(res);
7068e17d16fSSunil Mushran 				mlog(ML_ERROR, "%s: inprog %d, mig %d, reco %d,"
7078e17d16fSSunil Mushran 				     " dirty %d\n", dlm->name,
7088e17d16fSSunil Mushran 				     !!(res->state & DLM_LOCK_RES_IN_PROGRESS),
7098e17d16fSSunil Mushran 				     !!(res->state & DLM_LOCK_RES_MIGRATING),
7108e17d16fSSunil Mushran 				     !!(res->state & DLM_LOCK_RES_RECOVERING),
7118e17d16fSSunil Mushran 				     !!(res->state & DLM_LOCK_RES_DIRTY));
7126714d8e8SKurt Hackel 			}
7136714d8e8SKurt Hackel 			BUG_ON(res->owner != dlm->node_num);
7146714d8e8SKurt Hackel 
7156714d8e8SKurt Hackel 			/* it is now ok to move lockreses in these states
7166714d8e8SKurt Hackel 			 * to the dirty list, assuming that they will only be
7176714d8e8SKurt Hackel 			 * dirty for a short while. */
718ddc09c8dSKurt Hackel 			BUG_ON(res->state & DLM_LOCK_RES_MIGRATING);
7196714d8e8SKurt Hackel 			if (res->state & (DLM_LOCK_RES_IN_PROGRESS |
720814ce694SJiufei Xue 					  DLM_LOCK_RES_RECOVERING |
721814ce694SJiufei Xue 					  DLM_LOCK_RES_RECOVERY_WAITING)) {
7226714d8e8SKurt Hackel 				/* move it to the tail and keep going */
723ddc09c8dSKurt Hackel 				res->state &= ~DLM_LOCK_RES_DIRTY;
7246714d8e8SKurt Hackel 				spin_unlock(&res->spinlock);
725d9ef7522SWengang Wang 				spin_unlock(&dlm->ast_lock);
7268e17d16fSSunil Mushran 				mlog(0, "%s: res %.*s, inprogress, delay list "
7278e17d16fSSunil Mushran 				     "shuffle, state %d\n", dlm->name,
7286714d8e8SKurt Hackel 				     res->lockname.len, res->lockname.name,
7296714d8e8SKurt Hackel 				     res->state);
7306714d8e8SKurt Hackel 				delay = 1;
7316714d8e8SKurt Hackel 				goto in_progress;
7326714d8e8SKurt Hackel 			}
7336714d8e8SKurt Hackel 
7346714d8e8SKurt Hackel 			/* at this point the lockres is not migrating/
7356714d8e8SKurt Hackel 			 * recovering/in-progress.  we have the lockres
7366714d8e8SKurt Hackel 			 * spinlock and do NOT have the dlm lock.
7376714d8e8SKurt Hackel 			 * safe to reserve/queue asts and run the lists. */
7386714d8e8SKurt Hackel 
7396714d8e8SKurt Hackel 			/* called while holding lockres lock */
7406714d8e8SKurt Hackel 			dlm_shuffle_lists(dlm, res);
741ddc09c8dSKurt Hackel 			res->state &= ~DLM_LOCK_RES_DIRTY;
7426714d8e8SKurt Hackel 			spin_unlock(&res->spinlock);
743d9ef7522SWengang Wang 			spin_unlock(&dlm->ast_lock);
7446714d8e8SKurt Hackel 
7456714d8e8SKurt Hackel 			dlm_lockres_calc_usage(dlm, res);
7466714d8e8SKurt Hackel 
7476714d8e8SKurt Hackel in_progress:
7486714d8e8SKurt Hackel 
7496714d8e8SKurt Hackel 			spin_lock(&dlm->spinlock);
7506714d8e8SKurt Hackel 			/* if the lock was in-progress, stick
7516714d8e8SKurt Hackel 			 * it on the back of the list */
7526714d8e8SKurt Hackel 			if (delay) {
7536714d8e8SKurt Hackel 				spin_lock(&res->spinlock);
754ddc09c8dSKurt Hackel 				__dlm_dirty_lockres(dlm, res);
7556714d8e8SKurt Hackel 				spin_unlock(&res->spinlock);
7566714d8e8SKurt Hackel 			}
7576714d8e8SKurt Hackel 			dlm_lockres_put(res);
7586714d8e8SKurt Hackel 
7596714d8e8SKurt Hackel 			/* unlikely, but we may need to give time to
7606714d8e8SKurt Hackel 			 * other tasks */
7616714d8e8SKurt Hackel 			if (!--n) {
7628e17d16fSSunil Mushran 				mlog(0, "%s: Throttling dlm thread\n",
7638e17d16fSSunil Mushran 				     dlm->name);
7646714d8e8SKurt Hackel 				break;
7656714d8e8SKurt Hackel 			}
7666714d8e8SKurt Hackel 		}
7676714d8e8SKurt Hackel 
7686714d8e8SKurt Hackel 		spin_unlock(&dlm->spinlock);
7696714d8e8SKurt Hackel 		dlm_flush_asts(dlm);
7706714d8e8SKurt Hackel 
7716714d8e8SKurt Hackel 		/* yield and continue right away if there is more work to do */
7726714d8e8SKurt Hackel 		if (!n) {
773f85cd47aSKurt Hackel 			cond_resched();
7746714d8e8SKurt Hackel 			continue;
7756714d8e8SKurt Hackel 		}
7766714d8e8SKurt Hackel 
7776714d8e8SKurt Hackel 		wait_event_interruptible_timeout(dlm->dlm_thread_wq,
7786714d8e8SKurt Hackel 						 !dlm_dirty_list_empty(dlm) ||
7796714d8e8SKurt Hackel 						 kthread_should_stop(),
7806714d8e8SKurt Hackel 						 timeout);
7816714d8e8SKurt Hackel 	}
7826714d8e8SKurt Hackel 
7836714d8e8SKurt Hackel 	mlog(0, "quitting DLM thread\n");
7846714d8e8SKurt Hackel 	return 0;
7856714d8e8SKurt Hackel }
786