blob: dc2ad6008b2d08a354ce23507c44febb121b6466 [file] [log] [blame]
David Teiglande7fd4172006-01-18 09:30:29 +00001/******************************************************************************
2*******************************************************************************
3**
4** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
David Teiglandfd22a512008-12-09 11:55:46 -06005** Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
David Teiglande7fd4172006-01-18 09:30:29 +00006**
7** This copyrighted material is made available to anyone wishing to use,
8** modify, copy, or redistribute it subject to the terms and conditions
9** of the GNU General Public License v.2.
10**
11*******************************************************************************
12******************************************************************************/
13
14#include "dlm_internal.h"
15#include "lock.h"
David Teigland597d0ca2006-07-12 16:44:04 -050016#include "user.h"
Adrian Bunk8fa1de32007-04-04 17:25:29 +020017#include "ast.h"
David Teiglande7fd4172006-01-18 09:30:29 +000018
19#define WAKE_ASTS 0
20
21static struct list_head ast_queue;
22static spinlock_t ast_queue_lock;
23static struct task_struct * astd_task;
24static unsigned long astd_wakeflags;
David Teigland90135922006-01-20 08:47:07 +000025static struct mutex astd_running;
David Teiglande7fd4172006-01-18 09:30:29 +000026
27
28void dlm_del_ast(struct dlm_lkb *lkb)
29{
30 spin_lock(&ast_queue_lock);
31 if (lkb->lkb_ast_type & (AST_COMP | AST_BAST))
32 list_del(&lkb->lkb_astqueue);
33 spin_unlock(&ast_queue_lock);
34}
35
David Teiglandfd22a512008-12-09 11:55:46 -060036void dlm_add_ast(struct dlm_lkb *lkb, int type, int bastmode)
David Teiglande7fd4172006-01-18 09:30:29 +000037{
David Teigland597d0ca2006-07-12 16:44:04 -050038 if (lkb->lkb_flags & DLM_IFL_USER) {
David Teiglandfd22a512008-12-09 11:55:46 -060039 dlm_user_add_ast(lkb, type, bastmode);
David Teigland597d0ca2006-07-12 16:44:04 -050040 return;
41 }
42
David Teiglande7fd4172006-01-18 09:30:29 +000043 spin_lock(&ast_queue_lock);
44 if (!(lkb->lkb_ast_type & (AST_COMP | AST_BAST))) {
45 kref_get(&lkb->lkb_ref);
46 list_add_tail(&lkb->lkb_astqueue, &ast_queue);
47 }
48 lkb->lkb_ast_type |= type;
David Teiglandfd22a512008-12-09 11:55:46 -060049 if (bastmode)
50 lkb->lkb_bastmode = bastmode;
David Teiglande7fd4172006-01-18 09:30:29 +000051 spin_unlock(&ast_queue_lock);
52
53 set_bit(WAKE_ASTS, &astd_wakeflags);
54 wake_up_process(astd_task);
55}
56
57static void process_asts(void)
58{
59 struct dlm_ls *ls = NULL;
60 struct dlm_rsb *r = NULL;
61 struct dlm_lkb *lkb;
David Teiglande5dae542008-02-06 00:35:45 -060062 void (*cast) (void *astparam);
63 void (*bast) (void *astparam, int mode);
Andrew Morton722d7422008-12-23 10:22:56 -060064 int type = 0, bastmode;
David Teiglande7fd4172006-01-18 09:30:29 +000065
Andrew Morton722d7422008-12-23 10:22:56 -060066repeat:
67 spin_lock(&ast_queue_lock);
68 list_for_each_entry(lkb, &ast_queue, lkb_astqueue) {
69 r = lkb->lkb_resource;
70 ls = r->res_ls;
David Teiglande7fd4172006-01-18 09:30:29 +000071
Andrew Morton722d7422008-12-23 10:22:56 -060072 if (dlm_locking_stopped(ls))
73 continue;
David Teiglande7fd4172006-01-18 09:30:29 +000074
Andrew Morton722d7422008-12-23 10:22:56 -060075 list_del(&lkb->lkb_astqueue);
76 type = lkb->lkb_ast_type;
77 lkb->lkb_ast_type = 0;
78 bastmode = lkb->lkb_bastmode;
79
David Teiglande7fd4172006-01-18 09:30:29 +000080 spin_unlock(&ast_queue_lock);
David Teiglande5dae542008-02-06 00:35:45 -060081 cast = lkb->lkb_astfn;
82 bast = lkb->lkb_bastfn;
David Teiglande7fd4172006-01-18 09:30:29 +000083
84 if ((type & AST_COMP) && cast)
85 cast(lkb->lkb_astparam);
86
David Teiglande7fd4172006-01-18 09:30:29 +000087 if ((type & AST_BAST) && bast)
David Teiglandfd22a512008-12-09 11:55:46 -060088 bast(lkb->lkb_astparam, bastmode);
David Teiglande7fd4172006-01-18 09:30:29 +000089
90 /* this removes the reference added by dlm_add_ast
91 and may result in the lkb being freed */
92 dlm_put_lkb(lkb);
93
Steven Whitehoused61e9aa2008-12-10 09:31:02 -060094 cond_resched();
Andrew Morton722d7422008-12-23 10:22:56 -060095 goto repeat;
David Teiglande7fd4172006-01-18 09:30:29 +000096 }
Andrew Morton722d7422008-12-23 10:22:56 -060097 spin_unlock(&ast_queue_lock);
David Teiglande7fd4172006-01-18 09:30:29 +000098}
99
100static inline int no_asts(void)
101{
102 int ret;
103
104 spin_lock(&ast_queue_lock);
105 ret = list_empty(&ast_queue);
106 spin_unlock(&ast_queue_lock);
107 return ret;
108}
109
110static int dlm_astd(void *data)
111{
112 while (!kthread_should_stop()) {
113 set_current_state(TASK_INTERRUPTIBLE);
114 if (!test_bit(WAKE_ASTS, &astd_wakeflags))
115 schedule();
116 set_current_state(TASK_RUNNING);
117
David Teigland90135922006-01-20 08:47:07 +0000118 mutex_lock(&astd_running);
David Teiglande7fd4172006-01-18 09:30:29 +0000119 if (test_and_clear_bit(WAKE_ASTS, &astd_wakeflags))
120 process_asts();
David Teigland90135922006-01-20 08:47:07 +0000121 mutex_unlock(&astd_running);
David Teiglande7fd4172006-01-18 09:30:29 +0000122 }
123 return 0;
124}
125
126void dlm_astd_wake(void)
127{
128 if (!no_asts()) {
129 set_bit(WAKE_ASTS, &astd_wakeflags);
130 wake_up_process(astd_task);
131 }
132}
133
134int dlm_astd_start(void)
135{
136 struct task_struct *p;
137 int error = 0;
138
139 INIT_LIST_HEAD(&ast_queue);
140 spin_lock_init(&ast_queue_lock);
David Teigland90135922006-01-20 08:47:07 +0000141 mutex_init(&astd_running);
David Teiglande7fd4172006-01-18 09:30:29 +0000142
143 p = kthread_run(dlm_astd, NULL, "dlm_astd");
144 if (IS_ERR(p))
145 error = PTR_ERR(p);
146 else
147 astd_task = p;
148 return error;
149}
150
151void dlm_astd_stop(void)
152{
153 kthread_stop(astd_task);
154}
155
156void dlm_astd_suspend(void)
157{
David Teigland90135922006-01-20 08:47:07 +0000158 mutex_lock(&astd_running);
David Teiglande7fd4172006-01-18 09:30:29 +0000159}
160
161void dlm_astd_resume(void)
162{
David Teigland90135922006-01-20 08:47:07 +0000163 mutex_unlock(&astd_running);
David Teiglande7fd4172006-01-18 09:30:29 +0000164}
165