1 // SPDX-License-Identifier: GPL-2.0-only
2 /******************************************************************************
3 *******************************************************************************
5 ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
6 ** Copyright (C) 2004-2010 Red Hat, Inc. All rights reserved.
9 *******************************************************************************
10 ******************************************************************************/
12 #include <trace/events/dlm.h>
14 #include "dlm_internal.h"
20 void dlm_release_callback(struct kref *ref)
22 struct dlm_callback *cb = container_of(ref, struct dlm_callback, ref);
27 void dlm_callback_set_last_ptr(struct dlm_callback **from,
28 struct dlm_callback *to)
31 kref_put(&(*from)->ref, dlm_release_callback);
39 void dlm_purge_lkb_callbacks(struct dlm_lkb *lkb)
41 struct dlm_callback *cb, *safe;
43 list_for_each_entry_safe(cb, safe, &lkb->lkb_callbacks, list) {
45 kref_put(&cb->ref, dlm_release_callback);
48 clear_bit(DLM_IFL_CB_PENDING_BIT, &lkb->lkb_iflags);
51 dlm_callback_set_last_ptr(&lkb->lkb_last_cast, NULL);
52 dlm_callback_set_last_ptr(&lkb->lkb_last_cb, NULL);
53 lkb->lkb_last_bast_mode = -1;
56 int dlm_enqueue_lkb_callback(struct dlm_lkb *lkb, uint32_t flags, int mode,
57 int status, uint32_t sbflags)
59 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
60 int rv = DLM_ENQUEUE_CALLBACK_SUCCESS;
61 struct dlm_callback *cb;
64 if (flags & DLM_CB_BAST) {
65 /* if cb is a bast, it should be skipped if the blocking mode is
66 * compatible with the last granted mode
68 if (lkb->lkb_last_cast) {
69 if (dlm_modes_compat(mode, lkb->lkb_last_cast->mode)) {
70 log_debug(ls, "skip %x bast mode %d for cast mode %d",
72 lkb->lkb_last_cast->mode);
78 * Suppress some redundant basts here, do more on removal.
79 * Don't even add a bast if the callback just before it
80 * is a bast for the same mode or a more restrictive mode.
81 * (the addional > PR check is needed for PR/CW inversion)
83 if (lkb->lkb_last_cb && lkb->lkb_last_cb->flags & DLM_CB_BAST) {
84 prev_mode = lkb->lkb_last_cb->mode;
86 if ((prev_mode == mode) ||
87 (prev_mode > mode && prev_mode > DLM_LOCK_PR)) {
88 log_debug(ls, "skip %x add bast mode %d for bast mode %d",
89 lkb->lkb_id, mode, prev_mode);
95 cb = dlm_allocate_cb();
97 rv = DLM_ENQUEUE_CALLBACK_FAILURE;
103 cb->sb_status = status;
104 cb->sb_flags = (sbflags & 0x000000FF);
106 if (!test_and_set_bit(DLM_IFL_CB_PENDING_BIT, &lkb->lkb_iflags))
107 rv = DLM_ENQUEUE_CALLBACK_NEED_SCHED;
109 list_add_tail(&cb->list, &lkb->lkb_callbacks);
111 if (flags & DLM_CB_CAST)
112 dlm_callback_set_last_ptr(&lkb->lkb_last_cast, cb);
114 dlm_callback_set_last_ptr(&lkb->lkb_last_cb, cb);
120 int dlm_dequeue_lkb_callback(struct dlm_lkb *lkb, struct dlm_callback **cb)
122 /* oldest undelivered cb is callbacks first entry */
123 *cb = list_first_entry_or_null(&lkb->lkb_callbacks,
124 struct dlm_callback, list);
126 return DLM_DEQUEUE_CALLBACK_EMPTY;
128 /* remove it from callbacks so shift others down */
129 list_del(&(*cb)->list);
130 if (list_empty(&lkb->lkb_callbacks))
131 return DLM_DEQUEUE_CALLBACK_LAST;
133 return DLM_DEQUEUE_CALLBACK_SUCCESS;
136 void dlm_add_cb(struct dlm_lkb *lkb, uint32_t flags, int mode, int status,
139 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
142 if (test_bit(DLM_DFL_USER_BIT, &lkb->lkb_dflags)) {
143 dlm_user_add_ast(lkb, flags, mode, status, sbflags);
147 spin_lock(&lkb->lkb_cb_lock);
148 rv = dlm_enqueue_lkb_callback(lkb, flags, mode, status, sbflags);
150 case DLM_ENQUEUE_CALLBACK_NEED_SCHED:
151 kref_get(&lkb->lkb_ref);
153 spin_lock(&ls->ls_cb_lock);
154 if (test_bit(LSFL_CB_DELAY, &ls->ls_flags)) {
155 list_add(&lkb->lkb_cb_list, &ls->ls_cb_delay);
157 queue_work(ls->ls_callback_wq, &lkb->lkb_cb_work);
159 spin_unlock(&ls->ls_cb_lock);
161 case DLM_ENQUEUE_CALLBACK_FAILURE:
164 case DLM_ENQUEUE_CALLBACK_SUCCESS:
170 spin_unlock(&lkb->lkb_cb_lock);
173 void dlm_callback_work(struct work_struct *work)
175 struct dlm_lkb *lkb = container_of(work, struct dlm_lkb, lkb_cb_work);
176 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
177 void (*castfn) (void *astparam);
178 void (*bastfn) (void *astparam, int mode);
179 struct dlm_callback *cb;
182 spin_lock(&lkb->lkb_cb_lock);
183 rv = dlm_dequeue_lkb_callback(lkb, &cb);
184 spin_unlock(&lkb->lkb_cb_lock);
186 if (WARN_ON_ONCE(rv == DLM_DEQUEUE_CALLBACK_EMPTY))
190 castfn = lkb->lkb_astfn;
191 bastfn = lkb->lkb_bastfn;
193 if (cb->flags & DLM_CB_BAST) {
194 trace_dlm_bast(ls, lkb, cb->mode);
195 lkb->lkb_last_bast_time = ktime_get();
196 lkb->lkb_last_bast_mode = cb->mode;
197 bastfn(lkb->lkb_astparam, cb->mode);
198 } else if (cb->flags & DLM_CB_CAST) {
199 lkb->lkb_lksb->sb_status = cb->sb_status;
200 lkb->lkb_lksb->sb_flags = cb->sb_flags;
201 trace_dlm_ast(ls, lkb);
202 lkb->lkb_last_cast_time = ktime_get();
203 castfn(lkb->lkb_astparam);
206 kref_put(&cb->ref, dlm_release_callback);
208 spin_lock(&lkb->lkb_cb_lock);
209 rv = dlm_dequeue_lkb_callback(lkb, &cb);
210 if (rv == DLM_DEQUEUE_CALLBACK_EMPTY) {
211 clear_bit(DLM_IFL_CB_PENDING_BIT, &lkb->lkb_iflags);
212 spin_unlock(&lkb->lkb_cb_lock);
215 spin_unlock(&lkb->lkb_cb_lock);
219 /* undo kref_get from dlm_add_callback, may cause lkb to be freed */
223 int dlm_callback_start(struct dlm_ls *ls)
225 ls->ls_callback_wq = alloc_workqueue("dlm_callback",
226 WQ_HIGHPRI | WQ_MEM_RECLAIM, 0);
227 if (!ls->ls_callback_wq) {
228 log_print("can't start dlm_callback workqueue");
234 void dlm_callback_stop(struct dlm_ls *ls)
236 if (ls->ls_callback_wq)
237 destroy_workqueue(ls->ls_callback_wq);
240 void dlm_callback_suspend(struct dlm_ls *ls)
242 if (ls->ls_callback_wq) {
243 spin_lock(&ls->ls_cb_lock);
244 set_bit(LSFL_CB_DELAY, &ls->ls_flags);
245 spin_unlock(&ls->ls_cb_lock);
247 flush_workqueue(ls->ls_callback_wq);
251 #define MAX_CB_QUEUE 25
253 void dlm_callback_resume(struct dlm_ls *ls)
255 struct dlm_lkb *lkb, *safe;
256 int count = 0, sum = 0;
259 if (!ls->ls_callback_wq)
263 spin_lock(&ls->ls_cb_lock);
264 list_for_each_entry_safe(lkb, safe, &ls->ls_cb_delay, lkb_cb_list) {
265 list_del_init(&lkb->lkb_cb_list);
266 queue_work(ls->ls_callback_wq, &lkb->lkb_cb_work);
268 if (count == MAX_CB_QUEUE)
271 empty = list_empty(&ls->ls_cb_delay);
273 clear_bit(LSFL_CB_DELAY, &ls->ls_flags);
274 spin_unlock(&ls->ls_cb_lock);
284 log_rinfo(ls, "%s %d", __func__, sum);