xref: /openbmc/linux/fs/ocfs2/dlm/dlmthread.c (revision ac4fef4d)
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 
109a524812bSWengang Wang 	if (res->state & DLM_LOCK_RES_RECOVERING)
110a524812bSWengang Wang 		return 0;
111a524812bSWengang Wang 
112ff0a522eSSunil Mushran 	/* Another node has this resource with this node as the master */
113a524812bSWengang Wang 	bit = find_next_bit(res->refmap, O2NM_MAX_NODES, 0);
114a524812bSWengang Wang 	if (bit < O2NM_MAX_NODES)
115a524812bSWengang Wang 		return 0;
116a524812bSWengang Wang 
117ba2bf218SKurt Hackel 	return 1;
118ba2bf218SKurt Hackel }
1196714d8e8SKurt Hackel 
1206714d8e8SKurt Hackel 
1216714d8e8SKurt Hackel /* Call whenever you may have added or deleted something from one of
1226714d8e8SKurt Hackel  * the lockres queue's. This will figure out whether it belongs on the
1236714d8e8SKurt Hackel  * unused list or not and does the appropriate thing. */
1246714d8e8SKurt Hackel void __dlm_lockres_calc_usage(struct dlm_ctxt *dlm,
1256714d8e8SKurt Hackel 			      struct dlm_lock_resource *res)
1266714d8e8SKurt Hackel {
1276714d8e8SKurt Hackel 	assert_spin_locked(&dlm->spinlock);
1286714d8e8SKurt Hackel 	assert_spin_locked(&res->spinlock);
1296714d8e8SKurt Hackel 
1306714d8e8SKurt Hackel 	if (__dlm_lockres_unused(res)){
1316714d8e8SKurt Hackel 		if (list_empty(&res->purge)) {
1328e17d16fSSunil Mushran 			mlog(0, "%s: Adding res %.*s to purge list\n",
1338e17d16fSSunil Mushran 			     dlm->name, res->lockname.len, res->lockname.name);
1346714d8e8SKurt Hackel 
1356714d8e8SKurt Hackel 			res->last_used = jiffies;
136ba2bf218SKurt Hackel 			dlm_lockres_get(res);
1376714d8e8SKurt Hackel 			list_add_tail(&res->purge, &dlm->purge_list);
1386714d8e8SKurt Hackel 			dlm->purge_count++;
1396714d8e8SKurt Hackel 		}
1406714d8e8SKurt Hackel 	} else if (!list_empty(&res->purge)) {
1418e17d16fSSunil Mushran 		mlog(0, "%s: Removing res %.*s from purge list\n",
1428e17d16fSSunil Mushran 		     dlm->name, res->lockname.len, res->lockname.name);
1436714d8e8SKurt Hackel 
1446714d8e8SKurt Hackel 		list_del_init(&res->purge);
145ba2bf218SKurt Hackel 		dlm_lockres_put(res);
1466714d8e8SKurt Hackel 		dlm->purge_count--;
1476714d8e8SKurt Hackel 	}
1486714d8e8SKurt Hackel }
1496714d8e8SKurt Hackel 
1506714d8e8SKurt Hackel void dlm_lockres_calc_usage(struct dlm_ctxt *dlm,
1516714d8e8SKurt Hackel 			    struct dlm_lock_resource *res)
1526714d8e8SKurt Hackel {
1536714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
1546714d8e8SKurt Hackel 	spin_lock(&res->spinlock);
1556714d8e8SKurt Hackel 
1566714d8e8SKurt Hackel 	__dlm_lockres_calc_usage(dlm, res);
1576714d8e8SKurt Hackel 
1586714d8e8SKurt Hackel 	spin_unlock(&res->spinlock);
1596714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
1606714d8e8SKurt Hackel }
1616714d8e8SKurt Hackel 
1627beaf243SSrinivas Eeda static void dlm_purge_lockres(struct dlm_ctxt *dlm,
163faf0ec9fSAdrian Bunk 			     struct dlm_lock_resource *res)
1646714d8e8SKurt Hackel {
1656714d8e8SKurt Hackel 	int master;
166ba2bf218SKurt Hackel 	int ret = 0;
1676714d8e8SKurt Hackel 
1687beaf243SSrinivas Eeda 	assert_spin_locked(&dlm->spinlock);
1697beaf243SSrinivas Eeda 	assert_spin_locked(&res->spinlock);
170516b7e52SSunil Mushran 
171ba2bf218SKurt Hackel 	master = (res->owner == dlm->node_num);
172516b7e52SSunil Mushran 
1738e17d16fSSunil Mushran 	mlog(0, "%s: Purging res %.*s, master %d\n", dlm->name,
1748e17d16fSSunil Mushran 	     res->lockname.len, res->lockname.name, master);
175ba2bf218SKurt Hackel 
176ba2bf218SKurt Hackel 	if (!master) {
1777beaf243SSrinivas Eeda 		res->state |= DLM_LOCK_RES_DROPPING_REF;
178c824c3c7SSunil Mushran 		/* drop spinlock...  retake below */
1797beaf243SSrinivas Eeda 		spin_unlock(&res->spinlock);
180c824c3c7SSunil Mushran 		spin_unlock(&dlm->spinlock);
181c824c3c7SSunil Mushran 
1823b8118cfSKurt Hackel 		spin_lock(&res->spinlock);
1833b8118cfSKurt Hackel 		/* This ensures that clear refmap is sent after the set */
1847dc102b7SSunil Mushran 		__dlm_wait_on_lockres_flags(res, DLM_LOCK_RES_SETREF_INPROG);
1853b8118cfSKurt Hackel 		spin_unlock(&res->spinlock);
186c824c3c7SSunil Mushran 
187ba2bf218SKurt Hackel 		/* clear our bit from the master's refmap, ignore errors */
188ba2bf218SKurt Hackel 		ret = dlm_drop_lockres_ref(dlm, res);
189ba2bf218SKurt Hackel 		if (ret < 0) {
190ba2bf218SKurt Hackel 			if (!dlm_is_host_down(ret))
1916714d8e8SKurt Hackel 				BUG();
1926714d8e8SKurt Hackel 		}
1936714d8e8SKurt Hackel 		spin_lock(&dlm->spinlock);
1947beaf243SSrinivas Eeda 		spin_lock(&res->spinlock);
195ba2bf218SKurt Hackel 	}
1966714d8e8SKurt Hackel 
197ba2bf218SKurt Hackel 	if (!list_empty(&res->purge)) {
1988e17d16fSSunil Mushran 		mlog(0, "%s: Removing res %.*s from purgelist, master %d\n",
1998e17d16fSSunil Mushran 		     dlm->name, res->lockname.len, res->lockname.name, master);
200ba2bf218SKurt Hackel 		list_del_init(&res->purge);
201ba2bf218SKurt Hackel 		dlm_lockres_put(res);
2026714d8e8SKurt Hackel 		dlm->purge_count--;
2037beaf243SSrinivas Eeda 	}
2047beaf243SSrinivas Eeda 
2057beaf243SSrinivas Eeda 	if (!__dlm_lockres_unused(res)) {
2068e17d16fSSunil Mushran 		mlog(ML_ERROR, "%s: res %.*s in use after deref\n",
2077beaf243SSrinivas Eeda 		     dlm->name, res->lockname.len, res->lockname.name);
2087beaf243SSrinivas Eeda 		__dlm_print_one_lock_resource(res);
2097beaf243SSrinivas Eeda 		BUG();
2107beaf243SSrinivas Eeda 	}
21183e32d90SWengang Wang 
212e9f0b6a6SSunil Mushran 	__dlm_unhash_lockres(dlm, res);
213ba2bf218SKurt Hackel 
214ba2bf218SKurt Hackel 	/* lockres is not in the hash now.  drop the flag and wake up
215ba2bf218SKurt Hackel 	 * any processes waiting in dlm_get_lock_resource. */
216ba2bf218SKurt Hackel 	if (!master) {
217ba2bf218SKurt Hackel 		res->state &= ~DLM_LOCK_RES_DROPPING_REF;
218ba2bf218SKurt Hackel 		spin_unlock(&res->spinlock);
219ba2bf218SKurt Hackel 		wake_up(&res->wq);
2207beaf243SSrinivas Eeda 	} else
2217beaf243SSrinivas Eeda 		spin_unlock(&res->spinlock);
2228b219809SKurt Hackel }
2238b219809SKurt Hackel 
2246714d8e8SKurt Hackel static void dlm_run_purge_list(struct dlm_ctxt *dlm,
2256714d8e8SKurt Hackel 			       int purge_now)
2266714d8e8SKurt Hackel {
2276714d8e8SKurt Hackel 	unsigned int run_max, unused;
2286714d8e8SKurt Hackel 	unsigned long purge_jiffies;
2296714d8e8SKurt Hackel 	struct dlm_lock_resource *lockres;
2306714d8e8SKurt Hackel 
2316714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
2326714d8e8SKurt Hackel 	run_max = dlm->purge_count;
2336714d8e8SKurt Hackel 
2346714d8e8SKurt Hackel 	while(run_max && !list_empty(&dlm->purge_list)) {
2356714d8e8SKurt Hackel 		run_max--;
2366714d8e8SKurt Hackel 
2376714d8e8SKurt Hackel 		lockres = list_entry(dlm->purge_list.next,
2386714d8e8SKurt Hackel 				     struct dlm_lock_resource, purge);
2396714d8e8SKurt Hackel 
2406714d8e8SKurt Hackel 		spin_lock(&lockres->spinlock);
2416714d8e8SKurt Hackel 
2426714d8e8SKurt Hackel 		purge_jiffies = lockres->last_used +
2436714d8e8SKurt Hackel 			msecs_to_jiffies(DLM_PURGE_INTERVAL_MS);
2446714d8e8SKurt Hackel 
2456714d8e8SKurt Hackel 		/* Make sure that we want to be processing this guy at
2466714d8e8SKurt Hackel 		 * this time. */
2476714d8e8SKurt Hackel 		if (!purge_now && time_after(purge_jiffies, jiffies)) {
2486714d8e8SKurt Hackel 			/* Since resources are added to the purge list
2496714d8e8SKurt Hackel 			 * in tail order, we can stop at the first
2506714d8e8SKurt Hackel 			 * unpurgable resource -- anyone added after
2516714d8e8SKurt Hackel 			 * him will have a greater last_used value */
2527beaf243SSrinivas Eeda 			spin_unlock(&lockres->spinlock);
2536714d8e8SKurt Hackel 			break;
2546714d8e8SKurt Hackel 		}
2556714d8e8SKurt Hackel 
2567beaf243SSrinivas Eeda 		/* Status of the lockres *might* change so double
2577beaf243SSrinivas Eeda 		 * check. If the lockres is unused, holding the dlm
2587beaf243SSrinivas Eeda 		 * spinlock will prevent people from getting and more
2597beaf243SSrinivas Eeda 		 * refs on it. */
2607beaf243SSrinivas Eeda 		unused = __dlm_lockres_unused(lockres);
2617beaf243SSrinivas Eeda 		if (!unused ||
262ac4fef4dSXue jiufei 		    (lockres->state & DLM_LOCK_RES_MIGRATING) ||
263ac4fef4dSXue jiufei 		    (lockres->inflight_assert_workers != 0)) {
2648e17d16fSSunil Mushran 			mlog(0, "%s: res %.*s is in use or being remastered, "
265ac4fef4dSXue jiufei 			     "used %d, state %d, assert master workers %u\n",
266ac4fef4dSXue jiufei 			     dlm->name, lockres->lockname.len,
267ac4fef4dSXue jiufei 			     lockres->lockname.name,
268ac4fef4dSXue jiufei 			     !unused, lockres->state,
269ac4fef4dSXue jiufei 			     lockres->inflight_assert_workers);
270a270c6d3SXue jiufei 			list_move_tail(&lockres->purge, &dlm->purge_list);
2717beaf243SSrinivas Eeda 			spin_unlock(&lockres->spinlock);
2727beaf243SSrinivas Eeda 			continue;
2737beaf243SSrinivas Eeda 		}
2747beaf243SSrinivas Eeda 
27578062cb2SSunil Mushran 		dlm_lockres_get(lockres);
2766714d8e8SKurt Hackel 
2777beaf243SSrinivas Eeda 		dlm_purge_lockres(dlm, lockres);
27878062cb2SSunil Mushran 
2793fca0894SSunil Mushran 		dlm_lockres_put(lockres);
2806714d8e8SKurt Hackel 
2816714d8e8SKurt Hackel 		/* Avoid adding any scheduling latencies */
2826714d8e8SKurt Hackel 		cond_resched_lock(&dlm->spinlock);
2836714d8e8SKurt Hackel 	}
2846714d8e8SKurt Hackel 
2856714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
2866714d8e8SKurt Hackel }
2876714d8e8SKurt Hackel 
2886714d8e8SKurt Hackel static void dlm_shuffle_lists(struct dlm_ctxt *dlm,
2896714d8e8SKurt Hackel 			      struct dlm_lock_resource *res)
2906714d8e8SKurt Hackel {
2916714d8e8SKurt Hackel 	struct dlm_lock *lock, *target;
2926714d8e8SKurt Hackel 	int can_grant = 1;
2936714d8e8SKurt Hackel 
2948e17d16fSSunil Mushran 	/*
2958e17d16fSSunil Mushran 	 * Because this function is called with the lockres
2966714d8e8SKurt Hackel 	 * spinlock, and because we know that it is not migrating/
2976714d8e8SKurt Hackel 	 * recovering/in-progress, it is fine to reserve asts and
2988e17d16fSSunil Mushran 	 * basts right before queueing them all throughout
2998e17d16fSSunil Mushran 	 */
300d9ef7522SWengang Wang 	assert_spin_locked(&dlm->ast_lock);
3016714d8e8SKurt Hackel 	assert_spin_locked(&res->spinlock);
3026714d8e8SKurt Hackel 	BUG_ON((res->state & (DLM_LOCK_RES_MIGRATING|
3036714d8e8SKurt Hackel 			      DLM_LOCK_RES_RECOVERING|
3046714d8e8SKurt Hackel 			      DLM_LOCK_RES_IN_PROGRESS)));
3056714d8e8SKurt Hackel 
3066714d8e8SKurt Hackel converting:
3076714d8e8SKurt Hackel 	if (list_empty(&res->converting))
3086714d8e8SKurt Hackel 		goto blocked;
3098e17d16fSSunil Mushran 	mlog(0, "%s: res %.*s has locks on the convert queue\n", dlm->name,
3108e17d16fSSunil Mushran 	     res->lockname.len, res->lockname.name);
3116714d8e8SKurt Hackel 
3126714d8e8SKurt Hackel 	target = list_entry(res->converting.next, struct dlm_lock, list);
3136714d8e8SKurt Hackel 	if (target->ml.convert_type == LKM_IVMODE) {
3148e17d16fSSunil Mushran 		mlog(ML_ERROR, "%s: res %.*s converting lock to invalid mode\n",
3158e17d16fSSunil Mushran 		     dlm->name, res->lockname.len, res->lockname.name);
3166714d8e8SKurt Hackel 		BUG();
3176714d8e8SKurt Hackel 	}
318df53cd3bSDong Fang 	list_for_each_entry(lock, &res->granted, list) {
3196714d8e8SKurt Hackel 		if (lock==target)
3206714d8e8SKurt Hackel 			continue;
3216714d8e8SKurt Hackel 		if (!dlm_lock_compatible(lock->ml.type,
3226714d8e8SKurt Hackel 					 target->ml.convert_type)) {
3236714d8e8SKurt Hackel 			can_grant = 0;
3246714d8e8SKurt Hackel 			/* queue the BAST if not already */
3256714d8e8SKurt Hackel 			if (lock->ml.highest_blocked == LKM_IVMODE) {
3266714d8e8SKurt Hackel 				__dlm_lockres_reserve_ast(res);
327d9ef7522SWengang Wang 				__dlm_queue_bast(dlm, lock);
3286714d8e8SKurt Hackel 			}
3296714d8e8SKurt Hackel 			/* update the highest_blocked if needed */
3306714d8e8SKurt Hackel 			if (lock->ml.highest_blocked < target->ml.convert_type)
3316714d8e8SKurt Hackel 				lock->ml.highest_blocked =
3326714d8e8SKurt Hackel 					target->ml.convert_type;
3336714d8e8SKurt Hackel 		}
3346714d8e8SKurt Hackel 	}
335df53cd3bSDong Fang 
336df53cd3bSDong Fang 	list_for_each_entry(lock, &res->converting, list) {
3376714d8e8SKurt Hackel 		if (lock==target)
3386714d8e8SKurt Hackel 			continue;
3396714d8e8SKurt Hackel 		if (!dlm_lock_compatible(lock->ml.type,
3406714d8e8SKurt Hackel 					 target->ml.convert_type)) {
3416714d8e8SKurt Hackel 			can_grant = 0;
3426714d8e8SKurt Hackel 			if (lock->ml.highest_blocked == LKM_IVMODE) {
3436714d8e8SKurt Hackel 				__dlm_lockres_reserve_ast(res);
344d9ef7522SWengang Wang 				__dlm_queue_bast(dlm, lock);
3456714d8e8SKurt Hackel 			}
3466714d8e8SKurt Hackel 			if (lock->ml.highest_blocked < target->ml.convert_type)
3476714d8e8SKurt Hackel 				lock->ml.highest_blocked =
3486714d8e8SKurt Hackel 					target->ml.convert_type;
3496714d8e8SKurt Hackel 		}
3506714d8e8SKurt Hackel 	}
3516714d8e8SKurt Hackel 
3526714d8e8SKurt Hackel 	/* we can convert the lock */
3536714d8e8SKurt Hackel 	if (can_grant) {
3546714d8e8SKurt Hackel 		spin_lock(&target->spinlock);
3556714d8e8SKurt Hackel 		BUG_ON(target->ml.highest_blocked != LKM_IVMODE);
3566714d8e8SKurt Hackel 
3578e17d16fSSunil Mushran 		mlog(0, "%s: res %.*s, AST for Converting lock %u:%llu, type "
3588e17d16fSSunil Mushran 		     "%d => %d, node %u\n", dlm->name, res->lockname.len,
3598e17d16fSSunil Mushran 		     res->lockname.name,
3608e17d16fSSunil Mushran 		     dlm_get_lock_cookie_node(be64_to_cpu(target->ml.cookie)),
3618e17d16fSSunil Mushran 		     dlm_get_lock_cookie_seq(be64_to_cpu(target->ml.cookie)),
3628e17d16fSSunil Mushran 		     target->ml.type,
3636714d8e8SKurt Hackel 		     target->ml.convert_type, target->ml.node);
3646714d8e8SKurt Hackel 
3656714d8e8SKurt Hackel 		target->ml.type = target->ml.convert_type;
3666714d8e8SKurt Hackel 		target->ml.convert_type = LKM_IVMODE;
367f116629dSAkinobu Mita 		list_move_tail(&target->list, &res->granted);
3686714d8e8SKurt Hackel 
3696714d8e8SKurt Hackel 		BUG_ON(!target->lksb);
3706714d8e8SKurt Hackel 		target->lksb->status = DLM_NORMAL;
3716714d8e8SKurt Hackel 
3726714d8e8SKurt Hackel 		spin_unlock(&target->spinlock);
3736714d8e8SKurt Hackel 
3746714d8e8SKurt Hackel 		__dlm_lockres_reserve_ast(res);
375d9ef7522SWengang Wang 		__dlm_queue_ast(dlm, target);
3766714d8e8SKurt Hackel 		/* go back and check for more */
3776714d8e8SKurt Hackel 		goto converting;
3786714d8e8SKurt Hackel 	}
3796714d8e8SKurt Hackel 
3806714d8e8SKurt Hackel blocked:
3816714d8e8SKurt Hackel 	if (list_empty(&res->blocked))
3826714d8e8SKurt Hackel 		goto leave;
3836714d8e8SKurt Hackel 	target = list_entry(res->blocked.next, struct dlm_lock, list);
3846714d8e8SKurt Hackel 
385df53cd3bSDong Fang 	list_for_each_entry(lock, &res->granted, list) {
3866714d8e8SKurt Hackel 		if (lock==target)
3876714d8e8SKurt Hackel 			continue;
3886714d8e8SKurt Hackel 		if (!dlm_lock_compatible(lock->ml.type, target->ml.type)) {
3896714d8e8SKurt Hackel 			can_grant = 0;
3906714d8e8SKurt Hackel 			if (lock->ml.highest_blocked == LKM_IVMODE) {
3916714d8e8SKurt Hackel 				__dlm_lockres_reserve_ast(res);
392d9ef7522SWengang Wang 				__dlm_queue_bast(dlm, lock);
3936714d8e8SKurt Hackel 			}
3946714d8e8SKurt Hackel 			if (lock->ml.highest_blocked < target->ml.type)
3956714d8e8SKurt Hackel 				lock->ml.highest_blocked = target->ml.type;
3966714d8e8SKurt Hackel 		}
3976714d8e8SKurt Hackel 	}
3986714d8e8SKurt Hackel 
399df53cd3bSDong Fang 	list_for_each_entry(lock, &res->converting, list) {
4006714d8e8SKurt Hackel 		if (lock==target)
4016714d8e8SKurt Hackel 			continue;
4026714d8e8SKurt Hackel 		if (!dlm_lock_compatible(lock->ml.type, target->ml.type)) {
4036714d8e8SKurt Hackel 			can_grant = 0;
4046714d8e8SKurt Hackel 			if (lock->ml.highest_blocked == LKM_IVMODE) {
4056714d8e8SKurt Hackel 				__dlm_lockres_reserve_ast(res);
406d9ef7522SWengang Wang 				__dlm_queue_bast(dlm, lock);
4076714d8e8SKurt Hackel 			}
4086714d8e8SKurt Hackel 			if (lock->ml.highest_blocked < target->ml.type)
4096714d8e8SKurt Hackel 				lock->ml.highest_blocked = target->ml.type;
4106714d8e8SKurt Hackel 		}
4116714d8e8SKurt Hackel 	}
4126714d8e8SKurt Hackel 
4136714d8e8SKurt Hackel 	/* we can grant the blocked lock (only
4146714d8e8SKurt Hackel 	 * possible if converting list empty) */
4156714d8e8SKurt Hackel 	if (can_grant) {
4166714d8e8SKurt Hackel 		spin_lock(&target->spinlock);
4176714d8e8SKurt Hackel 		BUG_ON(target->ml.highest_blocked != LKM_IVMODE);
4186714d8e8SKurt Hackel 
4198e17d16fSSunil Mushran 		mlog(0, "%s: res %.*s, AST for Blocked lock %u:%llu, type %d, "
4208e17d16fSSunil Mushran 		     "node %u\n", dlm->name, res->lockname.len,
4218e17d16fSSunil Mushran 		     res->lockname.name,
4228e17d16fSSunil Mushran 		     dlm_get_lock_cookie_node(be64_to_cpu(target->ml.cookie)),
4238e17d16fSSunil Mushran 		     dlm_get_lock_cookie_seq(be64_to_cpu(target->ml.cookie)),
4246714d8e8SKurt Hackel 		     target->ml.type, target->ml.node);
4256714d8e8SKurt Hackel 
4268e17d16fSSunil Mushran 		/* target->ml.type is already correct */
427f116629dSAkinobu Mita 		list_move_tail(&target->list, &res->granted);
4286714d8e8SKurt Hackel 
4296714d8e8SKurt Hackel 		BUG_ON(!target->lksb);
4306714d8e8SKurt Hackel 		target->lksb->status = DLM_NORMAL;
4316714d8e8SKurt Hackel 
4326714d8e8SKurt Hackel 		spin_unlock(&target->spinlock);
4336714d8e8SKurt Hackel 
4346714d8e8SKurt Hackel 		__dlm_lockres_reserve_ast(res);
435d9ef7522SWengang Wang 		__dlm_queue_ast(dlm, target);
4366714d8e8SKurt Hackel 		/* go back and check for more */
4376714d8e8SKurt Hackel 		goto converting;
4386714d8e8SKurt Hackel 	}
4396714d8e8SKurt Hackel 
4406714d8e8SKurt Hackel leave:
4416714d8e8SKurt Hackel 	return;
4426714d8e8SKurt Hackel }
4436714d8e8SKurt Hackel 
4446714d8e8SKurt Hackel /* must have NO locks when calling this with res !=NULL * */
4456714d8e8SKurt Hackel void dlm_kick_thread(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
4466714d8e8SKurt Hackel {
4476714d8e8SKurt Hackel 	if (res) {
4486714d8e8SKurt Hackel 		spin_lock(&dlm->spinlock);
4496714d8e8SKurt Hackel 		spin_lock(&res->spinlock);
4506714d8e8SKurt Hackel 		__dlm_dirty_lockres(dlm, res);
4516714d8e8SKurt Hackel 		spin_unlock(&res->spinlock);
4526714d8e8SKurt Hackel 		spin_unlock(&dlm->spinlock);
4536714d8e8SKurt Hackel 	}
4546714d8e8SKurt Hackel 	wake_up(&dlm->dlm_thread_wq);
4556714d8e8SKurt Hackel }
4566714d8e8SKurt Hackel 
4576714d8e8SKurt Hackel void __dlm_dirty_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
4586714d8e8SKurt Hackel {
4596714d8e8SKurt Hackel 	assert_spin_locked(&dlm->spinlock);
4606714d8e8SKurt Hackel 	assert_spin_locked(&res->spinlock);
4616714d8e8SKurt Hackel 
4626714d8e8SKurt Hackel 	/* don't shuffle secondary queues */
463ddc09c8dSKurt Hackel 	if ((res->owner == dlm->node_num)) {
464ddc09c8dSKurt Hackel 		if (res->state & (DLM_LOCK_RES_MIGRATING |
465ddc09c8dSKurt Hackel 				  DLM_LOCK_RES_BLOCK_DIRTY))
466ddc09c8dSKurt Hackel 		    return;
467ddc09c8dSKurt Hackel 
468ddc09c8dSKurt Hackel 		if (list_empty(&res->dirty)) {
4696ff06a93SKurt Hackel 			/* ref for dirty_list */
4706ff06a93SKurt Hackel 			dlm_lockres_get(res);
4716714d8e8SKurt Hackel 			list_add_tail(&res->dirty, &dlm->dirty_list);
4726714d8e8SKurt Hackel 			res->state |= DLM_LOCK_RES_DIRTY;
4736714d8e8SKurt Hackel 		}
4746714d8e8SKurt Hackel 	}
4758e17d16fSSunil Mushran 
4768e17d16fSSunil Mushran 	mlog(0, "%s: res %.*s\n", dlm->name, res->lockname.len,
4778e17d16fSSunil Mushran 	     res->lockname.name);
478ddc09c8dSKurt Hackel }
4796714d8e8SKurt Hackel 
4806714d8e8SKurt Hackel 
4816714d8e8SKurt Hackel /* Launch the NM thread for the mounted volume */
4826714d8e8SKurt Hackel int dlm_launch_thread(struct dlm_ctxt *dlm)
4836714d8e8SKurt Hackel {
4848e17d16fSSunil Mushran 	mlog(0, "Starting dlm_thread...\n");
4856714d8e8SKurt Hackel 
4866714d8e8SKurt Hackel 	dlm->dlm_thread_task = kthread_run(dlm_thread, dlm, "dlm_thread");
4876714d8e8SKurt Hackel 	if (IS_ERR(dlm->dlm_thread_task)) {
4886714d8e8SKurt Hackel 		mlog_errno(PTR_ERR(dlm->dlm_thread_task));
4896714d8e8SKurt Hackel 		dlm->dlm_thread_task = NULL;
4906714d8e8SKurt Hackel 		return -EINVAL;
4916714d8e8SKurt Hackel 	}
4926714d8e8SKurt Hackel 
4936714d8e8SKurt Hackel 	return 0;
4946714d8e8SKurt Hackel }
4956714d8e8SKurt Hackel 
4966714d8e8SKurt Hackel void dlm_complete_thread(struct dlm_ctxt *dlm)
4976714d8e8SKurt Hackel {
4986714d8e8SKurt Hackel 	if (dlm->dlm_thread_task) {
4998e17d16fSSunil Mushran 		mlog(ML_KTHREAD, "Waiting for dlm thread to exit\n");
5006714d8e8SKurt Hackel 		kthread_stop(dlm->dlm_thread_task);
5016714d8e8SKurt Hackel 		dlm->dlm_thread_task = NULL;
5026714d8e8SKurt Hackel 	}
5036714d8e8SKurt Hackel }
5046714d8e8SKurt Hackel 
5056714d8e8SKurt Hackel static int dlm_dirty_list_empty(struct dlm_ctxt *dlm)
5066714d8e8SKurt Hackel {
5076714d8e8SKurt Hackel 	int empty;
5086714d8e8SKurt Hackel 
5096714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
5106714d8e8SKurt Hackel 	empty = list_empty(&dlm->dirty_list);
5116714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
5126714d8e8SKurt Hackel 
5136714d8e8SKurt Hackel 	return empty;
5146714d8e8SKurt Hackel }
5156714d8e8SKurt Hackel 
5166714d8e8SKurt Hackel static void dlm_flush_asts(struct dlm_ctxt *dlm)
5176714d8e8SKurt Hackel {
5186714d8e8SKurt Hackel 	int ret;
5196714d8e8SKurt Hackel 	struct dlm_lock *lock;
5206714d8e8SKurt Hackel 	struct dlm_lock_resource *res;
5216714d8e8SKurt Hackel 	u8 hi;
5226714d8e8SKurt Hackel 
5236714d8e8SKurt Hackel 	spin_lock(&dlm->ast_lock);
5246714d8e8SKurt Hackel 	while (!list_empty(&dlm->pending_asts)) {
5256714d8e8SKurt Hackel 		lock = list_entry(dlm->pending_asts.next,
5266714d8e8SKurt Hackel 				  struct dlm_lock, ast_list);
5276714d8e8SKurt Hackel 		/* get an extra ref on lock */
5286714d8e8SKurt Hackel 		dlm_lock_get(lock);
5296714d8e8SKurt Hackel 		res = lock->lockres;
5308e17d16fSSunil Mushran 		mlog(0, "%s: res %.*s, Flush AST for lock %u:%llu, type %d, "
5318e17d16fSSunil Mushran 		     "node %u\n", dlm->name, res->lockname.len,
5328e17d16fSSunil Mushran 		     res->lockname.name,
5338e17d16fSSunil Mushran 		     dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
5348e17d16fSSunil Mushran 		     dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)),
5358e17d16fSSunil Mushran 		     lock->ml.type, lock->ml.node);
5366714d8e8SKurt Hackel 
5376714d8e8SKurt Hackel 		BUG_ON(!lock->ast_pending);
5386714d8e8SKurt Hackel 
5396714d8e8SKurt Hackel 		/* remove from list (including ref) */
5406714d8e8SKurt Hackel 		list_del_init(&lock->ast_list);
5416714d8e8SKurt Hackel 		dlm_lock_put(lock);
5426714d8e8SKurt Hackel 		spin_unlock(&dlm->ast_lock);
5436714d8e8SKurt Hackel 
5446714d8e8SKurt Hackel 		if (lock->ml.node != dlm->node_num) {
5456714d8e8SKurt Hackel 			ret = dlm_do_remote_ast(dlm, res, lock);
5466714d8e8SKurt Hackel 			if (ret < 0)
5476714d8e8SKurt Hackel 				mlog_errno(ret);
5486714d8e8SKurt Hackel 		} else
5496714d8e8SKurt Hackel 			dlm_do_local_ast(dlm, res, lock);
5506714d8e8SKurt Hackel 
5516714d8e8SKurt Hackel 		spin_lock(&dlm->ast_lock);
5526714d8e8SKurt Hackel 
5536714d8e8SKurt Hackel 		/* possible that another ast was queued while
5546714d8e8SKurt Hackel 		 * we were delivering the last one */
5556714d8e8SKurt Hackel 		if (!list_empty(&lock->ast_list)) {
5568e17d16fSSunil Mushran 			mlog(0, "%s: res %.*s, AST queued while flushing last "
5578e17d16fSSunil Mushran 			     "one\n", dlm->name, res->lockname.len,
5588e17d16fSSunil Mushran 			     res->lockname.name);
5596714d8e8SKurt Hackel 		} else
5606714d8e8SKurt Hackel 			lock->ast_pending = 0;
5616714d8e8SKurt Hackel 
5626714d8e8SKurt Hackel 		/* drop the extra ref.
5636714d8e8SKurt Hackel 		 * this may drop it completely. */
5646714d8e8SKurt Hackel 		dlm_lock_put(lock);
5656714d8e8SKurt Hackel 		dlm_lockres_release_ast(dlm, res);
5666714d8e8SKurt Hackel 	}
5676714d8e8SKurt Hackel 
5686714d8e8SKurt Hackel 	while (!list_empty(&dlm->pending_basts)) {
5696714d8e8SKurt Hackel 		lock = list_entry(dlm->pending_basts.next,
5706714d8e8SKurt Hackel 				  struct dlm_lock, bast_list);
5716714d8e8SKurt Hackel 		/* get an extra ref on lock */
5726714d8e8SKurt Hackel 		dlm_lock_get(lock);
5736714d8e8SKurt Hackel 		res = lock->lockres;
5746714d8e8SKurt Hackel 
5756714d8e8SKurt Hackel 		BUG_ON(!lock->bast_pending);
5766714d8e8SKurt Hackel 
5776714d8e8SKurt Hackel 		/* get the highest blocked lock, and reset */
5786714d8e8SKurt Hackel 		spin_lock(&lock->spinlock);
5796714d8e8SKurt Hackel 		BUG_ON(lock->ml.highest_blocked <= LKM_IVMODE);
5806714d8e8SKurt Hackel 		hi = lock->ml.highest_blocked;
5816714d8e8SKurt Hackel 		lock->ml.highest_blocked = LKM_IVMODE;
5826714d8e8SKurt Hackel 		spin_unlock(&lock->spinlock);
5836714d8e8SKurt Hackel 
5846714d8e8SKurt Hackel 		/* remove from list (including ref) */
5856714d8e8SKurt Hackel 		list_del_init(&lock->bast_list);
5866714d8e8SKurt Hackel 		dlm_lock_put(lock);
5876714d8e8SKurt Hackel 		spin_unlock(&dlm->ast_lock);
5886714d8e8SKurt Hackel 
5898e17d16fSSunil Mushran 		mlog(0, "%s: res %.*s, Flush BAST for lock %u:%llu, "
5908e17d16fSSunil Mushran 		     "blocked %d, node %u\n",
5918e17d16fSSunil Mushran 		     dlm->name, res->lockname.len, res->lockname.name,
5928e17d16fSSunil Mushran 		     dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
5938e17d16fSSunil Mushran 		     dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)),
5948e17d16fSSunil Mushran 		     hi, lock->ml.node);
5956714d8e8SKurt Hackel 
5966714d8e8SKurt Hackel 		if (lock->ml.node != dlm->node_num) {
5976714d8e8SKurt Hackel 			ret = dlm_send_proxy_bast(dlm, res, lock, hi);
5986714d8e8SKurt Hackel 			if (ret < 0)
5996714d8e8SKurt Hackel 				mlog_errno(ret);
6006714d8e8SKurt Hackel 		} else
6016714d8e8SKurt Hackel 			dlm_do_local_bast(dlm, res, lock, hi);
6026714d8e8SKurt Hackel 
6036714d8e8SKurt Hackel 		spin_lock(&dlm->ast_lock);
6046714d8e8SKurt Hackel 
6056714d8e8SKurt Hackel 		/* possible that another bast was queued while
6066714d8e8SKurt Hackel 		 * we were delivering the last one */
6076714d8e8SKurt Hackel 		if (!list_empty(&lock->bast_list)) {
6088e17d16fSSunil Mushran 			mlog(0, "%s: res %.*s, BAST queued while flushing last "
6098e17d16fSSunil Mushran 			     "one\n", dlm->name, res->lockname.len,
6108e17d16fSSunil Mushran 			     res->lockname.name);
6116714d8e8SKurt Hackel 		} else
6126714d8e8SKurt Hackel 			lock->bast_pending = 0;
6136714d8e8SKurt Hackel 
6146714d8e8SKurt Hackel 		/* drop the extra ref.
6156714d8e8SKurt Hackel 		 * this may drop it completely. */
6166714d8e8SKurt Hackel 		dlm_lock_put(lock);
6176714d8e8SKurt Hackel 		dlm_lockres_release_ast(dlm, res);
6186714d8e8SKurt Hackel 	}
6196714d8e8SKurt Hackel 	wake_up(&dlm->ast_wq);
6206714d8e8SKurt Hackel 	spin_unlock(&dlm->ast_lock);
6216714d8e8SKurt Hackel }
6226714d8e8SKurt Hackel 
6236714d8e8SKurt Hackel 
6246714d8e8SKurt Hackel #define DLM_THREAD_TIMEOUT_MS (4 * 1000)
6256714d8e8SKurt Hackel #define DLM_THREAD_MAX_DIRTY  100
6266714d8e8SKurt Hackel #define DLM_THREAD_MAX_ASTS   10
6276714d8e8SKurt Hackel 
6286714d8e8SKurt Hackel static int dlm_thread(void *data)
6296714d8e8SKurt Hackel {
6306714d8e8SKurt Hackel 	struct dlm_lock_resource *res;
6316714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
6326714d8e8SKurt Hackel 	unsigned long timeout = msecs_to_jiffies(DLM_THREAD_TIMEOUT_MS);
6336714d8e8SKurt Hackel 
6346714d8e8SKurt Hackel 	mlog(0, "dlm thread running for %s...\n", dlm->name);
6356714d8e8SKurt Hackel 
6366714d8e8SKurt Hackel 	while (!kthread_should_stop()) {
6376714d8e8SKurt Hackel 		int n = DLM_THREAD_MAX_DIRTY;
6386714d8e8SKurt Hackel 
6396714d8e8SKurt Hackel 		/* dlm_shutting_down is very point-in-time, but that
6406714d8e8SKurt Hackel 		 * doesn't matter as we'll just loop back around if we
6416714d8e8SKurt Hackel 		 * get false on the leading edge of a state
6426714d8e8SKurt Hackel 		 * transition. */
6436714d8e8SKurt Hackel 		dlm_run_purge_list(dlm, dlm_shutting_down(dlm));
6446714d8e8SKurt Hackel 
6456714d8e8SKurt Hackel 		/* We really don't want to hold dlm->spinlock while
6466714d8e8SKurt Hackel 		 * calling dlm_shuffle_lists on each lockres that
6476714d8e8SKurt Hackel 		 * needs to have its queues adjusted and AST/BASTs
6486714d8e8SKurt Hackel 		 * run.  So let's pull each entry off the dirty_list
6496714d8e8SKurt Hackel 		 * and drop dlm->spinlock ASAP.  Once off the list,
6506714d8e8SKurt Hackel 		 * res->spinlock needs to be taken again to protect
6516714d8e8SKurt Hackel 		 * the queues while calling dlm_shuffle_lists.  */
6526714d8e8SKurt Hackel 		spin_lock(&dlm->spinlock);
6536714d8e8SKurt Hackel 		while (!list_empty(&dlm->dirty_list)) {
6546714d8e8SKurt Hackel 			int delay = 0;
6556714d8e8SKurt Hackel 			res = list_entry(dlm->dirty_list.next,
6566714d8e8SKurt Hackel 					 struct dlm_lock_resource, dirty);
6576714d8e8SKurt Hackel 
6586714d8e8SKurt Hackel 			/* peel a lockres off, remove it from the list,
6596714d8e8SKurt Hackel 			 * unset the dirty flag and drop the dlm lock */
6606714d8e8SKurt Hackel 			BUG_ON(!res);
6616714d8e8SKurt Hackel 			dlm_lockres_get(res);
6626714d8e8SKurt Hackel 
6636714d8e8SKurt Hackel 			spin_lock(&res->spinlock);
664ddc09c8dSKurt Hackel 			/* We clear the DLM_LOCK_RES_DIRTY state once we shuffle lists below */
6656714d8e8SKurt Hackel 			list_del_init(&res->dirty);
6666714d8e8SKurt Hackel 			spin_unlock(&res->spinlock);
6676714d8e8SKurt Hackel 			spin_unlock(&dlm->spinlock);
6686ff06a93SKurt Hackel 			/* Drop dirty_list ref */
6696ff06a93SKurt Hackel 			dlm_lockres_put(res);
6706714d8e8SKurt Hackel 
6716714d8e8SKurt Hackel 		 	/* lockres can be re-dirtied/re-added to the
6726714d8e8SKurt Hackel 			 * dirty_list in this gap, but that is ok */
6736714d8e8SKurt Hackel 
674d9ef7522SWengang Wang 			spin_lock(&dlm->ast_lock);
6756714d8e8SKurt Hackel 			spin_lock(&res->spinlock);
6766714d8e8SKurt Hackel 			if (res->owner != dlm->node_num) {
6776714d8e8SKurt Hackel 				__dlm_print_one_lock_resource(res);
6788e17d16fSSunil Mushran 				mlog(ML_ERROR, "%s: inprog %d, mig %d, reco %d,"
6798e17d16fSSunil Mushran 				     " dirty %d\n", dlm->name,
6808e17d16fSSunil Mushran 				     !!(res->state & DLM_LOCK_RES_IN_PROGRESS),
6818e17d16fSSunil Mushran 				     !!(res->state & DLM_LOCK_RES_MIGRATING),
6828e17d16fSSunil Mushran 				     !!(res->state & DLM_LOCK_RES_RECOVERING),
6838e17d16fSSunil Mushran 				     !!(res->state & DLM_LOCK_RES_DIRTY));
6846714d8e8SKurt Hackel 			}
6856714d8e8SKurt Hackel 			BUG_ON(res->owner != dlm->node_num);
6866714d8e8SKurt Hackel 
6876714d8e8SKurt Hackel 			/* it is now ok to move lockreses in these states
6886714d8e8SKurt Hackel 			 * to the dirty list, assuming that they will only be
6896714d8e8SKurt Hackel 			 * dirty for a short while. */
690ddc09c8dSKurt Hackel 			BUG_ON(res->state & DLM_LOCK_RES_MIGRATING);
6916714d8e8SKurt Hackel 			if (res->state & (DLM_LOCK_RES_IN_PROGRESS |
6926714d8e8SKurt Hackel 					  DLM_LOCK_RES_RECOVERING)) {
6936714d8e8SKurt Hackel 				/* move it to the tail and keep going */
694ddc09c8dSKurt Hackel 				res->state &= ~DLM_LOCK_RES_DIRTY;
6956714d8e8SKurt Hackel 				spin_unlock(&res->spinlock);
696d9ef7522SWengang Wang 				spin_unlock(&dlm->ast_lock);
6978e17d16fSSunil Mushran 				mlog(0, "%s: res %.*s, inprogress, delay list "
6988e17d16fSSunil Mushran 				     "shuffle, state %d\n", dlm->name,
6996714d8e8SKurt Hackel 				     res->lockname.len, res->lockname.name,
7006714d8e8SKurt Hackel 				     res->state);
7016714d8e8SKurt Hackel 				delay = 1;
7026714d8e8SKurt Hackel 				goto in_progress;
7036714d8e8SKurt Hackel 			}
7046714d8e8SKurt Hackel 
7056714d8e8SKurt Hackel 			/* at this point the lockres is not migrating/
7066714d8e8SKurt Hackel 			 * recovering/in-progress.  we have the lockres
7076714d8e8SKurt Hackel 			 * spinlock and do NOT have the dlm lock.
7086714d8e8SKurt Hackel 			 * safe to reserve/queue asts and run the lists. */
7096714d8e8SKurt Hackel 
7106714d8e8SKurt Hackel 			/* called while holding lockres lock */
7116714d8e8SKurt Hackel 			dlm_shuffle_lists(dlm, res);
712ddc09c8dSKurt Hackel 			res->state &= ~DLM_LOCK_RES_DIRTY;
7136714d8e8SKurt Hackel 			spin_unlock(&res->spinlock);
714d9ef7522SWengang Wang 			spin_unlock(&dlm->ast_lock);
7156714d8e8SKurt Hackel 
7166714d8e8SKurt Hackel 			dlm_lockres_calc_usage(dlm, res);
7176714d8e8SKurt Hackel 
7186714d8e8SKurt Hackel in_progress:
7196714d8e8SKurt Hackel 
7206714d8e8SKurt Hackel 			spin_lock(&dlm->spinlock);
7216714d8e8SKurt Hackel 			/* if the lock was in-progress, stick
7226714d8e8SKurt Hackel 			 * it on the back of the list */
7236714d8e8SKurt Hackel 			if (delay) {
7246714d8e8SKurt Hackel 				spin_lock(&res->spinlock);
725ddc09c8dSKurt Hackel 				__dlm_dirty_lockres(dlm, res);
7266714d8e8SKurt Hackel 				spin_unlock(&res->spinlock);
7276714d8e8SKurt Hackel 			}
7286714d8e8SKurt Hackel 			dlm_lockres_put(res);
7296714d8e8SKurt Hackel 
7306714d8e8SKurt Hackel 			/* unlikely, but we may need to give time to
7316714d8e8SKurt Hackel 			 * other tasks */
7326714d8e8SKurt Hackel 			if (!--n) {
7338e17d16fSSunil Mushran 				mlog(0, "%s: Throttling dlm thread\n",
7348e17d16fSSunil Mushran 				     dlm->name);
7356714d8e8SKurt Hackel 				break;
7366714d8e8SKurt Hackel 			}
7376714d8e8SKurt Hackel 		}
7386714d8e8SKurt Hackel 
7396714d8e8SKurt Hackel 		spin_unlock(&dlm->spinlock);
7406714d8e8SKurt Hackel 		dlm_flush_asts(dlm);
7416714d8e8SKurt Hackel 
7426714d8e8SKurt Hackel 		/* yield and continue right away if there is more work to do */
7436714d8e8SKurt Hackel 		if (!n) {
744f85cd47aSKurt Hackel 			cond_resched();
7456714d8e8SKurt Hackel 			continue;
7466714d8e8SKurt Hackel 		}
7476714d8e8SKurt Hackel 
7486714d8e8SKurt Hackel 		wait_event_interruptible_timeout(dlm->dlm_thread_wq,
7496714d8e8SKurt Hackel 						 !dlm_dirty_list_empty(dlm) ||
7506714d8e8SKurt Hackel 						 kthread_should_stop(),
7516714d8e8SKurt Hackel 						 timeout);
7526714d8e8SKurt Hackel 	}
7536714d8e8SKurt Hackel 
7546714d8e8SKurt Hackel 	mlog(0, "quitting DLM thread\n");
7556714d8e8SKurt Hackel 	return 0;
7566714d8e8SKurt Hackel }
757