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