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"
15 #include "lvb_table.h"
21 static void dlm_callback_work(struct work_struct *work)
23 struct dlm_callback *cb = container_of(work, struct dlm_callback, work);
25 if (cb->flags & DLM_CB_BAST) {
26 trace_dlm_bast(cb->ls_id, cb->lkb_id, cb->mode, cb->res_name,
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);
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)
44 struct dlm_rsb *rsb = lkb->lkb_resource;
45 int rv = DLM_ENQUEUE_CALLBACK_SUCCESS;
46 struct dlm_ls *ls = rsb->res_ls;
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
54 if (lkb->lkb_last_cast_cb_mode != -1) {
55 if (dlm_modes_compat(mode, lkb->lkb_last_cast_cb_mode)) {
56 log_debug(ls, "skip %x bast mode %d for cast mode %d",
58 lkb->lkb_last_cast_cb_mode);
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)
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;
73 if ((prev_mode == mode) ||
74 (prev_mode > mode && prev_mode > DLM_LOCK_PR)) {
75 log_debug(ls, "skip %x add bast mode %d for bast mode %d",
76 lkb->lkb_id, mode, prev_mode);
81 lkb->lkb_last_bast_time = ktime_get();
82 lkb->lkb_last_bast_cb_mode = mode;
83 } else if (flags & DLM_CB_CAST) {
84 if (test_bit(DLM_DFL_USER_BIT, &lkb->lkb_dflags)) {
85 prev_mode = lkb->lkb_last_cast_cb_mode;
87 if (!status && lkb->lkb_lksb->sb_lvbptr &&
88 dlm_lvb_operations[prev_mode + 1][mode + 1])
92 lkb->lkb_last_cast_cb_mode = mode;
93 lkb->lkb_last_cast_time = ktime_get();
96 lkb->lkb_last_cb_mode = mode;
97 lkb->lkb_last_cb_flags = flags;
99 *cb = dlm_allocate_cb();
101 rv = DLM_ENQUEUE_CALLBACK_FAILURE;
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;
111 (*cb)->flags = flags;
113 (*cb)->sb_status = status;
114 (*cb)->sb_flags = (sbflags & 0x000000FF);
115 (*cb)->copy_lvb = copy_lvb;
116 (*cb)->lkb_lksb = lkb->lkb_lksb;
118 rv = DLM_ENQUEUE_CALLBACK_NEED_SCHED;
124 void dlm_add_cb(struct dlm_lkb *lkb, uint32_t flags, int mode, int status,
127 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
128 struct dlm_callback *cb;
131 if (test_bit(DLM_DFL_USER_BIT, &lkb->lkb_dflags)) {
132 dlm_user_add_ast(lkb, flags, mode, status, sbflags);
136 rv = dlm_queue_lkb_callback(lkb, flags, mode, status, sbflags,
139 case DLM_ENQUEUE_CALLBACK_NEED_SCHED:
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);
145 spin_lock_bh(&ls->ls_cb_lock);
146 if (test_bit(LSFL_CB_DELAY, &ls->ls_flags))
147 list_add(&cb->list, &ls->ls_cb_delay);
149 queue_work(ls->ls_callback_wq, &cb->work);
150 spin_unlock_bh(&ls->ls_cb_lock);
152 case DLM_ENQUEUE_CALLBACK_SUCCESS:
154 case DLM_ENQUEUE_CALLBACK_FAILURE:
162 int dlm_callback_start(struct dlm_ls *ls)
164 ls->ls_callback_wq = alloc_ordered_workqueue("dlm_callback",
165 WQ_HIGHPRI | WQ_MEM_RECLAIM);
166 if (!ls->ls_callback_wq) {
167 log_print("can't start dlm_callback workqueue");
173 void dlm_callback_stop(struct dlm_ls *ls)
175 if (ls->ls_callback_wq)
176 destroy_workqueue(ls->ls_callback_wq);
179 void dlm_callback_suspend(struct dlm_ls *ls)
181 if (ls->ls_callback_wq) {
182 spin_lock_bh(&ls->ls_cb_lock);
183 set_bit(LSFL_CB_DELAY, &ls->ls_flags);
184 spin_unlock_bh(&ls->ls_cb_lock);
186 flush_workqueue(ls->ls_callback_wq);
190 #define MAX_CB_QUEUE 25
192 void dlm_callback_resume(struct dlm_ls *ls)
194 struct dlm_callback *cb, *safe;
195 int count = 0, sum = 0;
198 if (!ls->ls_callback_wq)
202 spin_lock_bh(&ls->ls_cb_lock);
203 list_for_each_entry_safe(cb, safe, &ls->ls_cb_delay, list) {
205 queue_work(ls->ls_callback_wq, &cb->work);
207 if (count == MAX_CB_QUEUE)
210 empty = list_empty(&ls->ls_cb_delay);
212 clear_bit(LSFL_CB_DELAY, &ls->ls_flags);
213 spin_unlock_bh(&ls->ls_cb_lock);
223 log_rinfo(ls, "%s %d", __func__, sum);