blob: 09b167df790e2f4d5c9d706a99a7a4c50515bbcb [file] [log] [blame]
David Teiglande7fd4172006-01-18 09:30:29 +00001/******************************************************************************
2*******************************************************************************
3**
4** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
5** Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
6**
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
36void dlm_add_ast(struct dlm_lkb *lkb, int type)
37{
David Teigland597d0ca2006-07-12 16:44:04 -050038 if (lkb->lkb_flags & DLM_IFL_USER) {
39 dlm_user_add_ast(lkb, type);
40 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;
49 spin_unlock(&ast_queue_lock);
50
51 set_bit(WAKE_ASTS, &astd_wakeflags);
52 wake_up_process(astd_task);
53}
54
55static void process_asts(void)
56{
57 struct dlm_ls *ls = NULL;
58 struct dlm_rsb *r = NULL;
59 struct dlm_lkb *lkb;
David Teiglande5dae542008-02-06 00:35:45 -060060 void (*cast) (void *astparam);
61 void (*bast) (void *astparam, int mode);
David Teiglande7fd4172006-01-18 09:30:29 +000062 int type = 0, found, bmode;
63
64 for (;;) {
David Teigland90135922006-01-20 08:47:07 +000065 found = 0;
David Teiglande7fd4172006-01-18 09:30:29 +000066 spin_lock(&ast_queue_lock);
67 list_for_each_entry(lkb, &ast_queue, lkb_astqueue) {
68 r = lkb->lkb_resource;
69 ls = r->res_ls;
70
71 if (dlm_locking_stopped(ls))
72 continue;
73
74 list_del(&lkb->lkb_astqueue);
75 type = lkb->lkb_ast_type;
76 lkb->lkb_ast_type = 0;
David Teigland90135922006-01-20 08:47:07 +000077 found = 1;
David Teiglande7fd4172006-01-18 09:30:29 +000078 break;
79 }
80 spin_unlock(&ast_queue_lock);
81
82 if (!found)
83 break;
84
David Teiglande5dae542008-02-06 00:35:45 -060085 cast = lkb->lkb_astfn;
86 bast = lkb->lkb_bastfn;
David Teiglande7fd4172006-01-18 09:30:29 +000087 bmode = lkb->lkb_bastmode;
88
89 if ((type & AST_COMP) && cast)
90 cast(lkb->lkb_astparam);
91
David Teiglande7fd4172006-01-18 09:30:29 +000092 if ((type & AST_BAST) && bast)
David Teigland03339692008-12-08 17:14:10 -060093 bast(lkb->lkb_astparam, bmode);
David Teiglande7fd4172006-01-18 09:30:29 +000094
95 /* this removes the reference added by dlm_add_ast
96 and may result in the lkb being freed */
97 dlm_put_lkb(lkb);
98
Steven Whitehoused61e9aa2008-12-10 09:31:02 -060099 cond_resched();
David Teiglande7fd4172006-01-18 09:30:29 +0000100 }
101}
102
103static inline int no_asts(void)
104{
105 int ret;
106
107 spin_lock(&ast_queue_lock);
108 ret = list_empty(&ast_queue);
109 spin_unlock(&ast_queue_lock);
110 return ret;
111}
112
113static int dlm_astd(void *data)
114{
115 while (!kthread_should_stop()) {
116 set_current_state(TASK_INTERRUPTIBLE);
117 if (!test_bit(WAKE_ASTS, &astd_wakeflags))
118 schedule();
119 set_current_state(TASK_RUNNING);
120
David Teigland90135922006-01-20 08:47:07 +0000121 mutex_lock(&astd_running);
David Teiglande7fd4172006-01-18 09:30:29 +0000122 if (test_and_clear_bit(WAKE_ASTS, &astd_wakeflags))
123 process_asts();
David Teigland90135922006-01-20 08:47:07 +0000124 mutex_unlock(&astd_running);
David Teiglande7fd4172006-01-18 09:30:29 +0000125 }
126 return 0;
127}
128
129void dlm_astd_wake(void)
130{
131 if (!no_asts()) {
132 set_bit(WAKE_ASTS, &astd_wakeflags);
133 wake_up_process(astd_task);
134 }
135}
136
137int dlm_astd_start(void)
138{
139 struct task_struct *p;
140 int error = 0;
141
142 INIT_LIST_HEAD(&ast_queue);
143 spin_lock_init(&ast_queue_lock);
David Teigland90135922006-01-20 08:47:07 +0000144 mutex_init(&astd_running);
David Teiglande7fd4172006-01-18 09:30:29 +0000145
146 p = kthread_run(dlm_astd, NULL, "dlm_astd");
147 if (IS_ERR(p))
148 error = PTR_ERR(p);
149 else
150 astd_task = p;
151 return error;
152}
153
154void dlm_astd_stop(void)
155{
156 kthread_stop(astd_task);
157}
158
159void dlm_astd_suspend(void)
160{
David Teigland90135922006-01-20 08:47:07 +0000161 mutex_lock(&astd_running);
David Teiglande7fd4172006-01-18 09:30:29 +0000162}
163
164void dlm_astd_resume(void)
165{
David Teigland90135922006-01-20 08:47:07 +0000166 mutex_unlock(&astd_running);
David Teiglande7fd4172006-01-18 09:30:29 +0000167}
168