]>
Commit | Line | Data |
---|---|---|
2522fe45 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
e7fd4179 DT |
2 | /****************************************************************************** |
3 | ******************************************************************************* | |
4 | ** | |
5 | ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. | |
7fe2b319 | 6 | ** Copyright (C) 2004-2010 Red Hat, Inc. All rights reserved. |
e7fd4179 | 7 | ** |
e7fd4179 DT |
8 | ** |
9 | ******************************************************************************* | |
10 | ******************************************************************************/ | |
11 | ||
f1d3b8f9 AA |
12 | #include <trace/events/dlm.h> |
13 | ||
e7fd4179 | 14 | #include "dlm_internal.h" |
ad191e0e | 15 | #include "lvb_table.h" |
61bed0ba | 16 | #include "memory.h" |
e7fd4179 | 17 | #include "lock.h" |
597d0cae | 18 | #include "user.h" |
95058571 | 19 | #include "ast.h" |
e7fd4179 | 20 | |
986ae3c2 | 21 | static void dlm_callback_work(struct work_struct *work) |
8304d6f2 | 22 | { |
986ae3c2 AA |
23 | struct dlm_callback *cb = container_of(work, struct dlm_callback, work); |
24 | ||
25 | if (cb->flags & DLM_CB_BAST) { | |
26 | trace_dlm_bast(cb->ls_id, cb->lkb_id, cb->mode, cb->res_name, | |
27 | cb->res_length); | |
28 | cb->bastfn(cb->astparam, cb->mode); | |
29 | } else if (cb->flags & DLM_CB_CAST) { | |
30 | trace_dlm_ast(cb->ls_id, cb->lkb_id, cb->sb_status, | |
31 | cb->sb_flags, cb->res_name, cb->res_length); | |
32 | cb->lkb_lksb->sb_status = cb->sb_status; | |
33 | cb->lkb_lksb->sb_flags = cb->sb_flags; | |
34 | cb->astfn(cb->astparam); | |
35 | } | |
36 | ||
2bec1bbd | 37 | dlm_free_cb(cb); |
986ae3c2 AA |
38 | } |
39 | ||
40 | int dlm_queue_lkb_callback(struct dlm_lkb *lkb, uint32_t flags, int mode, | |
41 | int status, uint32_t sbflags, | |
42 | struct dlm_callback **cb) | |
43 | { | |
44 | struct dlm_rsb *rsb = lkb->lkb_resource; | |
61bed0ba | 45 | int rv = DLM_ENQUEUE_CALLBACK_SUCCESS; |
986ae3c2 | 46 | struct dlm_ls *ls = rsb->res_ls; |
ad191e0e | 47 | int copy_lvb = 0; |
8304d6f2 | 48 | int prev_mode; |
8304d6f2 | 49 | |
61bed0ba AA |
50 | if (flags & DLM_CB_BAST) { |
51 | /* if cb is a bast, it should be skipped if the blocking mode is | |
52 | * compatible with the last granted mode | |
53 | */ | |
2bec1bbd AA |
54 | if (lkb->lkb_last_cast_cb_mode != -1) { |
55 | if (dlm_modes_compat(mode, lkb->lkb_last_cast_cb_mode)) { | |
61bed0ba AA |
56 | log_debug(ls, "skip %x bast mode %d for cast mode %d", |
57 | lkb->lkb_id, mode, | |
2bec1bbd | 58 | lkb->lkb_last_cast_cb_mode); |
61bed0ba AA |
59 | goto out; |
60 | } | |
61 | } | |
8304d6f2 DT |
62 | |
63 | /* | |
64 | * Suppress some redundant basts here, do more on removal. | |
65 | * Don't even add a bast if the callback just before it | |
66 | * is a bast for the same mode or a more restrictive mode. | |
67 | * (the addional > PR check is needed for PR/CW inversion) | |
68 | */ | |
2bec1bbd AA |
69 | if (lkb->lkb_last_cb_mode != -1 && |
70 | lkb->lkb_last_cb_flags & DLM_CB_BAST) { | |
71 | prev_mode = lkb->lkb_last_cb_mode; | |
8304d6f2 DT |
72 | |
73 | if ((prev_mode == mode) || | |
74 | (prev_mode > mode && prev_mode > DLM_LOCK_PR)) { | |
61bed0ba AA |
75 | log_debug(ls, "skip %x add bast mode %d for bast mode %d", |
76 | lkb->lkb_id, mode, prev_mode); | |
23e8e1aa | 77 | goto out; |
8304d6f2 DT |
78 | } |
79 | } | |
2bec1bbd AA |
80 | |
81 | lkb->lkb_last_bast_time = ktime_get(); | |
82 | lkb->lkb_last_bast_cb_mode = mode; | |
ad191e0e AA |
83 | } else if (flags & DLM_CB_CAST) { |
84 | if (test_bit(DLM_DFL_USER_BIT, &lkb->lkb_dflags)) { | |
2bec1bbd | 85 | prev_mode = lkb->lkb_last_cast_cb_mode; |
ad191e0e AA |
86 | |
87 | if (!status && lkb->lkb_lksb->sb_lvbptr && | |
88 | dlm_lvb_operations[prev_mode + 1][mode + 1]) | |
89 | copy_lvb = 1; | |
90 | } | |
2bec1bbd AA |
91 | |
92 | lkb->lkb_last_cast_cb_mode = mode; | |
93 | lkb->lkb_last_cast_time = ktime_get(); | |
8304d6f2 DT |
94 | } |
95 | ||
2bec1bbd AA |
96 | lkb->lkb_last_cb_mode = mode; |
97 | lkb->lkb_last_cb_flags = flags; | |
98 | ||
986ae3c2 AA |
99 | *cb = dlm_allocate_cb(); |
100 | if (!*cb) { | |
61bed0ba | 101 | rv = DLM_ENQUEUE_CALLBACK_FAILURE; |
23e8e1aa | 102 | goto out; |
8304d6f2 | 103 | } |
8304d6f2 | 104 | |
986ae3c2 AA |
105 | /* for tracing */ |
106 | (*cb)->lkb_id = lkb->lkb_id; | |
107 | (*cb)->ls_id = ls->ls_global_id; | |
108 | memcpy((*cb)->res_name, rsb->res_name, rsb->res_length); | |
109 | (*cb)->res_length = rsb->res_length; | |
a034c137 | 110 | |
986ae3c2 AA |
111 | (*cb)->flags = flags; |
112 | (*cb)->mode = mode; | |
113 | (*cb)->sb_status = status; | |
114 | (*cb)->sb_flags = (sbflags & 0x000000FF); | |
115 | (*cb)->copy_lvb = copy_lvb; | |
116 | (*cb)->lkb_lksb = lkb->lkb_lksb; | |
8304d6f2 | 117 | |
986ae3c2 | 118 | rv = DLM_ENQUEUE_CALLBACK_NEED_SCHED; |
8304d6f2 | 119 | |
986ae3c2 | 120 | out: |
23e8e1aa | 121 | return rv; |
8304d6f2 DT |
122 | } |
123 | ||
23e8e1aa | 124 | void dlm_add_cb(struct dlm_lkb *lkb, uint32_t flags, int mode, int status, |
986ae3c2 | 125 | uint32_t sbflags) |
8304d6f2 | 126 | { |
23e8e1aa | 127 | struct dlm_ls *ls = lkb->lkb_resource->res_ls; |
986ae3c2 | 128 | struct dlm_callback *cb; |
8304d6f2 DT |
129 | int rv; |
130 | ||
8a39dcd9 | 131 | if (test_bit(DLM_DFL_USER_BIT, &lkb->lkb_dflags)) { |
61bed0ba | 132 | dlm_user_add_ast(lkb, flags, mode, status, sbflags); |
597d0cae DT |
133 | return; |
134 | } | |
135 | ||
986ae3c2 AA |
136 | rv = dlm_queue_lkb_callback(lkb, flags, mode, status, sbflags, |
137 | &cb); | |
61bed0ba AA |
138 | switch (rv) { |
139 | case DLM_ENQUEUE_CALLBACK_NEED_SCHED: | |
986ae3c2 AA |
140 | cb->astfn = lkb->lkb_astfn; |
141 | cb->bastfn = lkb->lkb_bastfn; | |
142 | cb->astparam = lkb->lkb_astparam; | |
143 | INIT_WORK(&cb->work, dlm_callback_work); | |
e7fd4179 | 144 | |
578acf9a | 145 | spin_lock_bh(&ls->ls_cb_lock); |
986ae3c2 AA |
146 | if (test_bit(LSFL_CB_DELAY, &ls->ls_flags)) |
147 | list_add(&cb->list, &ls->ls_cb_delay); | |
148 | else | |
149 | queue_work(ls->ls_callback_wq, &cb->work); | |
578acf9a | 150 | spin_unlock_bh(&ls->ls_cb_lock); |
61bed0ba | 151 | break; |
61bed0ba AA |
152 | case DLM_ENQUEUE_CALLBACK_SUCCESS: |
153 | break; | |
0175e51b AA |
154 | case DLM_ENQUEUE_CALLBACK_FAILURE: |
155 | fallthrough; | |
61bed0ba | 156 | default: |
740bb8fc | 157 | WARN_ON_ONCE(1); |
61bed0ba | 158 | break; |
23e8e1aa | 159 | } |
e7fd4179 DT |
160 | } |
161 | ||
23e8e1aa | 162 | int dlm_callback_start(struct dlm_ls *ls) |
e7fd4179 | 163 | { |
986ae3c2 AA |
164 | ls->ls_callback_wq = alloc_ordered_workqueue("dlm_callback", |
165 | WQ_HIGHPRI | WQ_MEM_RECLAIM); | |
23e8e1aa DT |
166 | if (!ls->ls_callback_wq) { |
167 | log_print("can't start dlm_callback workqueue"); | |
168 | return -ENOMEM; | |
e7fd4179 DT |
169 | } |
170 | return 0; | |
171 | } | |
172 | ||
23e8e1aa | 173 | void dlm_callback_stop(struct dlm_ls *ls) |
e7fd4179 | 174 | { |
23e8e1aa DT |
175 | if (ls->ls_callback_wq) |
176 | destroy_workqueue(ls->ls_callback_wq); | |
e7fd4179 DT |
177 | } |
178 | ||
23e8e1aa | 179 | void dlm_callback_suspend(struct dlm_ls *ls) |
e7fd4179 | 180 | { |
9cb16d42 | 181 | if (ls->ls_callback_wq) { |
578acf9a | 182 | spin_lock_bh(&ls->ls_cb_lock); |
9cb16d42 | 183 | set_bit(LSFL_CB_DELAY, &ls->ls_flags); |
578acf9a | 184 | spin_unlock_bh(&ls->ls_cb_lock); |
e7fd4179 | 185 | |
23e8e1aa | 186 | flush_workqueue(ls->ls_callback_wq); |
9cb16d42 | 187 | } |
e7fd4179 DT |
188 | } |
189 | ||
216f0efd BP |
190 | #define MAX_CB_QUEUE 25 |
191 | ||
23e8e1aa | 192 | void dlm_callback_resume(struct dlm_ls *ls) |
e7fd4179 | 193 | { |
986ae3c2 | 194 | struct dlm_callback *cb, *safe; |
2f05ec43 | 195 | int count = 0, sum = 0; |
f70813d6 | 196 | bool empty; |
e7fd4179 | 197 | |
23e8e1aa DT |
198 | if (!ls->ls_callback_wq) |
199 | return; | |
200 | ||
216f0efd | 201 | more: |
578acf9a | 202 | spin_lock_bh(&ls->ls_cb_lock); |
986ae3c2 AA |
203 | list_for_each_entry_safe(cb, safe, &ls->ls_cb_delay, list) { |
204 | list_del(&cb->list); | |
205 | queue_work(ls->ls_callback_wq, &cb->work); | |
23e8e1aa | 206 | count++; |
216f0efd BP |
207 | if (count == MAX_CB_QUEUE) |
208 | break; | |
23e8e1aa | 209 | } |
f70813d6 | 210 | empty = list_empty(&ls->ls_cb_delay); |
85839f27 AA |
211 | if (empty) |
212 | clear_bit(LSFL_CB_DELAY, &ls->ls_flags); | |
578acf9a | 213 | spin_unlock_bh(&ls->ls_cb_lock); |
23e8e1aa | 214 | |
2f05ec43 | 215 | sum += count; |
f70813d6 | 216 | if (!empty) { |
216f0efd BP |
217 | count = 0; |
218 | cond_resched(); | |
219 | goto more; | |
220 | } | |
2f05ec43 AA |
221 | |
222 | if (sum) | |
223 | log_rinfo(ls, "%s %d", __func__, sum); | |
e7fd4179 DT |
224 | } |
225 |