requestqueue.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /******************************************************************************
  2. *******************************************************************************
  3. **
  4. ** Copyright (C) 2005-2007 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. struct dlm_message request;
  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. void dlm_add_requestqueue(struct dlm_ls *ls, int nodeid, struct dlm_message *ms)
  30. {
  31. struct rq_entry *e;
  32. int length = ms->m_header.h_length - sizeof(struct dlm_message);
  33. e = kmalloc(sizeof(struct rq_entry) + length, GFP_NOFS);
  34. if (!e) {
  35. log_print("dlm_add_requestqueue: out of memory len %d", length);
  36. return;
  37. }
  38. e->nodeid = nodeid;
  39. memcpy(&e->request, ms, ms->m_header.h_length);
  40. mutex_lock(&ls->ls_requestqueue_mutex);
  41. list_add_tail(&e->list, &ls->ls_requestqueue);
  42. mutex_unlock(&ls->ls_requestqueue_mutex);
  43. }
  44. /*
  45. * Called by dlm_recoverd to process normal messages saved while recovery was
  46. * happening. Normal locking has been enabled before this is called. dlm_recv
  47. * upon receiving a message, will wait for all saved messages to be drained
  48. * here before processing the message it got. If a new dlm_ls_stop() arrives
  49. * while we're processing these saved messages, it may block trying to suspend
  50. * dlm_recv if dlm_recv is waiting for us in dlm_wait_requestqueue. In that
  51. * case, we don't abort since locking_stopped is still 0. If dlm_recv is not
  52. * waiting for us, then this processing may be aborted due to locking_stopped.
  53. */
  54. int dlm_process_requestqueue(struct dlm_ls *ls)
  55. {
  56. struct rq_entry *e;
  57. int error = 0;
  58. mutex_lock(&ls->ls_requestqueue_mutex);
  59. for (;;) {
  60. if (list_empty(&ls->ls_requestqueue)) {
  61. mutex_unlock(&ls->ls_requestqueue_mutex);
  62. error = 0;
  63. break;
  64. }
  65. e = list_entry(ls->ls_requestqueue.next, struct rq_entry, list);
  66. mutex_unlock(&ls->ls_requestqueue_mutex);
  67. dlm_receive_message_saved(ls, &e->request);
  68. mutex_lock(&ls->ls_requestqueue_mutex);
  69. list_del(&e->list);
  70. kfree(e);
  71. if (dlm_locking_stopped(ls)) {
  72. log_debug(ls, "process_requestqueue abort running");
  73. mutex_unlock(&ls->ls_requestqueue_mutex);
  74. error = -EINTR;
  75. break;
  76. }
  77. schedule();
  78. }
  79. return error;
  80. }
  81. /*
  82. * After recovery is done, locking is resumed and dlm_recoverd takes all the
  83. * saved requests and processes them as they would have been by dlm_recv. At
  84. * the same time, dlm_recv will start receiving new requests from remote nodes.
  85. * We want to delay dlm_recv processing new requests until dlm_recoverd has
  86. * finished processing the old saved requests. We don't check for locking
  87. * stopped here because dlm_ls_stop won't stop locking until it's suspended us
  88. * (dlm_recv).
  89. */
  90. void dlm_wait_requestqueue(struct dlm_ls *ls)
  91. {
  92. for (;;) {
  93. mutex_lock(&ls->ls_requestqueue_mutex);
  94. if (list_empty(&ls->ls_requestqueue))
  95. break;
  96. mutex_unlock(&ls->ls_requestqueue_mutex);
  97. schedule();
  98. }
  99. mutex_unlock(&ls->ls_requestqueue_mutex);
  100. }
  101. static int purge_request(struct dlm_ls *ls, struct dlm_message *ms, int nodeid)
  102. {
  103. uint32_t type = ms->m_type;
  104. /* the ls is being cleaned up and freed by release_lockspace */
  105. if (!ls->ls_count)
  106. return 1;
  107. if (dlm_is_removed(ls, nodeid))
  108. return 1;
  109. /* directory operations are always purged because the directory is
  110. always rebuilt during recovery and the lookups resent */
  111. if (type == DLM_MSG_REMOVE ||
  112. type == DLM_MSG_LOOKUP ||
  113. type == DLM_MSG_LOOKUP_REPLY)
  114. return 1;
  115. if (!dlm_no_directory(ls))
  116. return 0;
  117. /* with no directory, the master is likely to change as a part of
  118. recovery; requests to/from the defunct master need to be purged */
  119. switch (type) {
  120. case DLM_MSG_REQUEST:
  121. case DLM_MSG_CONVERT:
  122. case DLM_MSG_UNLOCK:
  123. case DLM_MSG_CANCEL:
  124. /* we're no longer the master of this resource, the sender
  125. will resend to the new master (see waiter_needs_recovery) */
  126. if (dlm_hash2nodeid(ls, ms->m_hash) != dlm_our_nodeid())
  127. return 1;
  128. break;
  129. case DLM_MSG_REQUEST_REPLY:
  130. case DLM_MSG_CONVERT_REPLY:
  131. case DLM_MSG_UNLOCK_REPLY:
  132. case DLM_MSG_CANCEL_REPLY:
  133. case DLM_MSG_GRANT:
  134. /* this reply is from the former master of the resource,
  135. we'll resend to the new master if needed */
  136. if (dlm_hash2nodeid(ls, ms->m_hash) != nodeid)
  137. return 1;
  138. break;
  139. }
  140. return 0;
  141. }
  142. void dlm_purge_requestqueue(struct dlm_ls *ls)
  143. {
  144. struct dlm_message *ms;
  145. struct rq_entry *e, *safe;
  146. mutex_lock(&ls->ls_requestqueue_mutex);
  147. list_for_each_entry_safe(e, safe, &ls->ls_requestqueue, list) {
  148. ms = &e->request;
  149. if (purge_request(ls, ms, e->nodeid)) {
  150. list_del(&e->list);
  151. kfree(e);
  152. }
  153. }
  154. mutex_unlock(&ls->ls_requestqueue_mutex);
  155. }