ast.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. #define WAKE_ASTS 0
  18. static uint64_t ast_seq_count;
  19. static struct list_head ast_queue;
  20. static spinlock_t ast_queue_lock;
  21. static struct task_struct * astd_task;
  22. static unsigned long astd_wakeflags;
  23. static struct mutex astd_running;
  24. static void dlm_dump_lkb_callbacks(struct dlm_lkb *lkb)
  25. {
  26. int i;
  27. log_print("last_bast %x %llu flags %x mode %d sb %d %x",
  28. lkb->lkb_id,
  29. (unsigned long long)lkb->lkb_last_bast.seq,
  30. lkb->lkb_last_bast.flags,
  31. lkb->lkb_last_bast.mode,
  32. lkb->lkb_last_bast.sb_status,
  33. lkb->lkb_last_bast.sb_flags);
  34. log_print("last_cast %x %llu flags %x mode %d sb %d %x",
  35. lkb->lkb_id,
  36. (unsigned long long)lkb->lkb_last_cast.seq,
  37. lkb->lkb_last_cast.flags,
  38. lkb->lkb_last_cast.mode,
  39. lkb->lkb_last_cast.sb_status,
  40. lkb->lkb_last_cast.sb_flags);
  41. for (i = 0; i < DLM_CALLBACKS_SIZE; i++) {
  42. log_print("cb %x %llu flags %x mode %d sb %d %x",
  43. lkb->lkb_id,
  44. (unsigned long long)lkb->lkb_callbacks[i].seq,
  45. lkb->lkb_callbacks[i].flags,
  46. lkb->lkb_callbacks[i].mode,
  47. lkb->lkb_callbacks[i].sb_status,
  48. lkb->lkb_callbacks[i].sb_flags);
  49. }
  50. }
  51. void dlm_del_ast(struct dlm_lkb *lkb)
  52. {
  53. spin_lock(&ast_queue_lock);
  54. if (!list_empty(&lkb->lkb_astqueue))
  55. list_del_init(&lkb->lkb_astqueue);
  56. spin_unlock(&ast_queue_lock);
  57. }
  58. int dlm_add_lkb_callback(struct dlm_lkb *lkb, uint32_t flags, int mode,
  59. int status, uint32_t sbflags, uint64_t seq)
  60. {
  61. struct dlm_ls *ls = lkb->lkb_resource->res_ls;
  62. uint64_t prev_seq;
  63. int prev_mode;
  64. int i;
  65. for (i = 0; i < DLM_CALLBACKS_SIZE; i++) {
  66. if (lkb->lkb_callbacks[i].seq)
  67. continue;
  68. /*
  69. * Suppress some redundant basts here, do more on removal.
  70. * Don't even add a bast if the callback just before it
  71. * is a bast for the same mode or a more restrictive mode.
  72. * (the addional > PR check is needed for PR/CW inversion)
  73. */
  74. if ((i > 0) && (flags & DLM_CB_BAST) &&
  75. (lkb->lkb_callbacks[i-1].flags & DLM_CB_BAST)) {
  76. prev_seq = lkb->lkb_callbacks[i-1].seq;
  77. prev_mode = lkb->lkb_callbacks[i-1].mode;
  78. if ((prev_mode == mode) ||
  79. (prev_mode > mode && prev_mode > DLM_LOCK_PR)) {
  80. log_debug(ls, "skip %x add bast %llu mode %d "
  81. "for bast %llu mode %d",
  82. lkb->lkb_id,
  83. (unsigned long long)seq,
  84. mode,
  85. (unsigned long long)prev_seq,
  86. prev_mode);
  87. return 0;
  88. }
  89. }
  90. lkb->lkb_callbacks[i].seq = seq;
  91. lkb->lkb_callbacks[i].flags = flags;
  92. lkb->lkb_callbacks[i].mode = mode;
  93. lkb->lkb_callbacks[i].sb_status = status;
  94. lkb->lkb_callbacks[i].sb_flags = (sbflags & 0x000000FF);
  95. break;
  96. }
  97. if (i == DLM_CALLBACKS_SIZE) {
  98. log_error(ls, "no callbacks %x %llu flags %x mode %d sb %d %x",
  99. lkb->lkb_id, (unsigned long long)seq,
  100. flags, mode, status, sbflags);
  101. dlm_dump_lkb_callbacks(lkb);
  102. return -1;
  103. }
  104. return 0;
  105. }
  106. int dlm_rem_lkb_callback(struct dlm_ls *ls, struct dlm_lkb *lkb,
  107. struct dlm_callback *cb, int *resid)
  108. {
  109. int i;
  110. *resid = 0;
  111. if (!lkb->lkb_callbacks[0].seq)
  112. return -ENOENT;
  113. /* oldest undelivered cb is callbacks[0] */
  114. memcpy(cb, &lkb->lkb_callbacks[0], sizeof(struct dlm_callback));
  115. memset(&lkb->lkb_callbacks[0], 0, sizeof(struct dlm_callback));
  116. /* shift others down */
  117. for (i = 1; i < DLM_CALLBACKS_SIZE; i++) {
  118. if (!lkb->lkb_callbacks[i].seq)
  119. break;
  120. memcpy(&lkb->lkb_callbacks[i-1], &lkb->lkb_callbacks[i],
  121. sizeof(struct dlm_callback));
  122. memset(&lkb->lkb_callbacks[i], 0, sizeof(struct dlm_callback));
  123. (*resid)++;
  124. }
  125. /* if cb is a bast, it should be skipped if the blocking mode is
  126. compatible with the last granted mode */
  127. if ((cb->flags & DLM_CB_BAST) && lkb->lkb_last_cast.seq) {
  128. if (dlm_modes_compat(cb->mode, lkb->lkb_last_cast.mode)) {
  129. cb->flags |= DLM_CB_SKIP;
  130. log_debug(ls, "skip %x bast %llu mode %d "
  131. "for cast %llu mode %d",
  132. lkb->lkb_id,
  133. (unsigned long long)cb->seq,
  134. cb->mode,
  135. (unsigned long long)lkb->lkb_last_cast.seq,
  136. lkb->lkb_last_cast.mode);
  137. return 0;
  138. }
  139. }
  140. if (cb->flags & DLM_CB_CAST) {
  141. memcpy(&lkb->lkb_last_cast, cb, sizeof(struct dlm_callback));
  142. lkb->lkb_last_cast_time = ktime_get();
  143. }
  144. if (cb->flags & DLM_CB_BAST) {
  145. memcpy(&lkb->lkb_last_bast, cb, sizeof(struct dlm_callback));
  146. lkb->lkb_last_bast_time = ktime_get();
  147. }
  148. return 0;
  149. }
  150. void dlm_add_ast(struct dlm_lkb *lkb, uint32_t flags, int mode, int status,
  151. uint32_t sbflags)
  152. {
  153. uint64_t seq;
  154. int rv;
  155. spin_lock(&ast_queue_lock);
  156. seq = ++ast_seq_count;
  157. if (lkb->lkb_flags & DLM_IFL_USER) {
  158. spin_unlock(&ast_queue_lock);
  159. dlm_user_add_ast(lkb, flags, mode, status, sbflags, seq);
  160. return;
  161. }
  162. rv = dlm_add_lkb_callback(lkb, flags, mode, status, sbflags, seq);
  163. if (rv < 0) {
  164. spin_unlock(&ast_queue_lock);
  165. return;
  166. }
  167. if (list_empty(&lkb->lkb_astqueue)) {
  168. kref_get(&lkb->lkb_ref);
  169. list_add_tail(&lkb->lkb_astqueue, &ast_queue);
  170. }
  171. spin_unlock(&ast_queue_lock);
  172. set_bit(WAKE_ASTS, &astd_wakeflags);
  173. wake_up_process(astd_task);
  174. }
  175. static void process_asts(void)
  176. {
  177. struct dlm_ls *ls = NULL;
  178. struct dlm_rsb *r = NULL;
  179. struct dlm_lkb *lkb;
  180. void (*castfn) (void *astparam);
  181. void (*bastfn) (void *astparam, int mode);
  182. struct dlm_callback callbacks[DLM_CALLBACKS_SIZE];
  183. int i, rv, resid;
  184. repeat:
  185. spin_lock(&ast_queue_lock);
  186. list_for_each_entry(lkb, &ast_queue, lkb_astqueue) {
  187. r = lkb->lkb_resource;
  188. ls = r->res_ls;
  189. if (dlm_locking_stopped(ls))
  190. continue;
  191. /* we remove from astqueue list and remove everything in
  192. lkb_callbacks before releasing the spinlock so empty
  193. lkb_astqueue is always consistent with empty lkb_callbacks */
  194. list_del_init(&lkb->lkb_astqueue);
  195. castfn = lkb->lkb_astfn;
  196. bastfn = lkb->lkb_bastfn;
  197. memset(&callbacks, 0, sizeof(callbacks));
  198. for (i = 0; i < DLM_CALLBACKS_SIZE; i++) {
  199. rv = dlm_rem_lkb_callback(ls, lkb, &callbacks[i], &resid);
  200. if (rv < 0)
  201. break;
  202. }
  203. spin_unlock(&ast_queue_lock);
  204. if (resid) {
  205. /* shouldn't happen, for loop should have removed all */
  206. log_error(ls, "callback resid %d lkb %x",
  207. resid, lkb->lkb_id);
  208. }
  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. /* removes ref for ast_queue, may cause lkb to be freed */
  223. dlm_put_lkb(lkb);
  224. cond_resched();
  225. goto repeat;
  226. }
  227. spin_unlock(&ast_queue_lock);
  228. }
  229. static inline int no_asts(void)
  230. {
  231. int ret;
  232. spin_lock(&ast_queue_lock);
  233. ret = list_empty(&ast_queue);
  234. spin_unlock(&ast_queue_lock);
  235. return ret;
  236. }
  237. static int dlm_astd(void *data)
  238. {
  239. while (!kthread_should_stop()) {
  240. set_current_state(TASK_INTERRUPTIBLE);
  241. if (!test_bit(WAKE_ASTS, &astd_wakeflags))
  242. schedule();
  243. set_current_state(TASK_RUNNING);
  244. mutex_lock(&astd_running);
  245. if (test_and_clear_bit(WAKE_ASTS, &astd_wakeflags))
  246. process_asts();
  247. mutex_unlock(&astd_running);
  248. }
  249. return 0;
  250. }
  251. void dlm_astd_wake(void)
  252. {
  253. if (!no_asts()) {
  254. set_bit(WAKE_ASTS, &astd_wakeflags);
  255. wake_up_process(astd_task);
  256. }
  257. }
  258. int dlm_astd_start(void)
  259. {
  260. struct task_struct *p;
  261. int error = 0;
  262. INIT_LIST_HEAD(&ast_queue);
  263. spin_lock_init(&ast_queue_lock);
  264. mutex_init(&astd_running);
  265. p = kthread_run(dlm_astd, NULL, "dlm_astd");
  266. if (IS_ERR(p))
  267. error = PTR_ERR(p);
  268. else
  269. astd_task = p;
  270. return error;
  271. }
  272. void dlm_astd_stop(void)
  273. {
  274. kthread_stop(astd_task);
  275. }
  276. void dlm_astd_suspend(void)
  277. {
  278. mutex_lock(&astd_running);
  279. }
  280. void dlm_astd_resume(void)
  281. {
  282. mutex_unlock(&astd_running);
  283. }