recoverd.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /******************************************************************************
  2. *******************************************************************************
  3. **
  4. ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  5. ** Copyright (C) 2004-2007 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. We suspend dlm_recv threads here to avoid the race where dlm_recv a) sees
  26. locking stopped and b) adds a message to the requestqueue, but dlm_recoverd
  27. enables locking and clears the requestqueue between a and b. */
  28. static int enable_locking(struct dlm_ls *ls, uint64_t seq)
  29. {
  30. int error = -EINTR;
  31. down_write(&ls->ls_recv_active);
  32. spin_lock(&ls->ls_recover_lock);
  33. if (ls->ls_recover_seq == seq) {
  34. set_bit(LSFL_RUNNING, &ls->ls_flags);
  35. /* unblocks processes waiting to enter the dlm */
  36. up_write(&ls->ls_in_recovery);
  37. error = 0;
  38. }
  39. spin_unlock(&ls->ls_recover_lock);
  40. up_write(&ls->ls_recv_active);
  41. return error;
  42. }
  43. static int ls_recover(struct dlm_ls *ls, struct dlm_recover *rv)
  44. {
  45. unsigned long start;
  46. int error, neg = 0;
  47. log_debug(ls, "recover %llx", (unsigned long long)rv->seq);
  48. mutex_lock(&ls->ls_recoverd_active);
  49. /*
  50. * Suspending and resuming dlm_astd ensures that no lkb's from this ls
  51. * will be processed by dlm_astd during recovery.
  52. */
  53. dlm_astd_suspend();
  54. dlm_astd_resume();
  55. /*
  56. * This list of root rsb's will be the basis of most of the recovery
  57. * routines.
  58. */
  59. dlm_create_root_list(ls);
  60. /*
  61. * Free all the tossed rsb's so we don't have to recover them.
  62. */
  63. dlm_clear_toss_list(ls);
  64. /*
  65. * Add or remove nodes from the lockspace's ls_nodes list.
  66. * Also waits for all nodes to complete dlm_recover_members.
  67. */
  68. error = dlm_recover_members(ls, rv, &neg);
  69. if (error) {
  70. log_debug(ls, "recover_members failed %d", error);
  71. goto fail;
  72. }
  73. start = jiffies;
  74. /*
  75. * Rebuild our own share of the directory by collecting from all other
  76. * nodes their master rsb names that hash to us.
  77. */
  78. error = dlm_recover_directory(ls);
  79. if (error) {
  80. log_debug(ls, "recover_directory failed %d", error);
  81. goto fail;
  82. }
  83. /*
  84. * Wait for all nodes to complete directory rebuild.
  85. */
  86. error = dlm_recover_directory_wait(ls);
  87. if (error) {
  88. log_debug(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_debug(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_debug(ls, "recover_locks failed %d", error);
  120. goto fail;
  121. }
  122. error = dlm_recover_locks_wait(ls);
  123. if (error) {
  124. log_debug(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. } else {
  134. /*
  135. * Other lockspace members may be going through the "neg" steps
  136. * while also adding us to the lockspace, in which case they'll
  137. * be doing the recover_locks (RS_LOCKS) barrier.
  138. */
  139. dlm_set_recover_status(ls, DLM_RS_LOCKS);
  140. error = dlm_recover_locks_wait(ls);
  141. if (error) {
  142. log_debug(ls, "recover_locks_wait failed %d", error);
  143. goto fail;
  144. }
  145. }
  146. dlm_release_root_list(ls);
  147. /*
  148. * Purge directory-related requests that are saved in requestqueue.
  149. * All dir requests from before recovery are invalid now due to the dir
  150. * rebuild and will be resent by the requesting nodes.
  151. */
  152. dlm_purge_requestqueue(ls);
  153. dlm_set_recover_status(ls, DLM_RS_DONE);
  154. error = dlm_recover_done_wait(ls);
  155. if (error) {
  156. log_debug(ls, "recover_done_wait failed %d", error);
  157. goto fail;
  158. }
  159. dlm_clear_members_gone(ls);
  160. dlm_adjust_timeouts(ls);
  161. error = enable_locking(ls, rv->seq);
  162. if (error) {
  163. log_debug(ls, "enable_locking failed %d", error);
  164. goto fail;
  165. }
  166. error = dlm_process_requestqueue(ls);
  167. if (error) {
  168. log_debug(ls, "process_requestqueue failed %d", error);
  169. goto fail;
  170. }
  171. error = dlm_recover_waiters_post(ls);
  172. if (error) {
  173. log_debug(ls, "recover_waiters_post failed %d", error);
  174. goto fail;
  175. }
  176. dlm_grant_after_purge(ls);
  177. dlm_astd_wake();
  178. log_debug(ls, "recover %llx done: %u ms",
  179. (unsigned long long)rv->seq,
  180. jiffies_to_msecs(jiffies - start));
  181. mutex_unlock(&ls->ls_recoverd_active);
  182. return 0;
  183. fail:
  184. dlm_release_root_list(ls);
  185. log_debug(ls, "recover %llx error %d",
  186. (unsigned long long)rv->seq, error);
  187. mutex_unlock(&ls->ls_recoverd_active);
  188. return error;
  189. }
  190. /* The dlm_ls_start() that created the rv we take here may already have been
  191. stopped via dlm_ls_stop(); in that case we need to leave the RECOVERY_STOP
  192. flag set. */
  193. static void do_ls_recovery(struct dlm_ls *ls)
  194. {
  195. struct dlm_recover *rv = NULL;
  196. spin_lock(&ls->ls_recover_lock);
  197. rv = ls->ls_recover_args;
  198. ls->ls_recover_args = NULL;
  199. if (rv && ls->ls_recover_seq == rv->seq)
  200. clear_bit(LSFL_RECOVERY_STOP, &ls->ls_flags);
  201. spin_unlock(&ls->ls_recover_lock);
  202. if (rv) {
  203. ls_recover(ls, rv);
  204. kfree(rv->nodeids);
  205. kfree(rv);
  206. }
  207. }
  208. static int dlm_recoverd(void *arg)
  209. {
  210. struct dlm_ls *ls;
  211. ls = dlm_find_lockspace_local(arg);
  212. if (!ls) {
  213. log_print("dlm_recoverd: no lockspace %p", arg);
  214. return -1;
  215. }
  216. while (!kthread_should_stop()) {
  217. set_current_state(TASK_INTERRUPTIBLE);
  218. if (!test_bit(LSFL_WORK, &ls->ls_flags))
  219. schedule();
  220. set_current_state(TASK_RUNNING);
  221. if (test_and_clear_bit(LSFL_WORK, &ls->ls_flags))
  222. do_ls_recovery(ls);
  223. }
  224. dlm_put_lockspace(ls);
  225. return 0;
  226. }
  227. void dlm_recoverd_kick(struct dlm_ls *ls)
  228. {
  229. set_bit(LSFL_WORK, &ls->ls_flags);
  230. wake_up_process(ls->ls_recoverd_task);
  231. }
  232. int dlm_recoverd_start(struct dlm_ls *ls)
  233. {
  234. struct task_struct *p;
  235. int error = 0;
  236. p = kthread_run(dlm_recoverd, ls, "dlm_recoverd");
  237. if (IS_ERR(p))
  238. error = PTR_ERR(p);
  239. else
  240. ls->ls_recoverd_task = p;
  241. return error;
  242. }
  243. void dlm_recoverd_stop(struct dlm_ls *ls)
  244. {
  245. kthread_stop(ls->ls_recoverd_task);
  246. }
  247. void dlm_recoverd_suspend(struct dlm_ls *ls)
  248. {
  249. wake_up(&ls->ls_wait_general);
  250. mutex_lock(&ls->ls_recoverd_active);
  251. }
  252. void dlm_recoverd_resume(struct dlm_ls *ls)
  253. {
  254. mutex_unlock(&ls->ls_recoverd_active);
  255. }