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