OSDN Git Service

ocfs2/dlm: return in progress if master can not clear the refmap bit right now
[uclinux-h8/linux.git] / fs / ocfs2 / dlm / dlmthread.c
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         int bit;
96
97         assert_spin_locked(&res->spinlock);
98
99         if (__dlm_lockres_has_locks(res))
100                 return 0;
101
102         /* Locks are in the process of being created */
103         if (res->inflight_locks)
104                 return 0;
105
106         if (!list_empty(&res->dirty) || res->state & DLM_LOCK_RES_DIRTY)
107                 return 0;
108
109         if (res->state & DLM_LOCK_RES_RECOVERING)
110                 return 0;
111
112         /* Another node has this resource with this node as the master */
113         bit = find_next_bit(res->refmap, O2NM_MAX_NODES, 0);
114         if (bit < O2NM_MAX_NODES)
115                 return 0;
116
117         return 1;
118 }
119
120
121 /* Call whenever you may have added or deleted something from one of
122  * the lockres queue's. This will figure out whether it belongs on the
123  * unused list or not and does the appropriate thing. */
124 void __dlm_lockres_calc_usage(struct dlm_ctxt *dlm,
125                               struct dlm_lock_resource *res)
126 {
127         assert_spin_locked(&dlm->spinlock);
128         assert_spin_locked(&res->spinlock);
129
130         if (__dlm_lockres_unused(res)){
131                 if (list_empty(&res->purge)) {
132                         mlog(0, "%s: Adding res %.*s to purge list\n",
133                              dlm->name, res->lockname.len, res->lockname.name);
134
135                         res->last_used = jiffies;
136                         dlm_lockres_get(res);
137                         list_add_tail(&res->purge, &dlm->purge_list);
138                         dlm->purge_count++;
139                 }
140         } else if (!list_empty(&res->purge)) {
141                 mlog(0, "%s: Removing res %.*s from purge list\n",
142                      dlm->name, res->lockname.len, res->lockname.name);
143
144                 list_del_init(&res->purge);
145                 dlm_lockres_put(res);
146                 dlm->purge_count--;
147         }
148 }
149
150 void dlm_lockres_calc_usage(struct dlm_ctxt *dlm,
151                             struct dlm_lock_resource *res)
152 {
153         spin_lock(&dlm->spinlock);
154         spin_lock(&res->spinlock);
155
156         __dlm_lockres_calc_usage(dlm, res);
157
158         spin_unlock(&res->spinlock);
159         spin_unlock(&dlm->spinlock);
160 }
161
162 static void dlm_purge_lockres(struct dlm_ctxt *dlm,
163                              struct dlm_lock_resource *res)
164 {
165         int master;
166         int ret = 0;
167
168         assert_spin_locked(&dlm->spinlock);
169         assert_spin_locked(&res->spinlock);
170
171         master = (res->owner == dlm->node_num);
172
173         mlog(0, "%s: Purging res %.*s, master %d\n", dlm->name,
174              res->lockname.len, res->lockname.name, master);
175
176         if (!master) {
177                 res->state |= DLM_LOCK_RES_DROPPING_REF;
178                 /* drop spinlock...  retake below */
179                 spin_unlock(&res->spinlock);
180                 spin_unlock(&dlm->spinlock);
181
182                 spin_lock(&res->spinlock);
183                 /* This ensures that clear refmap is sent after the set */
184                 __dlm_wait_on_lockres_flags(res, DLM_LOCK_RES_SETREF_INPROG);
185                 spin_unlock(&res->spinlock);
186
187                 /* clear our bit from the master's refmap, ignore errors */
188                 ret = dlm_drop_lockres_ref(dlm, res);
189                 if (ret < 0) {
190                         if (!dlm_is_host_down(ret))
191                                 BUG();
192                 }
193                 spin_lock(&dlm->spinlock);
194                 spin_lock(&res->spinlock);
195         }
196
197         if (!list_empty(&res->purge)) {
198                 mlog(0, "%s: Removing res %.*s from purgelist, master %d\n",
199                      dlm->name, res->lockname.len, res->lockname.name, master);
200                 list_del_init(&res->purge);
201                 dlm_lockres_put(res);
202                 dlm->purge_count--;
203         }
204
205         if (!master && ret != 0) {
206                 mlog(0, "%s: deref %.*s in progress or master goes down\n",
207                         dlm->name, res->lockname.len, res->lockname.name);
208                 spin_unlock(&res->spinlock);
209                 return;
210         }
211
212         if (!__dlm_lockres_unused(res)) {
213                 mlog(ML_ERROR, "%s: res %.*s in use after deref\n",
214                      dlm->name, res->lockname.len, res->lockname.name);
215                 __dlm_print_one_lock_resource(res);
216                 BUG();
217         }
218
219         __dlm_unhash_lockres(dlm, res);
220
221         spin_lock(&dlm->track_lock);
222         if (!list_empty(&res->tracking))
223                 list_del_init(&res->tracking);
224         else {
225                 mlog(ML_ERROR, "Resource %.*s not on the Tracking list\n",
226                                 res->lockname.len, res->lockname.name);
227                 __dlm_print_one_lock_resource(res);
228         }
229         spin_unlock(&dlm->track_lock);
230
231         /* lockres is not in the hash now.  drop the flag and wake up
232          * any processes waiting in dlm_get_lock_resource. */
233         if (!master) {
234                 res->state &= ~DLM_LOCK_RES_DROPPING_REF;
235                 spin_unlock(&res->spinlock);
236                 wake_up(&res->wq);
237         } else
238                 spin_unlock(&res->spinlock);
239 }
240
241 static void dlm_run_purge_list(struct dlm_ctxt *dlm,
242                                int purge_now)
243 {
244         unsigned int run_max, unused;
245         unsigned long purge_jiffies;
246         struct dlm_lock_resource *lockres;
247
248         spin_lock(&dlm->spinlock);
249         run_max = dlm->purge_count;
250
251         while(run_max && !list_empty(&dlm->purge_list)) {
252                 run_max--;
253
254                 lockres = list_entry(dlm->purge_list.next,
255                                      struct dlm_lock_resource, purge);
256
257                 spin_lock(&lockres->spinlock);
258
259                 purge_jiffies = lockres->last_used +
260                         msecs_to_jiffies(DLM_PURGE_INTERVAL_MS);
261
262                 /* Make sure that we want to be processing this guy at
263                  * this time. */
264                 if (!purge_now && time_after(purge_jiffies, jiffies)) {
265                         /* Since resources are added to the purge list
266                          * in tail order, we can stop at the first
267                          * unpurgable resource -- anyone added after
268                          * him will have a greater last_used value */
269                         spin_unlock(&lockres->spinlock);
270                         break;
271                 }
272
273                 /* Status of the lockres *might* change so double
274                  * check. If the lockres is unused, holding the dlm
275                  * spinlock will prevent people from getting and more
276                  * refs on it. */
277                 unused = __dlm_lockres_unused(lockres);
278                 if (!unused ||
279                     (lockres->state & DLM_LOCK_RES_MIGRATING) ||
280                     (lockres->inflight_assert_workers != 0)) {
281                         mlog(0, "%s: res %.*s is in use or being remastered, "
282                              "used %d, state %d, assert master workers %u\n",
283                              dlm->name, lockres->lockname.len,
284                              lockres->lockname.name,
285                              !unused, lockres->state,
286                              lockres->inflight_assert_workers);
287                         list_move_tail(&lockres->purge, &dlm->purge_list);
288                         spin_unlock(&lockres->spinlock);
289                         continue;
290                 }
291
292                 dlm_lockres_get(lockres);
293
294                 dlm_purge_lockres(dlm, lockres);
295
296                 dlm_lockres_put(lockres);
297
298                 /* Avoid adding any scheduling latencies */
299                 cond_resched_lock(&dlm->spinlock);
300         }
301
302         spin_unlock(&dlm->spinlock);
303 }
304
305 static void dlm_shuffle_lists(struct dlm_ctxt *dlm,
306                               struct dlm_lock_resource *res)
307 {
308         struct dlm_lock *lock, *target;
309         int can_grant = 1;
310
311         /*
312          * Because this function is called with the lockres
313          * spinlock, and because we know that it is not migrating/
314          * recovering/in-progress, it is fine to reserve asts and
315          * basts right before queueing them all throughout
316          */
317         assert_spin_locked(&dlm->ast_lock);
318         assert_spin_locked(&res->spinlock);
319         BUG_ON((res->state & (DLM_LOCK_RES_MIGRATING|
320                               DLM_LOCK_RES_RECOVERING|
321                               DLM_LOCK_RES_IN_PROGRESS)));
322
323 converting:
324         if (list_empty(&res->converting))
325                 goto blocked;
326         mlog(0, "%s: res %.*s has locks on the convert queue\n", dlm->name,
327              res->lockname.len, res->lockname.name);
328
329         target = list_entry(res->converting.next, struct dlm_lock, list);
330         if (target->ml.convert_type == LKM_IVMODE) {
331                 mlog(ML_ERROR, "%s: res %.*s converting lock to invalid mode\n",
332                      dlm->name, res->lockname.len, res->lockname.name);
333                 BUG();
334         }
335         list_for_each_entry(lock, &res->granted, list) {
336                 if (lock==target)
337                         continue;
338                 if (!dlm_lock_compatible(lock->ml.type,
339                                          target->ml.convert_type)) {
340                         can_grant = 0;
341                         /* queue the BAST if not already */
342                         if (lock->ml.highest_blocked == LKM_IVMODE) {
343                                 __dlm_lockres_reserve_ast(res);
344                                 __dlm_queue_bast(dlm, lock);
345                         }
346                         /* update the highest_blocked if needed */
347                         if (lock->ml.highest_blocked < target->ml.convert_type)
348                                 lock->ml.highest_blocked =
349                                         target->ml.convert_type;
350                 }
351         }
352
353         list_for_each_entry(lock, &res->converting, list) {
354                 if (lock==target)
355                         continue;
356                 if (!dlm_lock_compatible(lock->ml.type,
357                                          target->ml.convert_type)) {
358                         can_grant = 0;
359                         if (lock->ml.highest_blocked == LKM_IVMODE) {
360                                 __dlm_lockres_reserve_ast(res);
361                                 __dlm_queue_bast(dlm, lock);
362                         }
363                         if (lock->ml.highest_blocked < target->ml.convert_type)
364                                 lock->ml.highest_blocked =
365                                         target->ml.convert_type;
366                 }
367         }
368
369         /* we can convert the lock */
370         if (can_grant) {
371                 spin_lock(&target->spinlock);
372                 BUG_ON(target->ml.highest_blocked != LKM_IVMODE);
373
374                 mlog(0, "%s: res %.*s, AST for Converting lock %u:%llu, type "
375                      "%d => %d, node %u\n", dlm->name, res->lockname.len,
376                      res->lockname.name,
377                      dlm_get_lock_cookie_node(be64_to_cpu(target->ml.cookie)),
378                      dlm_get_lock_cookie_seq(be64_to_cpu(target->ml.cookie)),
379                      target->ml.type,
380                      target->ml.convert_type, target->ml.node);
381
382                 target->ml.type = target->ml.convert_type;
383                 target->ml.convert_type = LKM_IVMODE;
384                 list_move_tail(&target->list, &res->granted);
385
386                 BUG_ON(!target->lksb);
387                 target->lksb->status = DLM_NORMAL;
388
389                 spin_unlock(&target->spinlock);
390
391                 __dlm_lockres_reserve_ast(res);
392                 __dlm_queue_ast(dlm, target);
393                 /* go back and check for more */
394                 goto converting;
395         }
396
397 blocked:
398         if (list_empty(&res->blocked))
399                 goto leave;
400         target = list_entry(res->blocked.next, struct dlm_lock, list);
401
402         list_for_each_entry(lock, &res->granted, list) {
403                 if (lock==target)
404                         continue;
405                 if (!dlm_lock_compatible(lock->ml.type, target->ml.type)) {
406                         can_grant = 0;
407                         if (lock->ml.highest_blocked == LKM_IVMODE) {
408                                 __dlm_lockres_reserve_ast(res);
409                                 __dlm_queue_bast(dlm, lock);
410                         }
411                         if (lock->ml.highest_blocked < target->ml.type)
412                                 lock->ml.highest_blocked = target->ml.type;
413                 }
414         }
415
416         list_for_each_entry(lock, &res->converting, list) {
417                 if (lock==target)
418                         continue;
419                 if (!dlm_lock_compatible(lock->ml.type, target->ml.type)) {
420                         can_grant = 0;
421                         if (lock->ml.highest_blocked == LKM_IVMODE) {
422                                 __dlm_lockres_reserve_ast(res);
423                                 __dlm_queue_bast(dlm, lock);
424                         }
425                         if (lock->ml.highest_blocked < target->ml.type)
426                                 lock->ml.highest_blocked = target->ml.type;
427                 }
428         }
429
430         /* we can grant the blocked lock (only
431          * possible if converting list empty) */
432         if (can_grant) {
433                 spin_lock(&target->spinlock);
434                 BUG_ON(target->ml.highest_blocked != LKM_IVMODE);
435
436                 mlog(0, "%s: res %.*s, AST for Blocked lock %u:%llu, type %d, "
437                      "node %u\n", dlm->name, res->lockname.len,
438                      res->lockname.name,
439                      dlm_get_lock_cookie_node(be64_to_cpu(target->ml.cookie)),
440                      dlm_get_lock_cookie_seq(be64_to_cpu(target->ml.cookie)),
441                      target->ml.type, target->ml.node);
442
443                 /* target->ml.type is already correct */
444                 list_move_tail(&target->list, &res->granted);
445
446                 BUG_ON(!target->lksb);
447                 target->lksb->status = DLM_NORMAL;
448
449                 spin_unlock(&target->spinlock);
450
451                 __dlm_lockres_reserve_ast(res);
452                 __dlm_queue_ast(dlm, target);
453                 /* go back and check for more */
454                 goto converting;
455         }
456
457 leave:
458         return;
459 }
460
461 /* must have NO locks when calling this with res !=NULL * */
462 void dlm_kick_thread(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
463 {
464         if (res) {
465                 spin_lock(&dlm->spinlock);
466                 spin_lock(&res->spinlock);
467                 __dlm_dirty_lockres(dlm, res);
468                 spin_unlock(&res->spinlock);
469                 spin_unlock(&dlm->spinlock);
470         }
471         wake_up(&dlm->dlm_thread_wq);
472 }
473
474 void __dlm_dirty_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
475 {
476         assert_spin_locked(&dlm->spinlock);
477         assert_spin_locked(&res->spinlock);
478
479         /* don't shuffle secondary queues */
480         if ((res->owner == dlm->node_num)) {
481                 if (res->state & (DLM_LOCK_RES_MIGRATING |
482                                   DLM_LOCK_RES_BLOCK_DIRTY))
483                     return;
484
485                 if (list_empty(&res->dirty)) {
486                         /* ref for dirty_list */
487                         dlm_lockres_get(res);
488                         list_add_tail(&res->dirty, &dlm->dirty_list);
489                         res->state |= DLM_LOCK_RES_DIRTY;
490                 }
491         }
492
493         mlog(0, "%s: res %.*s\n", dlm->name, res->lockname.len,
494              res->lockname.name);
495 }
496
497
498 /* Launch the NM thread for the mounted volume */
499 int dlm_launch_thread(struct dlm_ctxt *dlm)
500 {
501         mlog(0, "Starting dlm_thread...\n");
502
503         dlm->dlm_thread_task = kthread_run(dlm_thread, dlm, "dlm-%s",
504                         dlm->name);
505         if (IS_ERR(dlm->dlm_thread_task)) {
506                 mlog_errno(PTR_ERR(dlm->dlm_thread_task));
507                 dlm->dlm_thread_task = NULL;
508                 return -EINVAL;
509         }
510
511         return 0;
512 }
513
514 void dlm_complete_thread(struct dlm_ctxt *dlm)
515 {
516         if (dlm->dlm_thread_task) {
517                 mlog(ML_KTHREAD, "Waiting for dlm thread to exit\n");
518                 kthread_stop(dlm->dlm_thread_task);
519                 dlm->dlm_thread_task = NULL;
520         }
521 }
522
523 static int dlm_dirty_list_empty(struct dlm_ctxt *dlm)
524 {
525         int empty;
526
527         spin_lock(&dlm->spinlock);
528         empty = list_empty(&dlm->dirty_list);
529         spin_unlock(&dlm->spinlock);
530
531         return empty;
532 }
533
534 static void dlm_flush_asts(struct dlm_ctxt *dlm)
535 {
536         int ret;
537         struct dlm_lock *lock;
538         struct dlm_lock_resource *res;
539         u8 hi;
540
541         spin_lock(&dlm->ast_lock);
542         while (!list_empty(&dlm->pending_asts)) {
543                 lock = list_entry(dlm->pending_asts.next,
544                                   struct dlm_lock, ast_list);
545                 /* get an extra ref on lock */
546                 dlm_lock_get(lock);
547                 res = lock->lockres;
548                 mlog(0, "%s: res %.*s, Flush AST for lock %u:%llu, type %d, "
549                      "node %u\n", dlm->name, res->lockname.len,
550                      res->lockname.name,
551                      dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
552                      dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)),
553                      lock->ml.type, lock->ml.node);
554
555                 BUG_ON(!lock->ast_pending);
556
557                 /* remove from list (including ref) */
558                 list_del_init(&lock->ast_list);
559                 dlm_lock_put(lock);
560                 spin_unlock(&dlm->ast_lock);
561
562                 if (lock->ml.node != dlm->node_num) {
563                         ret = dlm_do_remote_ast(dlm, res, lock);
564                         if (ret < 0)
565                                 mlog_errno(ret);
566                 } else
567                         dlm_do_local_ast(dlm, res, lock);
568
569                 spin_lock(&dlm->ast_lock);
570
571                 /* possible that another ast was queued while
572                  * we were delivering the last one */
573                 if (!list_empty(&lock->ast_list)) {
574                         mlog(0, "%s: res %.*s, AST queued while flushing last "
575                              "one\n", dlm->name, res->lockname.len,
576                              res->lockname.name);
577                 } else
578                         lock->ast_pending = 0;
579
580                 /* drop the extra ref.
581                  * this may drop it completely. */
582                 dlm_lock_put(lock);
583                 dlm_lockres_release_ast(dlm, res);
584         }
585
586         while (!list_empty(&dlm->pending_basts)) {
587                 lock = list_entry(dlm->pending_basts.next,
588                                   struct dlm_lock, bast_list);
589                 /* get an extra ref on lock */
590                 dlm_lock_get(lock);
591                 res = lock->lockres;
592
593                 BUG_ON(!lock->bast_pending);
594
595                 /* get the highest blocked lock, and reset */
596                 spin_lock(&lock->spinlock);
597                 BUG_ON(lock->ml.highest_blocked <= LKM_IVMODE);
598                 hi = lock->ml.highest_blocked;
599                 lock->ml.highest_blocked = LKM_IVMODE;
600                 spin_unlock(&lock->spinlock);
601
602                 /* remove from list (including ref) */
603                 list_del_init(&lock->bast_list);
604                 dlm_lock_put(lock);
605                 spin_unlock(&dlm->ast_lock);
606
607                 mlog(0, "%s: res %.*s, Flush BAST for lock %u:%llu, "
608                      "blocked %d, node %u\n",
609                      dlm->name, res->lockname.len, res->lockname.name,
610                      dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
611                      dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)),
612                      hi, lock->ml.node);
613
614                 if (lock->ml.node != dlm->node_num) {
615                         ret = dlm_send_proxy_bast(dlm, res, lock, hi);
616                         if (ret < 0)
617                                 mlog_errno(ret);
618                 } else
619                         dlm_do_local_bast(dlm, res, lock, hi);
620
621                 spin_lock(&dlm->ast_lock);
622
623                 /* possible that another bast was queued while
624                  * we were delivering the last one */
625                 if (!list_empty(&lock->bast_list)) {
626                         mlog(0, "%s: res %.*s, BAST queued while flushing last "
627                              "one\n", dlm->name, res->lockname.len,
628                              res->lockname.name);
629                 } else
630                         lock->bast_pending = 0;
631
632                 /* drop the extra ref.
633                  * this may drop it completely. */
634                 dlm_lock_put(lock);
635                 dlm_lockres_release_ast(dlm, res);
636         }
637         wake_up(&dlm->ast_wq);
638         spin_unlock(&dlm->ast_lock);
639 }
640
641
642 #define DLM_THREAD_TIMEOUT_MS (4 * 1000)
643 #define DLM_THREAD_MAX_DIRTY  100
644 #define DLM_THREAD_MAX_ASTS   10
645
646 static int dlm_thread(void *data)
647 {
648         struct dlm_lock_resource *res;
649         struct dlm_ctxt *dlm = data;
650         unsigned long timeout = msecs_to_jiffies(DLM_THREAD_TIMEOUT_MS);
651
652         mlog(0, "dlm thread running for %s...\n", dlm->name);
653
654         while (!kthread_should_stop()) {
655                 int n = DLM_THREAD_MAX_DIRTY;
656
657                 /* dlm_shutting_down is very point-in-time, but that
658                  * doesn't matter as we'll just loop back around if we
659                  * get false on the leading edge of a state
660                  * transition. */
661                 dlm_run_purge_list(dlm, dlm_shutting_down(dlm));
662
663                 /* We really don't want to hold dlm->spinlock while
664                  * calling dlm_shuffle_lists on each lockres that
665                  * needs to have its queues adjusted and AST/BASTs
666                  * run.  So let's pull each entry off the dirty_list
667                  * and drop dlm->spinlock ASAP.  Once off the list,
668                  * res->spinlock needs to be taken again to protect
669                  * the queues while calling dlm_shuffle_lists.  */
670                 spin_lock(&dlm->spinlock);
671                 while (!list_empty(&dlm->dirty_list)) {
672                         int delay = 0;
673                         res = list_entry(dlm->dirty_list.next,
674                                          struct dlm_lock_resource, dirty);
675
676                         /* peel a lockres off, remove it from the list,
677                          * unset the dirty flag and drop the dlm lock */
678                         BUG_ON(!res);
679                         dlm_lockres_get(res);
680
681                         spin_lock(&res->spinlock);
682                         /* We clear the DLM_LOCK_RES_DIRTY state once we shuffle lists below */
683                         list_del_init(&res->dirty);
684                         spin_unlock(&res->spinlock);
685                         spin_unlock(&dlm->spinlock);
686                         /* Drop dirty_list ref */
687                         dlm_lockres_put(res);
688
689                         /* lockres can be re-dirtied/re-added to the
690                          * dirty_list in this gap, but that is ok */
691
692                         spin_lock(&dlm->ast_lock);
693                         spin_lock(&res->spinlock);
694                         if (res->owner != dlm->node_num) {
695                                 __dlm_print_one_lock_resource(res);
696                                 mlog(ML_ERROR, "%s: inprog %d, mig %d, reco %d,"
697                                      " dirty %d\n", dlm->name,
698                                      !!(res->state & DLM_LOCK_RES_IN_PROGRESS),
699                                      !!(res->state & DLM_LOCK_RES_MIGRATING),
700                                      !!(res->state & DLM_LOCK_RES_RECOVERING),
701                                      !!(res->state & DLM_LOCK_RES_DIRTY));
702                         }
703                         BUG_ON(res->owner != dlm->node_num);
704
705                         /* it is now ok to move lockreses in these states
706                          * to the dirty list, assuming that they will only be
707                          * dirty for a short while. */
708                         BUG_ON(res->state & DLM_LOCK_RES_MIGRATING);
709                         if (res->state & (DLM_LOCK_RES_IN_PROGRESS |
710                                           DLM_LOCK_RES_RECOVERING)) {
711                                 /* move it to the tail and keep going */
712                                 res->state &= ~DLM_LOCK_RES_DIRTY;
713                                 spin_unlock(&res->spinlock);
714                                 spin_unlock(&dlm->ast_lock);
715                                 mlog(0, "%s: res %.*s, inprogress, delay list "
716                                      "shuffle, state %d\n", dlm->name,
717                                      res->lockname.len, res->lockname.name,
718                                      res->state);
719                                 delay = 1;
720                                 goto in_progress;
721                         }
722
723                         /* at this point the lockres is not migrating/
724                          * recovering/in-progress.  we have the lockres
725                          * spinlock and do NOT have the dlm lock.
726                          * safe to reserve/queue asts and run the lists. */
727
728                         /* called while holding lockres lock */
729                         dlm_shuffle_lists(dlm, res);
730                         res->state &= ~DLM_LOCK_RES_DIRTY;
731                         spin_unlock(&res->spinlock);
732                         spin_unlock(&dlm->ast_lock);
733
734                         dlm_lockres_calc_usage(dlm, res);
735
736 in_progress:
737
738                         spin_lock(&dlm->spinlock);
739                         /* if the lock was in-progress, stick
740                          * it on the back of the list */
741                         if (delay) {
742                                 spin_lock(&res->spinlock);
743                                 __dlm_dirty_lockres(dlm, res);
744                                 spin_unlock(&res->spinlock);
745                         }
746                         dlm_lockres_put(res);
747
748                         /* unlikely, but we may need to give time to
749                          * other tasks */
750                         if (!--n) {
751                                 mlog(0, "%s: Throttling dlm thread\n",
752                                      dlm->name);
753                                 break;
754                         }
755                 }
756
757                 spin_unlock(&dlm->spinlock);
758                 dlm_flush_asts(dlm);
759
760                 /* yield and continue right away if there is more work to do */
761                 if (!n) {
762                         cond_resched();
763                         continue;
764                 }
765
766                 wait_event_interruptible_timeout(dlm->dlm_thread_wq,
767                                                  !dlm_dirty_list_empty(dlm) ||
768                                                  kthread_should_stop(),
769                                                  timeout);
770         }
771
772         mlog(0, "quitting DLM thread\n");
773         return 0;
774 }