ast.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /******************************************************************************
  2. *******************************************************************************
  3. **
  4. ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  5. ** Copyright (C) 2004-2010 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. #include "dlm_internal.h"
  14. #include "lock.h"
  15. #include "user.h"
  16. #include "ast.h"
  17. static uint64_t dlm_cb_seq;
  18. static DEFINE_SPINLOCK(dlm_cb_seq_spin);
  19. static void dlm_dump_lkb_callbacks(struct dlm_lkb *lkb)
  20. {
  21. int i;
  22. log_print("last_bast %x %llu flags %x mode %d sb %d %x",
  23. lkb->lkb_id,
  24. (unsigned long long)lkb->lkb_last_bast.seq,
  25. lkb->lkb_last_bast.flags,
  26. lkb->lkb_last_bast.mode,
  27. lkb->lkb_last_bast.sb_status,
  28. lkb->lkb_last_bast.sb_flags);
  29. log_print("last_cast %x %llu flags %x mode %d sb %d %x",
  30. lkb->lkb_id,
  31. (unsigned long long)lkb->lkb_last_cast.seq,
  32. lkb->lkb_last_cast.flags,
  33. lkb->lkb_last_cast.mode,
  34. lkb->lkb_last_cast.sb_status,
  35. lkb->lkb_last_cast.sb_flags);
  36. for (i = 0; i < DLM_CALLBACKS_SIZE; i++) {
  37. log_print("cb %x %llu flags %x mode %d sb %d %x",
  38. lkb->lkb_id,
  39. (unsigned long long)lkb->lkb_callbacks[i].seq,
  40. lkb->lkb_callbacks[i].flags,
  41. lkb->lkb_callbacks[i].mode,
  42. lkb->lkb_callbacks[i].sb_status,
  43. lkb->lkb_callbacks[i].sb_flags);
  44. }
  45. }
  46. int dlm_add_lkb_callback(struct dlm_lkb *lkb, uint32_t flags, int mode,
  47. int status, uint32_t sbflags, uint64_t seq)
  48. {
  49. struct dlm_ls *ls = lkb->lkb_resource->res_ls;
  50. uint64_t prev_seq;
  51. int prev_mode;
  52. int i, rv;
  53. for (i = 0; i < DLM_CALLBACKS_SIZE; i++) {
  54. if (lkb->lkb_callbacks[i].seq)
  55. continue;
  56. /*
  57. * Suppress some redundant basts here, do more on removal.
  58. * Don't even add a bast if the callback just before it
  59. * is a bast for the same mode or a more restrictive mode.
  60. * (the addional > PR check is needed for PR/CW inversion)
  61. */
  62. if ((i > 0) && (flags & DLM_CB_BAST) &&
  63. (lkb->lkb_callbacks[i-1].flags & DLM_CB_BAST)) {
  64. prev_seq = lkb->lkb_callbacks[i-1].seq;
  65. prev_mode = lkb->lkb_callbacks[i-1].mode;
  66. if ((prev_mode == mode) ||
  67. (prev_mode > mode && prev_mode > DLM_LOCK_PR)) {
  68. log_debug(ls, "skip %x add bast %llu mode %d "
  69. "for bast %llu mode %d",
  70. lkb->lkb_id,
  71. (unsigned long long)seq,
  72. mode,
  73. (unsigned long long)prev_seq,
  74. prev_mode);
  75. rv = 0;
  76. goto out;
  77. }
  78. }
  79. lkb->lkb_callbacks[i].seq = seq;
  80. lkb->lkb_callbacks[i].flags = flags;
  81. lkb->lkb_callbacks[i].mode = mode;
  82. lkb->lkb_callbacks[i].sb_status = status;
  83. lkb->lkb_callbacks[i].sb_flags = (sbflags & 0x000000FF);
  84. rv = 0;
  85. break;
  86. }
  87. if (i == DLM_CALLBACKS_SIZE) {
  88. log_error(ls, "no callbacks %x %llu flags %x mode %d sb %d %x",
  89. lkb->lkb_id, (unsigned long long)seq,
  90. flags, mode, status, sbflags);
  91. dlm_dump_lkb_callbacks(lkb);
  92. rv = -1;
  93. goto out;
  94. }
  95. out:
  96. return rv;
  97. }
  98. int dlm_rem_lkb_callback(struct dlm_ls *ls, struct dlm_lkb *lkb,
  99. struct dlm_callback *cb, int *resid)
  100. {
  101. int i, rv;
  102. *resid = 0;
  103. if (!lkb->lkb_callbacks[0].seq) {
  104. rv = -ENOENT;
  105. goto out;
  106. }
  107. /* oldest undelivered cb is callbacks[0] */
  108. memcpy(cb, &lkb->lkb_callbacks[0], sizeof(struct dlm_callback));
  109. memset(&lkb->lkb_callbacks[0], 0, sizeof(struct dlm_callback));
  110. /* shift others down */
  111. for (i = 1; i < DLM_CALLBACKS_SIZE; i++) {
  112. if (!lkb->lkb_callbacks[i].seq)
  113. break;
  114. memcpy(&lkb->lkb_callbacks[i-1], &lkb->lkb_callbacks[i],
  115. sizeof(struct dlm_callback));
  116. memset(&lkb->lkb_callbacks[i], 0, sizeof(struct dlm_callback));
  117. (*resid)++;
  118. }
  119. /* if cb is a bast, it should be skipped if the blocking mode is
  120. compatible with the last granted mode */
  121. if ((cb->flags & DLM_CB_BAST) && lkb->lkb_last_cast.seq) {
  122. if (dlm_modes_compat(cb->mode, lkb->lkb_last_cast.mode)) {
  123. cb->flags |= DLM_CB_SKIP;
  124. log_debug(ls, "skip %x bast %llu mode %d "
  125. "for cast %llu mode %d",
  126. lkb->lkb_id,
  127. (unsigned long long)cb->seq,
  128. cb->mode,
  129. (unsigned long long)lkb->lkb_last_cast.seq,
  130. lkb->lkb_last_cast.mode);
  131. rv = 0;
  132. goto out;
  133. }
  134. }
  135. if (cb->flags & DLM_CB_CAST) {
  136. memcpy(&lkb->lkb_last_cast, cb, sizeof(struct dlm_callback));
  137. lkb->lkb_last_cast_time = ktime_get();
  138. }
  139. if (cb->flags & DLM_CB_BAST) {
  140. memcpy(&lkb->lkb_last_bast, cb, sizeof(struct dlm_callback));
  141. lkb->lkb_last_bast_time = ktime_get();
  142. }
  143. rv = 0;
  144. out:
  145. return rv;
  146. }
  147. void dlm_add_cb(struct dlm_lkb *lkb, uint32_t flags, int mode, int status,
  148. uint32_t sbflags)
  149. {
  150. struct dlm_ls *ls = lkb->lkb_resource->res_ls;
  151. uint64_t new_seq, prev_seq;
  152. int rv;
  153. spin_lock(&dlm_cb_seq_spin);
  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_UNBOUND | 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. void dlm_callback_resume(struct dlm_ls *ls)
  247. {
  248. struct dlm_lkb *lkb, *safe;
  249. int count = 0;
  250. clear_bit(LSFL_CB_DELAY, &ls->ls_flags);
  251. if (!ls->ls_callback_wq)
  252. return;
  253. mutex_lock(&ls->ls_cb_mutex);
  254. list_for_each_entry_safe(lkb, safe, &ls->ls_cb_delay, lkb_cb_list) {
  255. list_del_init(&lkb->lkb_cb_list);
  256. queue_work(ls->ls_callback_wq, &lkb->lkb_cb_work);
  257. count++;
  258. }
  259. mutex_unlock(&ls->ls_cb_mutex);
  260. if (count)
  261. log_rinfo(ls, "dlm_callback_resume %d", count);
  262. }