ast.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /******************************************************************************
  3. *******************************************************************************
  4. **
  5. ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  6. ** Copyright (C) 2004-2010 Red Hat, Inc. All rights reserved.
  7. **
  8. **
  9. *******************************************************************************
  10. ******************************************************************************/
  11. #include "dlm_internal.h"
  12. #include "lock.h"
  13. #include "user.h"
  14. #include "ast.h"
  15. static uint64_t dlm_cb_seq;
  16. static DEFINE_SPINLOCK(dlm_cb_seq_spin);
  17. static void dlm_dump_lkb_callbacks(struct dlm_lkb *lkb)
  18. {
  19. int i;
  20. log_print("last_bast %x %llu flags %x mode %d sb %d %x",
  21. lkb->lkb_id,
  22. (unsigned long long)lkb->lkb_last_bast.seq,
  23. lkb->lkb_last_bast.flags,
  24. lkb->lkb_last_bast.mode,
  25. lkb->lkb_last_bast.sb_status,
  26. lkb->lkb_last_bast.sb_flags);
  27. log_print("last_cast %x %llu flags %x mode %d sb %d %x",
  28. lkb->lkb_id,
  29. (unsigned long long)lkb->lkb_last_cast.seq,
  30. lkb->lkb_last_cast.flags,
  31. lkb->lkb_last_cast.mode,
  32. lkb->lkb_last_cast.sb_status,
  33. lkb->lkb_last_cast.sb_flags);
  34. for (i = 0; i < DLM_CALLBACKS_SIZE; i++) {
  35. log_print("cb %x %llu flags %x mode %d sb %d %x",
  36. lkb->lkb_id,
  37. (unsigned long long)lkb->lkb_callbacks[i].seq,
  38. lkb->lkb_callbacks[i].flags,
  39. lkb->lkb_callbacks[i].mode,
  40. lkb->lkb_callbacks[i].sb_status,
  41. lkb->lkb_callbacks[i].sb_flags);
  42. }
  43. }
  44. int dlm_add_lkb_callback(struct dlm_lkb *lkb, uint32_t flags, int mode,
  45. int status, uint32_t sbflags, uint64_t seq)
  46. {
  47. struct dlm_ls *ls = lkb->lkb_resource->res_ls;
  48. uint64_t prev_seq;
  49. int prev_mode;
  50. int i, rv;
  51. for (i = 0; i < DLM_CALLBACKS_SIZE; i++) {
  52. if (lkb->lkb_callbacks[i].seq)
  53. continue;
  54. /*
  55. * Suppress some redundant basts here, do more on removal.
  56. * Don't even add a bast if the callback just before it
  57. * is a bast for the same mode or a more restrictive mode.
  58. * (the addional > PR check is needed for PR/CW inversion)
  59. */
  60. if ((i > 0) && (flags & DLM_CB_BAST) &&
  61. (lkb->lkb_callbacks[i-1].flags & DLM_CB_BAST)) {
  62. prev_seq = lkb->lkb_callbacks[i-1].seq;
  63. prev_mode = lkb->lkb_callbacks[i-1].mode;
  64. if ((prev_mode == mode) ||
  65. (prev_mode > mode && prev_mode > DLM_LOCK_PR)) {
  66. log_debug(ls, "skip %x add bast %llu mode %d "
  67. "for bast %llu mode %d",
  68. lkb->lkb_id,
  69. (unsigned long long)seq,
  70. mode,
  71. (unsigned long long)prev_seq,
  72. prev_mode);
  73. rv = 0;
  74. goto out;
  75. }
  76. }
  77. lkb->lkb_callbacks[i].seq = seq;
  78. lkb->lkb_callbacks[i].flags = flags;
  79. lkb->lkb_callbacks[i].mode = mode;
  80. lkb->lkb_callbacks[i].sb_status = status;
  81. lkb->lkb_callbacks[i].sb_flags = (sbflags & 0x000000FF);
  82. rv = 0;
  83. break;
  84. }
  85. if (i == DLM_CALLBACKS_SIZE) {
  86. log_error(ls, "no callbacks %x %llu flags %x mode %d sb %d %x",
  87. lkb->lkb_id, (unsigned long long)seq,
  88. flags, mode, status, sbflags);
  89. dlm_dump_lkb_callbacks(lkb);
  90. rv = -1;
  91. goto out;
  92. }
  93. out:
  94. return rv;
  95. }
  96. int dlm_rem_lkb_callback(struct dlm_ls *ls, struct dlm_lkb *lkb,
  97. struct dlm_callback *cb, int *resid)
  98. {
  99. int i, rv;
  100. *resid = 0;
  101. if (!lkb->lkb_callbacks[0].seq) {
  102. rv = -ENOENT;
  103. goto out;
  104. }
  105. /* oldest undelivered cb is callbacks[0] */
  106. memcpy(cb, &lkb->lkb_callbacks[0], sizeof(struct dlm_callback));
  107. memset(&lkb->lkb_callbacks[0], 0, sizeof(struct dlm_callback));
  108. /* shift others down */
  109. for (i = 1; i < DLM_CALLBACKS_SIZE; i++) {
  110. if (!lkb->lkb_callbacks[i].seq)
  111. break;
  112. memcpy(&lkb->lkb_callbacks[i-1], &lkb->lkb_callbacks[i],
  113. sizeof(struct dlm_callback));
  114. memset(&lkb->lkb_callbacks[i], 0, sizeof(struct dlm_callback));
  115. (*resid)++;
  116. }
  117. /* if cb is a bast, it should be skipped if the blocking mode is
  118. compatible with the last granted mode */
  119. if ((cb->flags & DLM_CB_BAST) && lkb->lkb_last_cast.seq) {
  120. if (dlm_modes_compat(cb->mode, lkb->lkb_last_cast.mode)) {
  121. cb->flags |= DLM_CB_SKIP;
  122. log_debug(ls, "skip %x bast %llu mode %d "
  123. "for cast %llu mode %d",
  124. lkb->lkb_id,
  125. (unsigned long long)cb->seq,
  126. cb->mode,
  127. (unsigned long long)lkb->lkb_last_cast.seq,
  128. lkb->lkb_last_cast.mode);
  129. rv = 0;
  130. goto out;
  131. }
  132. }
  133. if (cb->flags & DLM_CB_CAST) {
  134. memcpy(&lkb->lkb_last_cast, cb, sizeof(struct dlm_callback));
  135. lkb->lkb_last_cast_time = ktime_get();
  136. }
  137. if (cb->flags & DLM_CB_BAST) {
  138. memcpy(&lkb->lkb_last_bast, cb, sizeof(struct dlm_callback));
  139. lkb->lkb_last_bast_time = ktime_get();
  140. }
  141. rv = 0;
  142. out:
  143. return rv;
  144. }
  145. void dlm_add_cb(struct dlm_lkb *lkb, uint32_t flags, int mode, int status,
  146. uint32_t sbflags)
  147. {
  148. struct dlm_ls *ls = lkb->lkb_resource->res_ls;
  149. uint64_t new_seq, prev_seq;
  150. int rv;
  151. spin_lock(&dlm_cb_seq_spin);
  152. new_seq = ++dlm_cb_seq;
  153. if (!dlm_cb_seq)
  154. new_seq = ++dlm_cb_seq;
  155. spin_unlock(&dlm_cb_seq_spin);
  156. if (lkb->lkb_flags & DLM_IFL_USER) {
  157. dlm_user_add_ast(lkb, flags, mode, status, sbflags, new_seq);
  158. return;
  159. }
  160. mutex_lock(&lkb->lkb_cb_mutex);
  161. prev_seq = lkb->lkb_callbacks[0].seq;
  162. rv = dlm_add_lkb_callback(lkb, flags, mode, status, sbflags, new_seq);
  163. if (rv < 0)
  164. goto out;
  165. if (!prev_seq) {
  166. kref_get(&lkb->lkb_ref);
  167. if (test_bit(LSFL_CB_DELAY, &ls->ls_flags)) {
  168. mutex_lock(&ls->ls_cb_mutex);
  169. list_add(&lkb->lkb_cb_list, &ls->ls_cb_delay);
  170. mutex_unlock(&ls->ls_cb_mutex);
  171. } else {
  172. queue_work(ls->ls_callback_wq, &lkb->lkb_cb_work);
  173. }
  174. }
  175. out:
  176. mutex_unlock(&lkb->lkb_cb_mutex);
  177. }
  178. void dlm_callback_work(struct work_struct *work)
  179. {
  180. struct dlm_lkb *lkb = container_of(work, struct dlm_lkb, lkb_cb_work);
  181. struct dlm_ls *ls = lkb->lkb_resource->res_ls;
  182. void (*castfn) (void *astparam);
  183. void (*bastfn) (void *astparam, int mode);
  184. struct dlm_callback callbacks[DLM_CALLBACKS_SIZE];
  185. int i, rv, resid;
  186. memset(&callbacks, 0, sizeof(callbacks));
  187. mutex_lock(&lkb->lkb_cb_mutex);
  188. if (!lkb->lkb_callbacks[0].seq) {
  189. /* no callback work exists, shouldn't happen */
  190. log_error(ls, "dlm_callback_work %x no work", lkb->lkb_id);
  191. dlm_print_lkb(lkb);
  192. dlm_dump_lkb_callbacks(lkb);
  193. }
  194. for (i = 0; i < DLM_CALLBACKS_SIZE; i++) {
  195. rv = dlm_rem_lkb_callback(ls, lkb, &callbacks[i], &resid);
  196. if (rv < 0)
  197. break;
  198. }
  199. if (resid) {
  200. /* cbs remain, loop should have removed all, shouldn't happen */
  201. log_error(ls, "dlm_callback_work %x resid %d", lkb->lkb_id,
  202. resid);
  203. dlm_print_lkb(lkb);
  204. dlm_dump_lkb_callbacks(lkb);
  205. }
  206. mutex_unlock(&lkb->lkb_cb_mutex);
  207. castfn = lkb->lkb_astfn;
  208. bastfn = lkb->lkb_bastfn;
  209. for (i = 0; i < DLM_CALLBACKS_SIZE; i++) {
  210. if (!callbacks[i].seq)
  211. break;
  212. if (callbacks[i].flags & DLM_CB_SKIP) {
  213. continue;
  214. } else if (callbacks[i].flags & DLM_CB_BAST) {
  215. bastfn(lkb->lkb_astparam, callbacks[i].mode);
  216. } else if (callbacks[i].flags & DLM_CB_CAST) {
  217. lkb->lkb_lksb->sb_status = callbacks[i].sb_status;
  218. lkb->lkb_lksb->sb_flags = callbacks[i].sb_flags;
  219. castfn(lkb->lkb_astparam);
  220. }
  221. }
  222. /* undo kref_get from dlm_add_callback, may cause lkb to be freed */
  223. dlm_put_lkb(lkb);
  224. }
  225. int dlm_callback_start(struct dlm_ls *ls)
  226. {
  227. ls->ls_callback_wq = alloc_workqueue("dlm_callback",
  228. WQ_HIGHPRI | WQ_MEM_RECLAIM, 0);
  229. if (!ls->ls_callback_wq) {
  230. log_print("can't start dlm_callback workqueue");
  231. return -ENOMEM;
  232. }
  233. return 0;
  234. }
  235. void dlm_callback_stop(struct dlm_ls *ls)
  236. {
  237. if (ls->ls_callback_wq)
  238. destroy_workqueue(ls->ls_callback_wq);
  239. }
  240. void dlm_callback_suspend(struct dlm_ls *ls)
  241. {
  242. set_bit(LSFL_CB_DELAY, &ls->ls_flags);
  243. if (ls->ls_callback_wq)
  244. flush_workqueue(ls->ls_callback_wq);
  245. }
  246. #define MAX_CB_QUEUE 25
  247. void dlm_callback_resume(struct dlm_ls *ls)
  248. {
  249. struct dlm_lkb *lkb, *safe;
  250. int count = 0;
  251. clear_bit(LSFL_CB_DELAY, &ls->ls_flags);
  252. if (!ls->ls_callback_wq)
  253. return;
  254. more:
  255. mutex_lock(&ls->ls_cb_mutex);
  256. list_for_each_entry_safe(lkb, safe, &ls->ls_cb_delay, lkb_cb_list) {
  257. list_del_init(&lkb->lkb_cb_list);
  258. queue_work(ls->ls_callback_wq, &lkb->lkb_cb_work);
  259. count++;
  260. if (count == MAX_CB_QUEUE)
  261. break;
  262. }
  263. mutex_unlock(&ls->ls_cb_mutex);
  264. if (count)
  265. log_rinfo(ls, "dlm_callback_resume %d", count);
  266. if (count == MAX_CB_QUEUE) {
  267. count = 0;
  268. cond_resched();
  269. goto more;
  270. }
  271. }