xref: /linux/fs/ocfs2/dlm/dlmthread.c (revision 5d4a2e29fba5b2bef95b96a46b338ec4d76fa4fd)
1 /* -*- mode: c; c-basic-offset: 8; -*-
2  * vim: noexpandtab sw=8 ts=8 sts=0:
3  *
4  * dlmthread.c
5  *
6  * standalone DLM module
7  *
8  * Copyright (C) 2004 Oracle.  All rights reserved.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public
21  * License along with this program; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23  * Boston, MA 021110-1307, USA.
24  *
25  */
26 
27 
28 #include <linux/module.h>
29 #include <linux/fs.h>
30 #include <linux/types.h>
31 #include <linux/highmem.h>
32 #include <linux/init.h>
33 #include <linux/sysctl.h>
34 #include <linux/random.h>
35 #include <linux/blkdev.h>
36 #include <linux/socket.h>
37 #include <linux/inet.h>
38 #include <linux/timer.h>
39 #include <linux/kthread.h>
40 #include <linux/delay.h>
41 
42 
43 #include "cluster/heartbeat.h"
44 #include "cluster/nodemanager.h"
45 #include "cluster/tcp.h"
46 
47 #include "dlmapi.h"
48 #include "dlmcommon.h"
49 #include "dlmdomain.h"
50 
51 #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_THREAD)
52 #include "cluster/masklog.h"
53 
54 static int dlm_thread(void *data);
55 static void dlm_flush_asts(struct dlm_ctxt *dlm);
56 
57 #define dlm_lock_is_remote(dlm, lock)     ((lock)->ml.node != (dlm)->node_num)
58 
59 /* will exit holding res->spinlock, but may drop in function */
60 /* waits until flags are cleared on res->state */
61 void __dlm_wait_on_lockres_flags(struct dlm_lock_resource *res, int flags)
62 {
63 	DECLARE_WAITQUEUE(wait, current);
64 
65 	assert_spin_locked(&res->spinlock);
66 
67 	add_wait_queue(&res->wq, &wait);
68 repeat:
69 	set_current_state(TASK_UNINTERRUPTIBLE);
70 	if (res->state & flags) {
71 		spin_unlock(&res->spinlock);
72 		schedule();
73 		spin_lock(&res->spinlock);
74 		goto repeat;
75 	}
76 	remove_wait_queue(&res->wq, &wait);
77 	__set_current_state(TASK_RUNNING);
78 }
79 
80 int __dlm_lockres_has_locks(struct dlm_lock_resource *res)
81 {
82 	if (list_empty(&res->granted) &&
83 	    list_empty(&res->converting) &&
84 	    list_empty(&res->blocked))
85 		return 0;
86 	return 1;
87 }
88 
89 /* "unused": the lockres has no locks, is not on the dirty list,
90  * has no inflight locks (in the gap between mastery and acquiring
91  * the first lock), and has no bits in its refmap.
92  * truly ready to be freed. */
93 int __dlm_lockres_unused(struct dlm_lock_resource *res)
94 {
95 	if (!__dlm_lockres_has_locks(res) &&
96 	    (list_empty(&res->dirty) && !(res->state & DLM_LOCK_RES_DIRTY))) {
97 		/* try not to scan the bitmap unless the first two
98 		 * conditions are already true */
99 		int bit = find_next_bit(res->refmap, O2NM_MAX_NODES, 0);
100 		if (bit >= O2NM_MAX_NODES) {
101 			/* since the bit for dlm->node_num is not
102 			 * set, inflight_locks better be zero */
103 			BUG_ON(res->inflight_locks != 0);
104 			return 1;
105 		}
106 	}
107 	return 0;
108 }
109 
110 
111 /* Call whenever you may have added or deleted something from one of
112  * the lockres queue's. This will figure out whether it belongs on the
113  * unused list or not and does the appropriate thing. */
114 void __dlm_lockres_calc_usage(struct dlm_ctxt *dlm,
115 			      struct dlm_lock_resource *res)
116 {
117 	mlog_entry("%.*s\n", res->lockname.len, res->lockname.name);
118 
119 	assert_spin_locked(&dlm->spinlock);
120 	assert_spin_locked(&res->spinlock);
121 
122 	if (__dlm_lockres_unused(res)){
123 		if (list_empty(&res->purge)) {
124 			mlog(0, "putting lockres %.*s:%p onto purge list\n",
125 			     res->lockname.len, res->lockname.name, res);
126 
127 			res->last_used = jiffies;
128 			dlm_lockres_get(res);
129 			list_add_tail(&res->purge, &dlm->purge_list);
130 			dlm->purge_count++;
131 		}
132 	} else if (!list_empty(&res->purge)) {
133 		mlog(0, "removing lockres %.*s:%p from purge list, owner=%u\n",
134 		     res->lockname.len, res->lockname.name, res, res->owner);
135 
136 		list_del_init(&res->purge);
137 		dlm_lockres_put(res);
138 		dlm->purge_count--;
139 	}
140 }
141 
142 void dlm_lockres_calc_usage(struct dlm_ctxt *dlm,
143 			    struct dlm_lock_resource *res)
144 {
145 	mlog_entry("%.*s\n", res->lockname.len, res->lockname.name);
146 	spin_lock(&dlm->spinlock);
147 	spin_lock(&res->spinlock);
148 
149 	__dlm_lockres_calc_usage(dlm, res);
150 
151 	spin_unlock(&res->spinlock);
152 	spin_unlock(&dlm->spinlock);
153 }
154 
155 static int dlm_purge_lockres(struct dlm_ctxt *dlm,
156 			     struct dlm_lock_resource *res)
157 {
158 	int master;
159 	int ret = 0;
160 
161 	spin_lock(&res->spinlock);
162 	if (!__dlm_lockres_unused(res)) {
163 		mlog(0, "%s:%.*s: tried to purge but not unused\n",
164 		     dlm->name, res->lockname.len, res->lockname.name);
165 		__dlm_print_one_lock_resource(res);
166 		spin_unlock(&res->spinlock);
167 		BUG();
168 	}
169 
170 	if (res->state & DLM_LOCK_RES_MIGRATING) {
171 		mlog(0, "%s:%.*s: Delay dropref as this lockres is "
172 		     "being remastered\n", dlm->name, res->lockname.len,
173 		     res->lockname.name);
174 		/* Re-add the lockres to the end of the purge list */
175 		if (!list_empty(&res->purge)) {
176 			list_del_init(&res->purge);
177 			list_add_tail(&res->purge, &dlm->purge_list);
178 		}
179 		spin_unlock(&res->spinlock);
180 		return 0;
181 	}
182 
183 	master = (res->owner == dlm->node_num);
184 
185 	if (!master)
186 		res->state |= DLM_LOCK_RES_DROPPING_REF;
187 	spin_unlock(&res->spinlock);
188 
189 	mlog(0, "purging lockres %.*s, master = %d\n", res->lockname.len,
190 	     res->lockname.name, master);
191 
192 	if (!master) {
193 		/* drop spinlock...  retake below */
194 		spin_unlock(&dlm->spinlock);
195 
196 		spin_lock(&res->spinlock);
197 		/* This ensures that clear refmap is sent after the set */
198 		__dlm_wait_on_lockres_flags(res, DLM_LOCK_RES_SETREF_INPROG);
199 		spin_unlock(&res->spinlock);
200 
201 		/* clear our bit from the master's refmap, ignore errors */
202 		ret = dlm_drop_lockres_ref(dlm, res);
203 		if (ret < 0) {
204 			mlog_errno(ret);
205 			if (!dlm_is_host_down(ret))
206 				BUG();
207 		}
208 		mlog(0, "%s:%.*s: dlm_deref_lockres returned %d\n",
209 		     dlm->name, res->lockname.len, res->lockname.name, ret);
210 		spin_lock(&dlm->spinlock);
211 	}
212 
213 	spin_lock(&res->spinlock);
214 	if (!list_empty(&res->purge)) {
215 		mlog(0, "removing lockres %.*s:%p from purgelist, "
216 		     "master = %d\n", res->lockname.len, res->lockname.name,
217 		     res, master);
218 		list_del_init(&res->purge);
219 		spin_unlock(&res->spinlock);
220 		dlm_lockres_put(res);
221 		dlm->purge_count--;
222 	} else
223 		spin_unlock(&res->spinlock);
224 
225 	__dlm_unhash_lockres(res);
226 
227 	/* lockres is not in the hash now.  drop the flag and wake up
228 	 * any processes waiting in dlm_get_lock_resource. */
229 	if (!master) {
230 		spin_lock(&res->spinlock);
231 		res->state &= ~DLM_LOCK_RES_DROPPING_REF;
232 		spin_unlock(&res->spinlock);
233 		wake_up(&res->wq);
234 	}
235 	return 0;
236 }
237 
238 static void dlm_run_purge_list(struct dlm_ctxt *dlm,
239 			       int purge_now)
240 {
241 	unsigned int run_max, unused;
242 	unsigned long purge_jiffies;
243 	struct dlm_lock_resource *lockres;
244 
245 	spin_lock(&dlm->spinlock);
246 	run_max = dlm->purge_count;
247 
248 	while(run_max && !list_empty(&dlm->purge_list)) {
249 		run_max--;
250 
251 		lockres = list_entry(dlm->purge_list.next,
252 				     struct dlm_lock_resource, purge);
253 
254 		/* Status of the lockres *might* change so double
255 		 * check. If the lockres is unused, holding the dlm
256 		 * spinlock will prevent people from getting and more
257 		 * refs on it -- there's no need to keep the lockres
258 		 * spinlock. */
259 		spin_lock(&lockres->spinlock);
260 		unused = __dlm_lockres_unused(lockres);
261 		spin_unlock(&lockres->spinlock);
262 
263 		if (!unused)
264 			continue;
265 
266 		purge_jiffies = lockres->last_used +
267 			msecs_to_jiffies(DLM_PURGE_INTERVAL_MS);
268 
269 		/* Make sure that we want to be processing this guy at
270 		 * this time. */
271 		if (!purge_now && time_after(purge_jiffies, jiffies)) {
272 			/* Since resources are added to the purge list
273 			 * in tail order, we can stop at the first
274 			 * unpurgable resource -- anyone added after
275 			 * him will have a greater last_used value */
276 			break;
277 		}
278 
279 		dlm_lockres_get(lockres);
280 
281 		/* This may drop and reacquire the dlm spinlock if it
282 		 * has to do migration. */
283 		if (dlm_purge_lockres(dlm, lockres))
284 			BUG();
285 
286 		dlm_lockres_put(lockres);
287 
288 		/* Avoid adding any scheduling latencies */
289 		cond_resched_lock(&dlm->spinlock);
290 	}
291 
292 	spin_unlock(&dlm->spinlock);
293 }
294 
295 static void dlm_shuffle_lists(struct dlm_ctxt *dlm,
296 			      struct dlm_lock_resource *res)
297 {
298 	struct dlm_lock *lock, *target;
299 	struct list_head *iter;
300 	struct list_head *head;
301 	int can_grant = 1;
302 
303 	//mlog(0, "res->lockname.len=%d\n", res->lockname.len);
304 	//mlog(0, "res->lockname.name=%p\n", res->lockname.name);
305 	//mlog(0, "shuffle res %.*s\n", res->lockname.len,
306 	//	  res->lockname.name);
307 
308 	/* because this function is called with the lockres
309 	 * spinlock, and because we know that it is not migrating/
310 	 * recovering/in-progress, it is fine to reserve asts and
311 	 * basts right before queueing them all throughout */
312 	assert_spin_locked(&dlm->ast_lock);
313 	assert_spin_locked(&res->spinlock);
314 	BUG_ON((res->state & (DLM_LOCK_RES_MIGRATING|
315 			      DLM_LOCK_RES_RECOVERING|
316 			      DLM_LOCK_RES_IN_PROGRESS)));
317 
318 converting:
319 	if (list_empty(&res->converting))
320 		goto blocked;
321 	mlog(0, "res %.*s has locks on a convert queue\n", res->lockname.len,
322 	     res->lockname.name);
323 
324 	target = list_entry(res->converting.next, struct dlm_lock, list);
325 	if (target->ml.convert_type == LKM_IVMODE) {
326 		mlog(ML_ERROR, "%.*s: converting a lock with no "
327 		     "convert_type!\n", res->lockname.len, res->lockname.name);
328 		BUG();
329 	}
330 	head = &res->granted;
331 	list_for_each(iter, head) {
332 		lock = list_entry(iter, struct dlm_lock, list);
333 		if (lock==target)
334 			continue;
335 		if (!dlm_lock_compatible(lock->ml.type,
336 					 target->ml.convert_type)) {
337 			can_grant = 0;
338 			/* queue the BAST if not already */
339 			if (lock->ml.highest_blocked == LKM_IVMODE) {
340 				__dlm_lockres_reserve_ast(res);
341 				__dlm_queue_bast(dlm, lock);
342 			}
343 			/* update the highest_blocked if needed */
344 			if (lock->ml.highest_blocked < target->ml.convert_type)
345 				lock->ml.highest_blocked =
346 					target->ml.convert_type;
347 		}
348 	}
349 	head = &res->converting;
350 	list_for_each(iter, head) {
351 		lock = list_entry(iter, struct dlm_lock, list);
352 		if (lock==target)
353 			continue;
354 		if (!dlm_lock_compatible(lock->ml.type,
355 					 target->ml.convert_type)) {
356 			can_grant = 0;
357 			if (lock->ml.highest_blocked == LKM_IVMODE) {
358 				__dlm_lockres_reserve_ast(res);
359 				__dlm_queue_bast(dlm, lock);
360 			}
361 			if (lock->ml.highest_blocked < target->ml.convert_type)
362 				lock->ml.highest_blocked =
363 					target->ml.convert_type;
364 		}
365 	}
366 
367 	/* we can convert the lock */
368 	if (can_grant) {
369 		spin_lock(&target->spinlock);
370 		BUG_ON(target->ml.highest_blocked != LKM_IVMODE);
371 
372 		mlog(0, "calling ast for converting lock: %.*s, have: %d, "
373 		     "granting: %d, node: %u\n", res->lockname.len,
374 		     res->lockname.name, target->ml.type,
375 		     target->ml.convert_type, target->ml.node);
376 
377 		target->ml.type = target->ml.convert_type;
378 		target->ml.convert_type = LKM_IVMODE;
379 		list_move_tail(&target->list, &res->granted);
380 
381 		BUG_ON(!target->lksb);
382 		target->lksb->status = DLM_NORMAL;
383 
384 		spin_unlock(&target->spinlock);
385 
386 		__dlm_lockres_reserve_ast(res);
387 		__dlm_queue_ast(dlm, target);
388 		/* go back and check for more */
389 		goto converting;
390 	}
391 
392 blocked:
393 	if (list_empty(&res->blocked))
394 		goto leave;
395 	target = list_entry(res->blocked.next, struct dlm_lock, list);
396 
397 	head = &res->granted;
398 	list_for_each(iter, head) {
399 		lock = list_entry(iter, struct dlm_lock, list);
400 		if (lock==target)
401 			continue;
402 		if (!dlm_lock_compatible(lock->ml.type, target->ml.type)) {
403 			can_grant = 0;
404 			if (lock->ml.highest_blocked == LKM_IVMODE) {
405 				__dlm_lockres_reserve_ast(res);
406 				__dlm_queue_bast(dlm, lock);
407 			}
408 			if (lock->ml.highest_blocked < target->ml.type)
409 				lock->ml.highest_blocked = target->ml.type;
410 		}
411 	}
412 
413 	head = &res->converting;
414 	list_for_each(iter, head) {
415 		lock = list_entry(iter, struct dlm_lock, list);
416 		if (lock==target)
417 			continue;
418 		if (!dlm_lock_compatible(lock->ml.type, target->ml.type)) {
419 			can_grant = 0;
420 			if (lock->ml.highest_blocked == LKM_IVMODE) {
421 				__dlm_lockres_reserve_ast(res);
422 				__dlm_queue_bast(dlm, lock);
423 			}
424 			if (lock->ml.highest_blocked < target->ml.type)
425 				lock->ml.highest_blocked = target->ml.type;
426 		}
427 	}
428 
429 	/* we can grant the blocked lock (only
430 	 * possible if converting list empty) */
431 	if (can_grant) {
432 		spin_lock(&target->spinlock);
433 		BUG_ON(target->ml.highest_blocked != LKM_IVMODE);
434 
435 		mlog(0, "calling ast for blocked lock: %.*s, granting: %d, "
436 		     "node: %u\n", res->lockname.len, res->lockname.name,
437 		     target->ml.type, target->ml.node);
438 
439 		// target->ml.type is already correct
440 		list_move_tail(&target->list, &res->granted);
441 
442 		BUG_ON(!target->lksb);
443 		target->lksb->status = DLM_NORMAL;
444 
445 		spin_unlock(&target->spinlock);
446 
447 		__dlm_lockres_reserve_ast(res);
448 		__dlm_queue_ast(dlm, target);
449 		/* go back and check for more */
450 		goto converting;
451 	}
452 
453 leave:
454 	return;
455 }
456 
457 /* must have NO locks when calling this with res !=NULL * */
458 void dlm_kick_thread(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
459 {
460 	mlog_entry("dlm=%p, res=%p\n", dlm, res);
461 	if (res) {
462 		spin_lock(&dlm->spinlock);
463 		spin_lock(&res->spinlock);
464 		__dlm_dirty_lockres(dlm, res);
465 		spin_unlock(&res->spinlock);
466 		spin_unlock(&dlm->spinlock);
467 	}
468 	wake_up(&dlm->dlm_thread_wq);
469 }
470 
471 void __dlm_dirty_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
472 {
473 	mlog_entry("dlm=%p, res=%p\n", dlm, res);
474 
475 	assert_spin_locked(&dlm->spinlock);
476 	assert_spin_locked(&res->spinlock);
477 
478 	/* don't shuffle secondary queues */
479 	if ((res->owner == dlm->node_num)) {
480 		if (res->state & (DLM_LOCK_RES_MIGRATING |
481 				  DLM_LOCK_RES_BLOCK_DIRTY))
482 		    return;
483 
484 		if (list_empty(&res->dirty)) {
485 			/* ref for dirty_list */
486 			dlm_lockres_get(res);
487 			list_add_tail(&res->dirty, &dlm->dirty_list);
488 			res->state |= DLM_LOCK_RES_DIRTY;
489 		}
490 	}
491 }
492 
493 
494 /* Launch the NM thread for the mounted volume */
495 int dlm_launch_thread(struct dlm_ctxt *dlm)
496 {
497 	mlog(0, "starting dlm thread...\n");
498 
499 	dlm->dlm_thread_task = kthread_run(dlm_thread, dlm, "dlm_thread");
500 	if (IS_ERR(dlm->dlm_thread_task)) {
501 		mlog_errno(PTR_ERR(dlm->dlm_thread_task));
502 		dlm->dlm_thread_task = NULL;
503 		return -EINVAL;
504 	}
505 
506 	return 0;
507 }
508 
509 void dlm_complete_thread(struct dlm_ctxt *dlm)
510 {
511 	if (dlm->dlm_thread_task) {
512 		mlog(ML_KTHREAD, "waiting for dlm thread to exit\n");
513 		kthread_stop(dlm->dlm_thread_task);
514 		dlm->dlm_thread_task = NULL;
515 	}
516 }
517 
518 static int dlm_dirty_list_empty(struct dlm_ctxt *dlm)
519 {
520 	int empty;
521 
522 	spin_lock(&dlm->spinlock);
523 	empty = list_empty(&dlm->dirty_list);
524 	spin_unlock(&dlm->spinlock);
525 
526 	return empty;
527 }
528 
529 static void dlm_flush_asts(struct dlm_ctxt *dlm)
530 {
531 	int ret;
532 	struct dlm_lock *lock;
533 	struct dlm_lock_resource *res;
534 	u8 hi;
535 
536 	spin_lock(&dlm->ast_lock);
537 	while (!list_empty(&dlm->pending_asts)) {
538 		lock = list_entry(dlm->pending_asts.next,
539 				  struct dlm_lock, ast_list);
540 		/* get an extra ref on lock */
541 		dlm_lock_get(lock);
542 		res = lock->lockres;
543 		mlog(0, "delivering an ast for this lockres\n");
544 
545 		BUG_ON(!lock->ast_pending);
546 
547 		/* remove from list (including ref) */
548 		list_del_init(&lock->ast_list);
549 		dlm_lock_put(lock);
550 		spin_unlock(&dlm->ast_lock);
551 
552 		if (lock->ml.node != dlm->node_num) {
553 			ret = dlm_do_remote_ast(dlm, res, lock);
554 			if (ret < 0)
555 				mlog_errno(ret);
556 		} else
557 			dlm_do_local_ast(dlm, res, lock);
558 
559 		spin_lock(&dlm->ast_lock);
560 
561 		/* possible that another ast was queued while
562 		 * we were delivering the last one */
563 		if (!list_empty(&lock->ast_list)) {
564 			mlog(0, "aha another ast got queued while "
565 			     "we were finishing the last one.  will "
566 			     "keep the ast_pending flag set.\n");
567 		} else
568 			lock->ast_pending = 0;
569 
570 		/* drop the extra ref.
571 		 * this may drop it completely. */
572 		dlm_lock_put(lock);
573 		dlm_lockres_release_ast(dlm, res);
574 	}
575 
576 	while (!list_empty(&dlm->pending_basts)) {
577 		lock = list_entry(dlm->pending_basts.next,
578 				  struct dlm_lock, bast_list);
579 		/* get an extra ref on lock */
580 		dlm_lock_get(lock);
581 		res = lock->lockres;
582 
583 		BUG_ON(!lock->bast_pending);
584 
585 		/* get the highest blocked lock, and reset */
586 		spin_lock(&lock->spinlock);
587 		BUG_ON(lock->ml.highest_blocked <= LKM_IVMODE);
588 		hi = lock->ml.highest_blocked;
589 		lock->ml.highest_blocked = LKM_IVMODE;
590 		spin_unlock(&lock->spinlock);
591 
592 		/* remove from list (including ref) */
593 		list_del_init(&lock->bast_list);
594 		dlm_lock_put(lock);
595 		spin_unlock(&dlm->ast_lock);
596 
597 		mlog(0, "delivering a bast for this lockres "
598 		     "(blocked = %d\n", hi);
599 
600 		if (lock->ml.node != dlm->node_num) {
601 			ret = dlm_send_proxy_bast(dlm, res, lock, hi);
602 			if (ret < 0)
603 				mlog_errno(ret);
604 		} else
605 			dlm_do_local_bast(dlm, res, lock, hi);
606 
607 		spin_lock(&dlm->ast_lock);
608 
609 		/* possible that another bast was queued while
610 		 * we were delivering the last one */
611 		if (!list_empty(&lock->bast_list)) {
612 			mlog(0, "aha another bast got queued while "
613 			     "we were finishing the last one.  will "
614 			     "keep the bast_pending flag set.\n");
615 		} else
616 			lock->bast_pending = 0;
617 
618 		/* drop the extra ref.
619 		 * this may drop it completely. */
620 		dlm_lock_put(lock);
621 		dlm_lockres_release_ast(dlm, res);
622 	}
623 	wake_up(&dlm->ast_wq);
624 	spin_unlock(&dlm->ast_lock);
625 }
626 
627 
628 #define DLM_THREAD_TIMEOUT_MS (4 * 1000)
629 #define DLM_THREAD_MAX_DIRTY  100
630 #define DLM_THREAD_MAX_ASTS   10
631 
632 static int dlm_thread(void *data)
633 {
634 	struct dlm_lock_resource *res;
635 	struct dlm_ctxt *dlm = data;
636 	unsigned long timeout = msecs_to_jiffies(DLM_THREAD_TIMEOUT_MS);
637 
638 	mlog(0, "dlm thread running for %s...\n", dlm->name);
639 
640 	while (!kthread_should_stop()) {
641 		int n = DLM_THREAD_MAX_DIRTY;
642 
643 		/* dlm_shutting_down is very point-in-time, but that
644 		 * doesn't matter as we'll just loop back around if we
645 		 * get false on the leading edge of a state
646 		 * transition. */
647 		dlm_run_purge_list(dlm, dlm_shutting_down(dlm));
648 
649 		/* We really don't want to hold dlm->spinlock while
650 		 * calling dlm_shuffle_lists on each lockres that
651 		 * needs to have its queues adjusted and AST/BASTs
652 		 * run.  So let's pull each entry off the dirty_list
653 		 * and drop dlm->spinlock ASAP.  Once off the list,
654 		 * res->spinlock needs to be taken again to protect
655 		 * the queues while calling dlm_shuffle_lists.  */
656 		spin_lock(&dlm->spinlock);
657 		while (!list_empty(&dlm->dirty_list)) {
658 			int delay = 0;
659 			res = list_entry(dlm->dirty_list.next,
660 					 struct dlm_lock_resource, dirty);
661 
662 			/* peel a lockres off, remove it from the list,
663 			 * unset the dirty flag and drop the dlm lock */
664 			BUG_ON(!res);
665 			dlm_lockres_get(res);
666 
667 			spin_lock(&res->spinlock);
668 			/* We clear the DLM_LOCK_RES_DIRTY state once we shuffle lists below */
669 			list_del_init(&res->dirty);
670 			spin_unlock(&res->spinlock);
671 			spin_unlock(&dlm->spinlock);
672 			/* Drop dirty_list ref */
673 			dlm_lockres_put(res);
674 
675 		 	/* lockres can be re-dirtied/re-added to the
676 			 * dirty_list in this gap, but that is ok */
677 
678 			spin_lock(&dlm->ast_lock);
679 			spin_lock(&res->spinlock);
680 			if (res->owner != dlm->node_num) {
681 				__dlm_print_one_lock_resource(res);
682 				mlog(ML_ERROR, "inprog:%s, mig:%s, reco:%s, dirty:%s\n",
683 				     res->state & DLM_LOCK_RES_IN_PROGRESS ? "yes" : "no",
684 				     res->state & DLM_LOCK_RES_MIGRATING ? "yes" : "no",
685 				     res->state & DLM_LOCK_RES_RECOVERING ? "yes" : "no",
686 				     res->state & DLM_LOCK_RES_DIRTY ? "yes" : "no");
687 			}
688 			BUG_ON(res->owner != dlm->node_num);
689 
690 			/* it is now ok to move lockreses in these states
691 			 * to the dirty list, assuming that they will only be
692 			 * dirty for a short while. */
693 			BUG_ON(res->state & DLM_LOCK_RES_MIGRATING);
694 			if (res->state & (DLM_LOCK_RES_IN_PROGRESS |
695 					  DLM_LOCK_RES_RECOVERING)) {
696 				/* move it to the tail and keep going */
697 				res->state &= ~DLM_LOCK_RES_DIRTY;
698 				spin_unlock(&res->spinlock);
699 				spin_unlock(&dlm->ast_lock);
700 				mlog(0, "delaying list shuffling for in-"
701 				     "progress lockres %.*s, state=%d\n",
702 				     res->lockname.len, res->lockname.name,
703 				     res->state);
704 				delay = 1;
705 				goto in_progress;
706 			}
707 
708 			/* at this point the lockres is not migrating/
709 			 * recovering/in-progress.  we have the lockres
710 			 * spinlock and do NOT have the dlm lock.
711 			 * safe to reserve/queue asts and run the lists. */
712 
713 			mlog(0, "calling dlm_shuffle_lists with dlm=%s, "
714 			     "res=%.*s\n", dlm->name,
715 			     res->lockname.len, res->lockname.name);
716 
717 			/* called while holding lockres lock */
718 			dlm_shuffle_lists(dlm, res);
719 			res->state &= ~DLM_LOCK_RES_DIRTY;
720 			spin_unlock(&res->spinlock);
721 			spin_unlock(&dlm->ast_lock);
722 
723 			dlm_lockres_calc_usage(dlm, res);
724 
725 in_progress:
726 
727 			spin_lock(&dlm->spinlock);
728 			/* if the lock was in-progress, stick
729 			 * it on the back of the list */
730 			if (delay) {
731 				spin_lock(&res->spinlock);
732 				__dlm_dirty_lockres(dlm, res);
733 				spin_unlock(&res->spinlock);
734 			}
735 			dlm_lockres_put(res);
736 
737 			/* unlikely, but we may need to give time to
738 			 * other tasks */
739 			if (!--n) {
740 				mlog(0, "throttling dlm_thread\n");
741 				break;
742 			}
743 		}
744 
745 		spin_unlock(&dlm->spinlock);
746 		dlm_flush_asts(dlm);
747 
748 		/* yield and continue right away if there is more work to do */
749 		if (!n) {
750 			cond_resched();
751 			continue;
752 		}
753 
754 		wait_event_interruptible_timeout(dlm->dlm_thread_wq,
755 						 !dlm_dirty_list_empty(dlm) ||
756 						 kthread_should_stop(),
757 						 timeout);
758 	}
759 
760 	mlog(0, "quitting DLM thread\n");
761 	return 0;
762 }
763