recoverd.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /******************************************************************************
  2. *******************************************************************************
  3. **
  4. ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  5. ** Copyright (C) 2004-2005 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 "lockspace.h"
  15. #include "member.h"
  16. #include "dir.h"
  17. #include "ast.h"
  18. #include "recover.h"
  19. #include "lowcomms.h"
  20. #include "lock.h"
  21. #include "requestqueue.h"
  22. #include "recoverd.h"
  23. /* If the start for which we're re-enabling locking (seq) has been superseded
  24. by a newer stop (ls_recover_seq), we need to leave locking disabled. */
  25. static int enable_locking(struct dlm_ls *ls, uint64_t seq)
  26. {
  27. int error = -EINTR;
  28. spin_lock(&ls->ls_recover_lock);
  29. if (ls->ls_recover_seq == seq) {
  30. set_bit(LSFL_RUNNING, &ls->ls_flags);
  31. up_write(&ls->ls_in_recovery);
  32. error = 0;
  33. }
  34. spin_unlock(&ls->ls_recover_lock);
  35. return error;
  36. }
  37. static int ls_recover(struct dlm_ls *ls, struct dlm_recover *rv)
  38. {
  39. unsigned long start;
  40. int error, neg = 0;
  41. log_debug(ls, "recover %llx", rv->seq);
  42. mutex_lock(&ls->ls_recoverd_active);
  43. /*
  44. * Suspending and resuming dlm_astd ensures that no lkb's from this ls
  45. * will be processed by dlm_astd during recovery.
  46. */
  47. dlm_astd_suspend();
  48. dlm_astd_resume();
  49. /*
  50. * This list of root rsb's will be the basis of most of the recovery
  51. * routines.
  52. */
  53. dlm_create_root_list(ls);
  54. /*
  55. * Free all the tossed rsb's so we don't have to recover them.
  56. */
  57. dlm_clear_toss_list(ls);
  58. /*
  59. * Add or remove nodes from the lockspace's ls_nodes list.
  60. * Also waits for all nodes to complete dlm_recover_members.
  61. */
  62. error = dlm_recover_members(ls, rv, &neg);
  63. if (error) {
  64. log_error(ls, "recover_members failed %d", error);
  65. goto fail;
  66. }
  67. start = jiffies;
  68. /*
  69. * Rebuild our own share of the directory by collecting from all other
  70. * nodes their master rsb names that hash to us.
  71. */
  72. error = dlm_recover_directory(ls);
  73. if (error) {
  74. log_error(ls, "recover_directory failed %d", error);
  75. goto fail;
  76. }
  77. /*
  78. * Purge directory-related requests that are saved in requestqueue.
  79. * All dir requests from before recovery are invalid now due to the dir
  80. * rebuild and will be resent by the requesting nodes.
  81. */
  82. dlm_purge_requestqueue(ls);
  83. /*
  84. * Wait for all nodes to complete directory rebuild.
  85. */
  86. error = dlm_recover_directory_wait(ls);
  87. if (error) {
  88. log_error(ls, "recover_directory_wait failed %d", error);
  89. goto fail;
  90. }
  91. /*
  92. * We may have outstanding operations that are waiting for a reply from
  93. * a failed node. Mark these to be resent after recovery. Unlock and
  94. * cancel ops can just be completed.
  95. */
  96. dlm_recover_waiters_pre(ls);
  97. error = dlm_recovery_stopped(ls);
  98. if (error)
  99. goto fail;
  100. if (neg || dlm_no_directory(ls)) {
  101. /*
  102. * Clear lkb's for departed nodes.
  103. */
  104. dlm_purge_locks(ls);
  105. /*
  106. * Get new master nodeid's for rsb's that were mastered on
  107. * departed nodes.
  108. */
  109. error = dlm_recover_masters(ls);
  110. if (error) {
  111. log_error(ls, "recover_masters failed %d", error);
  112. goto fail;
  113. }
  114. /*
  115. * Send our locks on remastered rsb's to the new masters.
  116. */
  117. error = dlm_recover_locks(ls);
  118. if (error) {
  119. log_error(ls, "recover_locks failed %d", error);
  120. goto fail;
  121. }
  122. error = dlm_recover_locks_wait(ls);
  123. if (error) {
  124. log_error(ls, "recover_locks_wait failed %d", error);
  125. goto fail;
  126. }
  127. /*
  128. * Finalize state in master rsb's now that all locks can be
  129. * checked. This includes conversion resolution and lvb
  130. * settings.
  131. */
  132. dlm_recover_rsbs(ls);
  133. }
  134. dlm_release_root_list(ls);
  135. dlm_set_recover_status(ls, DLM_RS_DONE);
  136. error = dlm_recover_done_wait(ls);
  137. if (error) {
  138. log_error(ls, "recover_done_wait failed %d", error);
  139. goto fail;
  140. }
  141. dlm_clear_members_gone(ls);
  142. error = enable_locking(ls, rv->seq);
  143. if (error) {
  144. log_error(ls, "enable_locking failed %d", error);
  145. goto fail;
  146. }
  147. error = dlm_process_requestqueue(ls);
  148. if (error) {
  149. log_error(ls, "process_requestqueue failed %d", error);
  150. goto fail;
  151. }
  152. error = dlm_recover_waiters_post(ls);
  153. if (error) {
  154. log_error(ls, "recover_waiters_post failed %d", error);
  155. goto fail;
  156. }
  157. dlm_grant_after_purge(ls);
  158. dlm_astd_wake();
  159. log_debug(ls, "recover %llx done: %u ms", rv->seq,
  160. jiffies_to_msecs(jiffies - start));
  161. mutex_unlock(&ls->ls_recoverd_active);
  162. return 0;
  163. fail:
  164. dlm_release_root_list(ls);
  165. log_debug(ls, "recover %llx error %d", rv->seq, error);
  166. mutex_unlock(&ls->ls_recoverd_active);
  167. return error;
  168. }
  169. static void do_ls_recovery(struct dlm_ls *ls)
  170. {
  171. struct dlm_recover *rv = NULL;
  172. spin_lock(&ls->ls_recover_lock);
  173. rv = ls->ls_recover_args;
  174. ls->ls_recover_args = NULL;
  175. clear_bit(LSFL_RECOVERY_STOP, &ls->ls_flags);
  176. spin_unlock(&ls->ls_recover_lock);
  177. if (rv) {
  178. ls_recover(ls, rv);
  179. kfree(rv->nodeids);
  180. kfree(rv);
  181. }
  182. }
  183. static int dlm_recoverd(void *arg)
  184. {
  185. struct dlm_ls *ls;
  186. ls = dlm_find_lockspace_local(arg);
  187. if (!ls) {
  188. log_print("dlm_recoverd: no lockspace %p", arg);
  189. return -1;
  190. }
  191. while (!kthread_should_stop()) {
  192. set_current_state(TASK_INTERRUPTIBLE);
  193. if (!test_bit(LSFL_WORK, &ls->ls_flags))
  194. schedule();
  195. set_current_state(TASK_RUNNING);
  196. if (test_and_clear_bit(LSFL_WORK, &ls->ls_flags))
  197. do_ls_recovery(ls);
  198. }
  199. dlm_put_lockspace(ls);
  200. return 0;
  201. }
  202. void dlm_recoverd_kick(struct dlm_ls *ls)
  203. {
  204. set_bit(LSFL_WORK, &ls->ls_flags);
  205. wake_up_process(ls->ls_recoverd_task);
  206. }
  207. int dlm_recoverd_start(struct dlm_ls *ls)
  208. {
  209. struct task_struct *p;
  210. int error = 0;
  211. p = kthread_run(dlm_recoverd, ls, "dlm_recoverd");
  212. if (IS_ERR(p))
  213. error = PTR_ERR(p);
  214. else
  215. ls->ls_recoverd_task = p;
  216. return error;
  217. }
  218. void dlm_recoverd_stop(struct dlm_ls *ls)
  219. {
  220. kthread_stop(ls->ls_recoverd_task);
  221. }
  222. void dlm_recoverd_suspend(struct dlm_ls *ls)
  223. {
  224. wake_up(&ls->ls_wait_general);
  225. mutex_lock(&ls->ls_recoverd_active);
  226. }
  227. void dlm_recoverd_resume(struct dlm_ls *ls)
  228. {
  229. mutex_unlock(&ls->ls_recoverd_active);
  230. }