ast.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. if (!dlm_cb_seq)
  156. new_seq = ++dlm_cb_seq;
  157. spin_unlock(&dlm_cb_seq_spin);
  158. if (lkb->lkb_flags & DLM_IFL_USER) {
  159. dlm_user_add_ast(lkb, flags, mode, status, sbflags, new_seq);
  160. return;
  161. }
  162. mutex_lock(&lkb->lkb_cb_mutex);
  163. prev_seq = lkb->lkb_callbacks[0].seq;
  164. rv = dlm_add_lkb_callback(lkb, flags, mode, status, sbflags, new_seq);
  165. if (rv < 0)
  166. goto out;
  167. if (!prev_seq) {
  168. kref_get(&lkb->lkb_ref);
  169. if (test_bit(LSFL_CB_DELAY, &ls->ls_flags)) {
  170. mutex_lock(&ls->ls_cb_mutex);
  171. list_add(&lkb->lkb_cb_list, &ls->ls_cb_delay);
  172. mutex_unlock(&ls->ls_cb_mutex);
  173. } else {
  174. queue_work(ls->ls_callback_wq, &lkb->lkb_cb_work);
  175. }
  176. }
  177. out:
  178. mutex_unlock(&lkb->lkb_cb_mutex);
  179. }
  180. void dlm_callback_work(struct work_struct *work)
  181. {
  182. struct dlm_lkb *lkb = container_of(work, struct dlm_lkb, lkb_cb_work);
  183. struct dlm_ls *ls = lkb->lkb_resource->res_ls;
  184. void (*castfn) (void *astparam);
  185. void (*bastfn) (void *astparam, int mode);
  186. struct dlm_callback callbacks[DLM_CALLBACKS_SIZE];
  187. int i, rv, resid;
  188. memset(&callbacks, 0, sizeof(callbacks));
  189. mutex_lock(&lkb->lkb_cb_mutex);
  190. if (!lkb->lkb_callbacks[0].seq) {
  191. /* no callback work exists, shouldn't happen */
  192. log_error(ls, "dlm_callback_work %x no work", lkb->lkb_id);
  193. dlm_print_lkb(lkb);
  194. dlm_dump_lkb_callbacks(lkb);
  195. }
  196. for (i = 0; i < DLM_CALLBACKS_SIZE; i++) {
  197. rv = dlm_rem_lkb_callback(ls, lkb, &callbacks[i], &resid);
  198. if (rv < 0)
  199. break;
  200. }
  201. if (resid) {
  202. /* cbs remain, loop should have removed all, shouldn't happen */
  203. log_error(ls, "dlm_callback_work %x resid %d", lkb->lkb_id,
  204. resid);
  205. dlm_print_lkb(lkb);
  206. dlm_dump_lkb_callbacks(lkb);
  207. }
  208. mutex_unlock(&lkb->lkb_cb_mutex);
  209. castfn = lkb->lkb_astfn;
  210. bastfn = lkb->lkb_bastfn;
  211. for (i = 0; i < DLM_CALLBACKS_SIZE; i++) {
  212. if (!callbacks[i].seq)
  213. break;
  214. if (callbacks[i].flags & DLM_CB_SKIP) {
  215. continue;
  216. } else if (callbacks[i].flags & DLM_CB_BAST) {
  217. bastfn(lkb->lkb_astparam, callbacks[i].mode);
  218. } else if (callbacks[i].flags & DLM_CB_CAST) {
  219. lkb->lkb_lksb->sb_status = callbacks[i].sb_status;
  220. lkb->lkb_lksb->sb_flags = callbacks[i].sb_flags;
  221. castfn(lkb->lkb_astparam);
  222. }
  223. }
  224. /* undo kref_get from dlm_add_callback, may cause lkb to be freed */
  225. dlm_put_lkb(lkb);
  226. }
  227. int dlm_callback_start(struct dlm_ls *ls)
  228. {
  229. ls->ls_callback_wq = alloc_workqueue("dlm_callback",
  230. WQ_HIGHPRI | WQ_MEM_RECLAIM, 0);
  231. if (!ls->ls_callback_wq) {
  232. log_print("can't start dlm_callback workqueue");
  233. return -ENOMEM;
  234. }
  235. return 0;
  236. }
  237. void dlm_callback_stop(struct dlm_ls *ls)
  238. {
  239. if (ls->ls_callback_wq)
  240. destroy_workqueue(ls->ls_callback_wq);
  241. }
  242. void dlm_callback_suspend(struct dlm_ls *ls)
  243. {
  244. set_bit(LSFL_CB_DELAY, &ls->ls_flags);
  245. if (ls->ls_callback_wq)
  246. flush_workqueue(ls->ls_callback_wq);
  247. }
  248. #define MAX_CB_QUEUE 25
  249. void dlm_callback_resume(struct dlm_ls *ls)
  250. {
  251. struct dlm_lkb *lkb, *safe;
  252. int count = 0;
  253. clear_bit(LSFL_CB_DELAY, &ls->ls_flags);
  254. if (!ls->ls_callback_wq)
  255. return;
  256. more:
  257. mutex_lock(&ls->ls_cb_mutex);
  258. list_for_each_entry_safe(lkb, safe, &ls->ls_cb_delay, lkb_cb_list) {
  259. list_del_init(&lkb->lkb_cb_list);
  260. queue_work(ls->ls_callback_wq, &lkb->lkb_cb_work);
  261. count++;
  262. if (count == MAX_CB_QUEUE)
  263. break;
  264. }
  265. mutex_unlock(&ls->ls_cb_mutex);
  266. if (count)
  267. log_rinfo(ls, "dlm_callback_resume %d", count);
  268. if (count == MAX_CB_QUEUE) {
  269. count = 0;
  270. cond_resched();
  271. goto more;
  272. }
  273. }