recoverd.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. * Free non-master tossed rsb's. Master rsb's are kept on toss
  57. * list and put on root list to be included in resdir recovery.
  58. */
  59. dlm_clear_toss_list(ls);
  60. /*
  61. * This list of root rsb's will be the basis of most of the recovery
  62. * routines.
  63. */
  64. dlm_create_root_list(ls);
  65. /*
  66. * Add or remove nodes from the lockspace's ls_nodes list.
  67. * Also waits for all nodes to complete dlm_recover_members.
  68. */
  69. error = dlm_recover_members(ls, rv, &neg);
  70. if (error) {
  71. log_debug(ls, "recover_members failed %d", error);
  72. goto fail;
  73. }
  74. start = jiffies;
  75. /*
  76. * Rebuild our own share of the directory by collecting from all other
  77. * nodes their master rsb names that hash to us.
  78. */
  79. error = dlm_recover_directory(ls);
  80. if (error) {
  81. log_debug(ls, "recover_directory failed %d", error);
  82. goto fail;
  83. }
  84. /*
  85. * Wait for all nodes to complete directory rebuild.
  86. */
  87. error = dlm_recover_directory_wait(ls);
  88. if (error) {
  89. log_debug(ls, "recover_directory_wait failed %d", error);
  90. goto fail;
  91. }
  92. /*
  93. * We may have outstanding operations that are waiting for a reply from
  94. * a failed node. Mark these to be resent after recovery. Unlock and
  95. * cancel ops can just be completed.
  96. */
  97. dlm_recover_waiters_pre(ls);
  98. error = dlm_recovery_stopped(ls);
  99. if (error)
  100. goto fail;
  101. if (neg || dlm_no_directory(ls)) {
  102. /*
  103. * Clear lkb's for departed nodes.
  104. */
  105. dlm_purge_locks(ls);
  106. /*
  107. * Get new master nodeid's for rsb's that were mastered on
  108. * departed nodes.
  109. */
  110. error = dlm_recover_masters(ls);
  111. if (error) {
  112. log_debug(ls, "recover_masters failed %d", error);
  113. goto fail;
  114. }
  115. /*
  116. * Send our locks on remastered rsb's to the new masters.
  117. */
  118. error = dlm_recover_locks(ls);
  119. if (error) {
  120. log_debug(ls, "recover_locks failed %d", error);
  121. goto fail;
  122. }
  123. error = dlm_recover_locks_wait(ls);
  124. if (error) {
  125. log_debug(ls, "recover_locks_wait failed %d", error);
  126. goto fail;
  127. }
  128. /*
  129. * Finalize state in master rsb's now that all locks can be
  130. * checked. This includes conversion resolution and lvb
  131. * settings.
  132. */
  133. dlm_recover_rsbs(ls);
  134. } else {
  135. /*
  136. * Other lockspace members may be going through the "neg" steps
  137. * while also adding us to the lockspace, in which case they'll
  138. * be doing the recover_locks (RS_LOCKS) barrier.
  139. */
  140. dlm_set_recover_status(ls, DLM_RS_LOCKS);
  141. error = dlm_recover_locks_wait(ls);
  142. if (error) {
  143. log_debug(ls, "recover_locks_wait failed %d", error);
  144. goto fail;
  145. }
  146. }
  147. dlm_release_root_list(ls);
  148. /*
  149. * Purge directory-related requests that are saved in requestqueue.
  150. * All dir requests from before recovery are invalid now due to the dir
  151. * rebuild and will be resent by the requesting nodes.
  152. */
  153. dlm_purge_requestqueue(ls);
  154. dlm_set_recover_status(ls, DLM_RS_DONE);
  155. error = dlm_recover_done_wait(ls);
  156. if (error) {
  157. log_debug(ls, "recover_done_wait failed %d", error);
  158. goto fail;
  159. }
  160. dlm_clear_members_gone(ls);
  161. dlm_adjust_timeouts(ls);
  162. error = enable_locking(ls, rv->seq);
  163. if (error) {
  164. log_debug(ls, "enable_locking failed %d", error);
  165. goto fail;
  166. }
  167. error = dlm_process_requestqueue(ls);
  168. if (error) {
  169. log_debug(ls, "process_requestqueue failed %d", error);
  170. goto fail;
  171. }
  172. error = dlm_recover_waiters_post(ls);
  173. if (error) {
  174. log_debug(ls, "recover_waiters_post failed %d", error);
  175. goto fail;
  176. }
  177. dlm_grant_after_purge(ls);
  178. dlm_astd_wake();
  179. log_debug(ls, "recover %llx done: %u ms",
  180. (unsigned long long)rv->seq,
  181. jiffies_to_msecs(jiffies - start));
  182. mutex_unlock(&ls->ls_recoverd_active);
  183. return 0;
  184. fail:
  185. dlm_release_root_list(ls);
  186. log_debug(ls, "recover %llx error %d",
  187. (unsigned long long)rv->seq, error);
  188. mutex_unlock(&ls->ls_recoverd_active);
  189. return error;
  190. }
  191. /* The dlm_ls_start() that created the rv we take here may already have been
  192. stopped via dlm_ls_stop(); in that case we need to leave the RECOVERY_STOP
  193. flag set. */
  194. static void do_ls_recovery(struct dlm_ls *ls)
  195. {
  196. struct dlm_recover *rv = NULL;
  197. spin_lock(&ls->ls_recover_lock);
  198. rv = ls->ls_recover_args;
  199. ls->ls_recover_args = NULL;
  200. if (rv && ls->ls_recover_seq == rv->seq)
  201. clear_bit(LSFL_RECOVERY_STOP, &ls->ls_flags);
  202. spin_unlock(&ls->ls_recover_lock);
  203. if (rv) {
  204. ls_recover(ls, rv);
  205. kfree(rv->nodeids);
  206. kfree(rv->new);
  207. kfree(rv);
  208. }
  209. }
  210. static int dlm_recoverd(void *arg)
  211. {
  212. struct dlm_ls *ls;
  213. ls = dlm_find_lockspace_local(arg);
  214. if (!ls) {
  215. log_print("dlm_recoverd: no lockspace %p", arg);
  216. return -1;
  217. }
  218. while (!kthread_should_stop()) {
  219. set_current_state(TASK_INTERRUPTIBLE);
  220. if (!test_bit(LSFL_WORK, &ls->ls_flags))
  221. schedule();
  222. set_current_state(TASK_RUNNING);
  223. if (test_and_clear_bit(LSFL_WORK, &ls->ls_flags))
  224. do_ls_recovery(ls);
  225. }
  226. dlm_put_lockspace(ls);
  227. return 0;
  228. }
  229. void dlm_recoverd_kick(struct dlm_ls *ls)
  230. {
  231. set_bit(LSFL_WORK, &ls->ls_flags);
  232. wake_up_process(ls->ls_recoverd_task);
  233. }
  234. int dlm_recoverd_start(struct dlm_ls *ls)
  235. {
  236. struct task_struct *p;
  237. int error = 0;
  238. p = kthread_run(dlm_recoverd, ls, "dlm_recoverd");
  239. if (IS_ERR(p))
  240. error = PTR_ERR(p);
  241. else
  242. ls->ls_recoverd_task = p;
  243. return error;
  244. }
  245. void dlm_recoverd_stop(struct dlm_ls *ls)
  246. {
  247. kthread_stop(ls->ls_recoverd_task);
  248. }
  249. void dlm_recoverd_suspend(struct dlm_ls *ls)
  250. {
  251. wake_up(&ls->ls_wait_general);
  252. mutex_lock(&ls->ls_recoverd_active);
  253. }
  254. void dlm_recoverd_resume(struct dlm_ls *ls)
  255. {
  256. mutex_unlock(&ls->ls_recoverd_active);
  257. }