requestqueue.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /******************************************************************************
  2. *******************************************************************************
  3. **
  4. ** Copyright (C) 2005 Red Hat, Inc. All rights reserved.
  5. **
  6. ** This copyrighted material is made available to anyone wishing to use,
  7. ** modify, copy, or redistribute it subject to the terms and conditions
  8. ** of the GNU General Public License v.2.
  9. **
  10. *******************************************************************************
  11. ******************************************************************************/
  12. #include "dlm_internal.h"
  13. #include "member.h"
  14. #include "lock.h"
  15. #include "dir.h"
  16. #include "config.h"
  17. #include "requestqueue.h"
  18. struct rq_entry {
  19. struct list_head list;
  20. int nodeid;
  21. char request[1];
  22. };
  23. /*
  24. * Requests received while the lockspace is in recovery get added to the
  25. * request queue and processed when recovery is complete. This happens when
  26. * the lockspace is suspended on some nodes before it is on others, or the
  27. * lockspace is enabled on some while still suspended on others.
  28. */
  29. int dlm_add_requestqueue(struct dlm_ls *ls, int nodeid, struct dlm_header *hd)
  30. {
  31. struct rq_entry *e;
  32. int length = hd->h_length;
  33. int rv = 0;
  34. e = kmalloc(sizeof(struct rq_entry) + length, GFP_KERNEL);
  35. if (!e) {
  36. log_print("dlm_add_requestqueue: out of memory\n");
  37. return 0;
  38. }
  39. e->nodeid = nodeid;
  40. memcpy(e->request, hd, length);
  41. /* We need to check dlm_locking_stopped() after taking the mutex to
  42. avoid a race where dlm_recoverd enables locking and runs
  43. process_requestqueue between our earlier dlm_locking_stopped check
  44. and this addition to the requestqueue. */
  45. mutex_lock(&ls->ls_requestqueue_mutex);
  46. if (dlm_locking_stopped(ls))
  47. list_add_tail(&e->list, &ls->ls_requestqueue);
  48. else {
  49. log_debug(ls, "dlm_add_requestqueue skip from %d", nodeid);
  50. kfree(e);
  51. rv = -EAGAIN;
  52. }
  53. mutex_unlock(&ls->ls_requestqueue_mutex);
  54. return rv;
  55. }
  56. int dlm_process_requestqueue(struct dlm_ls *ls)
  57. {
  58. struct rq_entry *e;
  59. struct dlm_header *hd;
  60. int error = 0;
  61. mutex_lock(&ls->ls_requestqueue_mutex);
  62. for (;;) {
  63. if (list_empty(&ls->ls_requestqueue)) {
  64. mutex_unlock(&ls->ls_requestqueue_mutex);
  65. error = 0;
  66. break;
  67. }
  68. e = list_entry(ls->ls_requestqueue.next, struct rq_entry, list);
  69. mutex_unlock(&ls->ls_requestqueue_mutex);
  70. hd = (struct dlm_header *) e->request;
  71. error = dlm_receive_message(hd, e->nodeid, 1);
  72. if (error == -EINTR) {
  73. /* entry is left on requestqueue */
  74. log_debug(ls, "process_requestqueue abort eintr");
  75. break;
  76. }
  77. mutex_lock(&ls->ls_requestqueue_mutex);
  78. list_del(&e->list);
  79. kfree(e);
  80. if (dlm_locking_stopped(ls)) {
  81. log_debug(ls, "process_requestqueue abort running");
  82. mutex_unlock(&ls->ls_requestqueue_mutex);
  83. error = -EINTR;
  84. break;
  85. }
  86. schedule();
  87. }
  88. return error;
  89. }
  90. /*
  91. * After recovery is done, locking is resumed and dlm_recoverd takes all the
  92. * saved requests and processes them as they would have been by dlm_recvd. At
  93. * the same time, dlm_recvd will start receiving new requests from remote
  94. * nodes. We want to delay dlm_recvd processing new requests until
  95. * dlm_recoverd has finished processing the old saved requests.
  96. */
  97. void dlm_wait_requestqueue(struct dlm_ls *ls)
  98. {
  99. for (;;) {
  100. mutex_lock(&ls->ls_requestqueue_mutex);
  101. if (list_empty(&ls->ls_requestqueue))
  102. break;
  103. if (dlm_locking_stopped(ls))
  104. break;
  105. mutex_unlock(&ls->ls_requestqueue_mutex);
  106. schedule();
  107. }
  108. mutex_unlock(&ls->ls_requestqueue_mutex);
  109. }
  110. static int purge_request(struct dlm_ls *ls, struct dlm_message *ms, int nodeid)
  111. {
  112. uint32_t type = ms->m_type;
  113. /* the ls is being cleaned up and freed by release_lockspace */
  114. if (!ls->ls_count)
  115. return 1;
  116. if (dlm_is_removed(ls, nodeid))
  117. return 1;
  118. /* directory operations are always purged because the directory is
  119. always rebuilt during recovery and the lookups resent */
  120. if (type == DLM_MSG_REMOVE ||
  121. type == DLM_MSG_LOOKUP ||
  122. type == DLM_MSG_LOOKUP_REPLY)
  123. return 1;
  124. if (!dlm_no_directory(ls))
  125. return 0;
  126. /* with no directory, the master is likely to change as a part of
  127. recovery; requests to/from the defunct master need to be purged */
  128. switch (type) {
  129. case DLM_MSG_REQUEST:
  130. case DLM_MSG_CONVERT:
  131. case DLM_MSG_UNLOCK:
  132. case DLM_MSG_CANCEL:
  133. /* we're no longer the master of this resource, the sender
  134. will resend to the new master (see waiter_needs_recovery) */
  135. if (dlm_hash2nodeid(ls, ms->m_hash) != dlm_our_nodeid())
  136. return 1;
  137. break;
  138. case DLM_MSG_REQUEST_REPLY:
  139. case DLM_MSG_CONVERT_REPLY:
  140. case DLM_MSG_UNLOCK_REPLY:
  141. case DLM_MSG_CANCEL_REPLY:
  142. case DLM_MSG_GRANT:
  143. /* this reply is from the former master of the resource,
  144. we'll resend to the new master if needed */
  145. if (dlm_hash2nodeid(ls, ms->m_hash) != nodeid)
  146. return 1;
  147. break;
  148. }
  149. return 0;
  150. }
  151. void dlm_purge_requestqueue(struct dlm_ls *ls)
  152. {
  153. struct dlm_message *ms;
  154. struct rq_entry *e, *safe;
  155. mutex_lock(&ls->ls_requestqueue_mutex);
  156. list_for_each_entry_safe(e, safe, &ls->ls_requestqueue, list) {
  157. ms = (struct dlm_message *) e->request;
  158. if (purge_request(ls, ms, e->nodeid)) {
  159. list_del(&e->list);
  160. kfree(e);
  161. }
  162. }
  163. mutex_unlock(&ls->ls_requestqueue_mutex);
  164. }