recover.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  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 "dir.h"
  16. #include "config.h"
  17. #include "ast.h"
  18. #include "memory.h"
  19. #include "rcom.h"
  20. #include "lock.h"
  21. #include "lowcomms.h"
  22. #include "member.h"
  23. #include "recover.h"
  24. /*
  25. * Recovery waiting routines: these functions wait for a particular reply from
  26. * a remote node, or for the remote node to report a certain status. They need
  27. * to abort if the lockspace is stopped indicating a node has failed (perhaps
  28. * the one being waited for).
  29. */
  30. /*
  31. * Wait until given function returns non-zero or lockspace is stopped
  32. * (LS_RECOVERY_STOP set due to failure of a node in ls_nodes). When another
  33. * function thinks it could have completed the waited-on task, they should wake
  34. * up ls_wait_general to get an immediate response rather than waiting for the
  35. * timer to detect the result. A timer wakes us up periodically while waiting
  36. * to see if we should abort due to a node failure. This should only be called
  37. * by the dlm_recoverd thread.
  38. */
  39. static void dlm_wait_timer_fn(unsigned long data)
  40. {
  41. struct dlm_ls *ls = (struct dlm_ls *) data;
  42. mod_timer(&ls->ls_timer, jiffies + (dlm_config.ci_recover_timer * HZ));
  43. wake_up(&ls->ls_wait_general);
  44. }
  45. int dlm_wait_function(struct dlm_ls *ls, int (*testfn) (struct dlm_ls *ls))
  46. {
  47. int error = 0;
  48. init_timer(&ls->ls_timer);
  49. ls->ls_timer.function = dlm_wait_timer_fn;
  50. ls->ls_timer.data = (long) ls;
  51. ls->ls_timer.expires = jiffies + (dlm_config.ci_recover_timer * HZ);
  52. add_timer(&ls->ls_timer);
  53. wait_event(ls->ls_wait_general, testfn(ls) || dlm_recovery_stopped(ls));
  54. del_timer_sync(&ls->ls_timer);
  55. if (dlm_recovery_stopped(ls)) {
  56. log_debug(ls, "dlm_wait_function aborted");
  57. error = -EINTR;
  58. }
  59. return error;
  60. }
  61. /*
  62. * An efficient way for all nodes to wait for all others to have a certain
  63. * status. The node with the lowest nodeid polls all the others for their
  64. * status (wait_status_all) and all the others poll the node with the low id
  65. * for its accumulated result (wait_status_low). When all nodes have set
  66. * status flag X, then status flag X_ALL will be set on the low nodeid.
  67. */
  68. uint32_t dlm_recover_status(struct dlm_ls *ls)
  69. {
  70. uint32_t status;
  71. spin_lock(&ls->ls_recover_lock);
  72. status = ls->ls_recover_status;
  73. spin_unlock(&ls->ls_recover_lock);
  74. return status;
  75. }
  76. static void _set_recover_status(struct dlm_ls *ls, uint32_t status)
  77. {
  78. ls->ls_recover_status |= status;
  79. }
  80. void dlm_set_recover_status(struct dlm_ls *ls, uint32_t status)
  81. {
  82. spin_lock(&ls->ls_recover_lock);
  83. _set_recover_status(ls, status);
  84. spin_unlock(&ls->ls_recover_lock);
  85. }
  86. static int wait_status_all(struct dlm_ls *ls, uint32_t wait_status,
  87. int save_slots)
  88. {
  89. struct dlm_rcom *rc = ls->ls_recover_buf;
  90. struct dlm_member *memb;
  91. int error = 0, delay;
  92. list_for_each_entry(memb, &ls->ls_nodes, list) {
  93. delay = 0;
  94. for (;;) {
  95. if (dlm_recovery_stopped(ls)) {
  96. error = -EINTR;
  97. goto out;
  98. }
  99. error = dlm_rcom_status(ls, memb->nodeid, 0);
  100. if (error)
  101. goto out;
  102. if (save_slots)
  103. dlm_slot_save(ls, rc, memb);
  104. if (rc->rc_result & wait_status)
  105. break;
  106. if (delay < 1000)
  107. delay += 20;
  108. msleep(delay);
  109. }
  110. }
  111. out:
  112. return error;
  113. }
  114. static int wait_status_low(struct dlm_ls *ls, uint32_t wait_status,
  115. uint32_t status_flags)
  116. {
  117. struct dlm_rcom *rc = ls->ls_recover_buf;
  118. int error = 0, delay = 0, nodeid = ls->ls_low_nodeid;
  119. for (;;) {
  120. if (dlm_recovery_stopped(ls)) {
  121. error = -EINTR;
  122. goto out;
  123. }
  124. error = dlm_rcom_status(ls, nodeid, status_flags);
  125. if (error)
  126. break;
  127. if (rc->rc_result & wait_status)
  128. break;
  129. if (delay < 1000)
  130. delay += 20;
  131. msleep(delay);
  132. }
  133. out:
  134. return error;
  135. }
  136. static int wait_status(struct dlm_ls *ls, uint32_t status)
  137. {
  138. uint32_t status_all = status << 1;
  139. int error;
  140. if (ls->ls_low_nodeid == dlm_our_nodeid()) {
  141. error = wait_status_all(ls, status, 0);
  142. if (!error)
  143. dlm_set_recover_status(ls, status_all);
  144. } else
  145. error = wait_status_low(ls, status_all, 0);
  146. return error;
  147. }
  148. int dlm_recover_members_wait(struct dlm_ls *ls)
  149. {
  150. struct dlm_member *memb;
  151. struct dlm_slot *slots;
  152. int num_slots, slots_size;
  153. int error, rv;
  154. uint32_t gen;
  155. list_for_each_entry(memb, &ls->ls_nodes, list) {
  156. memb->slot = -1;
  157. memb->generation = 0;
  158. }
  159. if (ls->ls_low_nodeid == dlm_our_nodeid()) {
  160. error = wait_status_all(ls, DLM_RS_NODES, 1);
  161. if (error)
  162. goto out;
  163. /* slots array is sparse, slots_size may be > num_slots */
  164. rv = dlm_slots_assign(ls, &num_slots, &slots_size, &slots, &gen);
  165. if (!rv) {
  166. spin_lock(&ls->ls_recover_lock);
  167. _set_recover_status(ls, DLM_RS_NODES_ALL);
  168. ls->ls_num_slots = num_slots;
  169. ls->ls_slots_size = slots_size;
  170. ls->ls_slots = slots;
  171. ls->ls_generation = gen;
  172. spin_unlock(&ls->ls_recover_lock);
  173. } else {
  174. dlm_set_recover_status(ls, DLM_RS_NODES_ALL);
  175. }
  176. } else {
  177. error = wait_status_low(ls, DLM_RS_NODES_ALL, DLM_RSF_NEED_SLOTS);
  178. if (error)
  179. goto out;
  180. dlm_slots_copy_in(ls);
  181. }
  182. out:
  183. return error;
  184. }
  185. int dlm_recover_directory_wait(struct dlm_ls *ls)
  186. {
  187. return wait_status(ls, DLM_RS_DIR);
  188. }
  189. int dlm_recover_locks_wait(struct dlm_ls *ls)
  190. {
  191. return wait_status(ls, DLM_RS_LOCKS);
  192. }
  193. int dlm_recover_done_wait(struct dlm_ls *ls)
  194. {
  195. return wait_status(ls, DLM_RS_DONE);
  196. }
  197. /*
  198. * The recover_list contains all the rsb's for which we've requested the new
  199. * master nodeid. As replies are returned from the resource directories the
  200. * rsb's are removed from the list. When the list is empty we're done.
  201. *
  202. * The recover_list is later similarly used for all rsb's for which we've sent
  203. * new lkb's and need to receive new corresponding lkid's.
  204. *
  205. * We use the address of the rsb struct as a simple local identifier for the
  206. * rsb so we can match an rcom reply with the rsb it was sent for.
  207. */
  208. static int recover_list_empty(struct dlm_ls *ls)
  209. {
  210. int empty;
  211. spin_lock(&ls->ls_recover_list_lock);
  212. empty = list_empty(&ls->ls_recover_list);
  213. spin_unlock(&ls->ls_recover_list_lock);
  214. return empty;
  215. }
  216. static void recover_list_add(struct dlm_rsb *r)
  217. {
  218. struct dlm_ls *ls = r->res_ls;
  219. spin_lock(&ls->ls_recover_list_lock);
  220. if (list_empty(&r->res_recover_list)) {
  221. list_add_tail(&r->res_recover_list, &ls->ls_recover_list);
  222. ls->ls_recover_list_count++;
  223. dlm_hold_rsb(r);
  224. }
  225. spin_unlock(&ls->ls_recover_list_lock);
  226. }
  227. static void recover_list_del(struct dlm_rsb *r)
  228. {
  229. struct dlm_ls *ls = r->res_ls;
  230. spin_lock(&ls->ls_recover_list_lock);
  231. list_del_init(&r->res_recover_list);
  232. ls->ls_recover_list_count--;
  233. spin_unlock(&ls->ls_recover_list_lock);
  234. dlm_put_rsb(r);
  235. }
  236. static void recover_list_clear(struct dlm_ls *ls)
  237. {
  238. struct dlm_rsb *r, *s;
  239. spin_lock(&ls->ls_recover_list_lock);
  240. list_for_each_entry_safe(r, s, &ls->ls_recover_list, res_recover_list) {
  241. list_del_init(&r->res_recover_list);
  242. r->res_recover_locks_count = 0;
  243. dlm_put_rsb(r);
  244. ls->ls_recover_list_count--;
  245. }
  246. if (ls->ls_recover_list_count != 0) {
  247. log_error(ls, "warning: recover_list_count %d",
  248. ls->ls_recover_list_count);
  249. ls->ls_recover_list_count = 0;
  250. }
  251. spin_unlock(&ls->ls_recover_list_lock);
  252. }
  253. static int recover_idr_empty(struct dlm_ls *ls)
  254. {
  255. int empty = 1;
  256. spin_lock(&ls->ls_recover_idr_lock);
  257. if (ls->ls_recover_list_count)
  258. empty = 0;
  259. spin_unlock(&ls->ls_recover_idr_lock);
  260. return empty;
  261. }
  262. static int recover_idr_add(struct dlm_rsb *r)
  263. {
  264. struct dlm_ls *ls = r->res_ls;
  265. int rv, id;
  266. rv = idr_pre_get(&ls->ls_recover_idr, GFP_NOFS);
  267. if (!rv)
  268. return -ENOMEM;
  269. spin_lock(&ls->ls_recover_idr_lock);
  270. if (r->res_id) {
  271. spin_unlock(&ls->ls_recover_idr_lock);
  272. return -1;
  273. }
  274. rv = idr_get_new_above(&ls->ls_recover_idr, r, 1, &id);
  275. if (rv) {
  276. spin_unlock(&ls->ls_recover_idr_lock);
  277. return rv;
  278. }
  279. r->res_id = id;
  280. ls->ls_recover_list_count++;
  281. dlm_hold_rsb(r);
  282. spin_unlock(&ls->ls_recover_idr_lock);
  283. return 0;
  284. }
  285. static void recover_idr_del(struct dlm_rsb *r)
  286. {
  287. struct dlm_ls *ls = r->res_ls;
  288. spin_lock(&ls->ls_recover_idr_lock);
  289. idr_remove(&ls->ls_recover_idr, r->res_id);
  290. r->res_id = 0;
  291. ls->ls_recover_list_count--;
  292. spin_unlock(&ls->ls_recover_idr_lock);
  293. dlm_put_rsb(r);
  294. }
  295. static struct dlm_rsb *recover_idr_find(struct dlm_ls *ls, uint64_t id)
  296. {
  297. struct dlm_rsb *r;
  298. spin_lock(&ls->ls_recover_idr_lock);
  299. r = idr_find(&ls->ls_recover_idr, (int)id);
  300. spin_unlock(&ls->ls_recover_idr_lock);
  301. return r;
  302. }
  303. static int recover_idr_clear_rsb(int id, void *p, void *data)
  304. {
  305. struct dlm_ls *ls = data;
  306. struct dlm_rsb *r = p;
  307. r->res_id = 0;
  308. r->res_recover_locks_count = 0;
  309. ls->ls_recover_list_count--;
  310. dlm_put_rsb(r);
  311. return 0;
  312. }
  313. static void recover_idr_clear(struct dlm_ls *ls)
  314. {
  315. spin_lock(&ls->ls_recover_idr_lock);
  316. idr_for_each(&ls->ls_recover_idr, recover_idr_clear_rsb, ls);
  317. idr_remove_all(&ls->ls_recover_idr);
  318. if (ls->ls_recover_list_count != 0) {
  319. log_error(ls, "warning: recover_list_count %d",
  320. ls->ls_recover_list_count);
  321. ls->ls_recover_list_count = 0;
  322. }
  323. spin_unlock(&ls->ls_recover_idr_lock);
  324. }
  325. /* Master recovery: find new master node for rsb's that were
  326. mastered on nodes that have been removed.
  327. dlm_recover_masters
  328. recover_master
  329. dlm_send_rcom_lookup -> receive_rcom_lookup
  330. dlm_dir_lookup
  331. receive_rcom_lookup_reply <-
  332. dlm_recover_master_reply
  333. set_new_master
  334. set_master_lkbs
  335. set_lock_master
  336. */
  337. /*
  338. * Set the lock master for all LKBs in a lock queue
  339. * If we are the new master of the rsb, we may have received new
  340. * MSTCPY locks from other nodes already which we need to ignore
  341. * when setting the new nodeid.
  342. */
  343. static void set_lock_master(struct list_head *queue, int nodeid)
  344. {
  345. struct dlm_lkb *lkb;
  346. list_for_each_entry(lkb, queue, lkb_statequeue) {
  347. if (!(lkb->lkb_flags & DLM_IFL_MSTCPY)) {
  348. lkb->lkb_nodeid = nodeid;
  349. lkb->lkb_remid = 0;
  350. }
  351. }
  352. }
  353. static void set_master_lkbs(struct dlm_rsb *r)
  354. {
  355. set_lock_master(&r->res_grantqueue, r->res_nodeid);
  356. set_lock_master(&r->res_convertqueue, r->res_nodeid);
  357. set_lock_master(&r->res_waitqueue, r->res_nodeid);
  358. }
  359. /*
  360. * Propagate the new master nodeid to locks
  361. * The NEW_MASTER flag tells dlm_recover_locks() which rsb's to consider.
  362. * The NEW_MASTER2 flag tells recover_lvb() and recover_grant() which
  363. * rsb's to consider.
  364. */
  365. static void set_new_master(struct dlm_rsb *r)
  366. {
  367. set_master_lkbs(r);
  368. rsb_set_flag(r, RSB_NEW_MASTER);
  369. rsb_set_flag(r, RSB_NEW_MASTER2);
  370. }
  371. /*
  372. * We do async lookups on rsb's that need new masters. The rsb's
  373. * waiting for a lookup reply are kept on the recover_list.
  374. *
  375. * Another node recovering the master may have sent us a rcom lookup,
  376. * and our dlm_master_lookup() set it as the new master, along with
  377. * NEW_MASTER so that we'll recover it here (this implies dir_nodeid
  378. * equals our_nodeid below).
  379. */
  380. static int recover_master(struct dlm_rsb *r, unsigned int *count)
  381. {
  382. struct dlm_ls *ls = r->res_ls;
  383. int our_nodeid, dir_nodeid;
  384. int is_removed = 0;
  385. int error;
  386. if (is_master(r))
  387. return 0;
  388. is_removed = dlm_is_removed(ls, r->res_nodeid);
  389. if (!is_removed && !rsb_flag(r, RSB_NEW_MASTER))
  390. return 0;
  391. our_nodeid = dlm_our_nodeid();
  392. dir_nodeid = dlm_dir_nodeid(r);
  393. if (dir_nodeid == our_nodeid) {
  394. if (is_removed) {
  395. r->res_master_nodeid = our_nodeid;
  396. r->res_nodeid = 0;
  397. }
  398. /* set master of lkbs to ourself when is_removed, or to
  399. another new master which we set along with NEW_MASTER
  400. in dlm_master_lookup */
  401. set_new_master(r);
  402. error = 0;
  403. } else {
  404. recover_idr_add(r);
  405. error = dlm_send_rcom_lookup(r, dir_nodeid);
  406. }
  407. (*count)++;
  408. return error;
  409. }
  410. /*
  411. * All MSTCPY locks are purged and rebuilt, even if the master stayed the same.
  412. * This is necessary because recovery can be started, aborted and restarted,
  413. * causing the master nodeid to briefly change during the aborted recovery, and
  414. * change back to the original value in the second recovery. The MSTCPY locks
  415. * may or may not have been purged during the aborted recovery. Another node
  416. * with an outstanding request in waiters list and a request reply saved in the
  417. * requestqueue, cannot know whether it should ignore the reply and resend the
  418. * request, or accept the reply and complete the request. It must do the
  419. * former if the remote node purged MSTCPY locks, and it must do the later if
  420. * the remote node did not. This is solved by always purging MSTCPY locks, in
  421. * which case, the request reply would always be ignored and the request
  422. * resent.
  423. */
  424. static int recover_master_static(struct dlm_rsb *r, unsigned int *count)
  425. {
  426. int dir_nodeid = dlm_dir_nodeid(r);
  427. int new_master = dir_nodeid;
  428. if (dir_nodeid == dlm_our_nodeid())
  429. new_master = 0;
  430. dlm_purge_mstcpy_locks(r);
  431. r->res_master_nodeid = dir_nodeid;
  432. r->res_nodeid = new_master;
  433. set_new_master(r);
  434. (*count)++;
  435. return 0;
  436. }
  437. /*
  438. * Go through local root resources and for each rsb which has a master which
  439. * has departed, get the new master nodeid from the directory. The dir will
  440. * assign mastery to the first node to look up the new master. That means
  441. * we'll discover in this lookup if we're the new master of any rsb's.
  442. *
  443. * We fire off all the dir lookup requests individually and asynchronously to
  444. * the correct dir node.
  445. */
  446. int dlm_recover_masters(struct dlm_ls *ls)
  447. {
  448. struct dlm_rsb *r;
  449. unsigned int total = 0;
  450. unsigned int count = 0;
  451. int nodir = dlm_no_directory(ls);
  452. int error;
  453. log_debug(ls, "dlm_recover_masters");
  454. down_read(&ls->ls_root_sem);
  455. list_for_each_entry(r, &ls->ls_root_list, res_root_list) {
  456. if (dlm_recovery_stopped(ls)) {
  457. up_read(&ls->ls_root_sem);
  458. error = -EINTR;
  459. goto out;
  460. }
  461. lock_rsb(r);
  462. if (nodir)
  463. error = recover_master_static(r, &count);
  464. else
  465. error = recover_master(r, &count);
  466. unlock_rsb(r);
  467. cond_resched();
  468. total++;
  469. if (error) {
  470. up_read(&ls->ls_root_sem);
  471. goto out;
  472. }
  473. }
  474. up_read(&ls->ls_root_sem);
  475. log_debug(ls, "dlm_recover_masters %u of %u", count, total);
  476. error = dlm_wait_function(ls, &recover_idr_empty);
  477. out:
  478. if (error)
  479. recover_idr_clear(ls);
  480. return error;
  481. }
  482. int dlm_recover_master_reply(struct dlm_ls *ls, struct dlm_rcom *rc)
  483. {
  484. struct dlm_rsb *r;
  485. int ret_nodeid, new_master;
  486. r = recover_idr_find(ls, rc->rc_id);
  487. if (!r) {
  488. log_error(ls, "dlm_recover_master_reply no id %llx",
  489. (unsigned long long)rc->rc_id);
  490. goto out;
  491. }
  492. ret_nodeid = rc->rc_result;
  493. if (ret_nodeid == dlm_our_nodeid())
  494. new_master = 0;
  495. else
  496. new_master = ret_nodeid;
  497. lock_rsb(r);
  498. r->res_master_nodeid = ret_nodeid;
  499. r->res_nodeid = new_master;
  500. set_new_master(r);
  501. unlock_rsb(r);
  502. recover_idr_del(r);
  503. if (recover_idr_empty(ls))
  504. wake_up(&ls->ls_wait_general);
  505. out:
  506. return 0;
  507. }
  508. /* Lock recovery: rebuild the process-copy locks we hold on a
  509. remastered rsb on the new rsb master.
  510. dlm_recover_locks
  511. recover_locks
  512. recover_locks_queue
  513. dlm_send_rcom_lock -> receive_rcom_lock
  514. dlm_recover_master_copy
  515. receive_rcom_lock_reply <-
  516. dlm_recover_process_copy
  517. */
  518. /*
  519. * keep a count of the number of lkb's we send to the new master; when we get
  520. * an equal number of replies then recovery for the rsb is done
  521. */
  522. static int recover_locks_queue(struct dlm_rsb *r, struct list_head *head)
  523. {
  524. struct dlm_lkb *lkb;
  525. int error = 0;
  526. list_for_each_entry(lkb, head, lkb_statequeue) {
  527. error = dlm_send_rcom_lock(r, lkb);
  528. if (error)
  529. break;
  530. r->res_recover_locks_count++;
  531. }
  532. return error;
  533. }
  534. static int recover_locks(struct dlm_rsb *r)
  535. {
  536. int error = 0;
  537. lock_rsb(r);
  538. DLM_ASSERT(!r->res_recover_locks_count, dlm_dump_rsb(r););
  539. error = recover_locks_queue(r, &r->res_grantqueue);
  540. if (error)
  541. goto out;
  542. error = recover_locks_queue(r, &r->res_convertqueue);
  543. if (error)
  544. goto out;
  545. error = recover_locks_queue(r, &r->res_waitqueue);
  546. if (error)
  547. goto out;
  548. if (r->res_recover_locks_count)
  549. recover_list_add(r);
  550. else
  551. rsb_clear_flag(r, RSB_NEW_MASTER);
  552. out:
  553. unlock_rsb(r);
  554. return error;
  555. }
  556. int dlm_recover_locks(struct dlm_ls *ls)
  557. {
  558. struct dlm_rsb *r;
  559. int error, count = 0;
  560. down_read(&ls->ls_root_sem);
  561. list_for_each_entry(r, &ls->ls_root_list, res_root_list) {
  562. if (is_master(r)) {
  563. rsb_clear_flag(r, RSB_NEW_MASTER);
  564. continue;
  565. }
  566. if (!rsb_flag(r, RSB_NEW_MASTER))
  567. continue;
  568. if (dlm_recovery_stopped(ls)) {
  569. error = -EINTR;
  570. up_read(&ls->ls_root_sem);
  571. goto out;
  572. }
  573. error = recover_locks(r);
  574. if (error) {
  575. up_read(&ls->ls_root_sem);
  576. goto out;
  577. }
  578. count += r->res_recover_locks_count;
  579. }
  580. up_read(&ls->ls_root_sem);
  581. log_debug(ls, "dlm_recover_locks %d out", count);
  582. error = dlm_wait_function(ls, &recover_list_empty);
  583. out:
  584. if (error)
  585. recover_list_clear(ls);
  586. return error;
  587. }
  588. void dlm_recovered_lock(struct dlm_rsb *r)
  589. {
  590. DLM_ASSERT(rsb_flag(r, RSB_NEW_MASTER), dlm_dump_rsb(r););
  591. r->res_recover_locks_count--;
  592. if (!r->res_recover_locks_count) {
  593. rsb_clear_flag(r, RSB_NEW_MASTER);
  594. recover_list_del(r);
  595. }
  596. if (recover_list_empty(r->res_ls))
  597. wake_up(&r->res_ls->ls_wait_general);
  598. }
  599. /*
  600. * The lvb needs to be recovered on all master rsb's. This includes setting
  601. * the VALNOTVALID flag if necessary, and determining the correct lvb contents
  602. * based on the lvb's of the locks held on the rsb.
  603. *
  604. * RSB_VALNOTVALID is set if there are only NL/CR locks on the rsb. If it
  605. * was already set prior to recovery, it's not cleared, regardless of locks.
  606. *
  607. * The LVB contents are only considered for changing when this is a new master
  608. * of the rsb (NEW_MASTER2). Then, the rsb's lvb is taken from any lkb with
  609. * mode > CR. If no lkb's exist with mode above CR, the lvb contents are taken
  610. * from the lkb with the largest lvb sequence number.
  611. */
  612. static void recover_lvb(struct dlm_rsb *r)
  613. {
  614. struct dlm_lkb *lkb, *high_lkb = NULL;
  615. uint32_t high_seq = 0;
  616. int lock_lvb_exists = 0;
  617. int big_lock_exists = 0;
  618. int lvblen = r->res_ls->ls_lvblen;
  619. list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue) {
  620. if (!(lkb->lkb_exflags & DLM_LKF_VALBLK))
  621. continue;
  622. lock_lvb_exists = 1;
  623. if (lkb->lkb_grmode > DLM_LOCK_CR) {
  624. big_lock_exists = 1;
  625. goto setflag;
  626. }
  627. if (((int)lkb->lkb_lvbseq - (int)high_seq) >= 0) {
  628. high_lkb = lkb;
  629. high_seq = lkb->lkb_lvbseq;
  630. }
  631. }
  632. list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue) {
  633. if (!(lkb->lkb_exflags & DLM_LKF_VALBLK))
  634. continue;
  635. lock_lvb_exists = 1;
  636. if (lkb->lkb_grmode > DLM_LOCK_CR) {
  637. big_lock_exists = 1;
  638. goto setflag;
  639. }
  640. if (((int)lkb->lkb_lvbseq - (int)high_seq) >= 0) {
  641. high_lkb = lkb;
  642. high_seq = lkb->lkb_lvbseq;
  643. }
  644. }
  645. setflag:
  646. if (!lock_lvb_exists)
  647. goto out;
  648. if (!big_lock_exists)
  649. rsb_set_flag(r, RSB_VALNOTVALID);
  650. /* don't mess with the lvb unless we're the new master */
  651. if (!rsb_flag(r, RSB_NEW_MASTER2))
  652. goto out;
  653. if (!r->res_lvbptr) {
  654. r->res_lvbptr = dlm_allocate_lvb(r->res_ls);
  655. if (!r->res_lvbptr)
  656. goto out;
  657. }
  658. if (big_lock_exists) {
  659. r->res_lvbseq = lkb->lkb_lvbseq;
  660. memcpy(r->res_lvbptr, lkb->lkb_lvbptr, lvblen);
  661. } else if (high_lkb) {
  662. r->res_lvbseq = high_lkb->lkb_lvbseq;
  663. memcpy(r->res_lvbptr, high_lkb->lkb_lvbptr, lvblen);
  664. } else {
  665. r->res_lvbseq = 0;
  666. memset(r->res_lvbptr, 0, lvblen);
  667. }
  668. out:
  669. return;
  670. }
  671. /* All master rsb's flagged RECOVER_CONVERT need to be looked at. The locks
  672. converting PR->CW or CW->PR need to have their lkb_grmode set. */
  673. static void recover_conversion(struct dlm_rsb *r)
  674. {
  675. struct dlm_lkb *lkb;
  676. int grmode = -1;
  677. list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue) {
  678. if (lkb->lkb_grmode == DLM_LOCK_PR ||
  679. lkb->lkb_grmode == DLM_LOCK_CW) {
  680. grmode = lkb->lkb_grmode;
  681. break;
  682. }
  683. }
  684. list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue) {
  685. if (lkb->lkb_grmode != DLM_LOCK_IV)
  686. continue;
  687. if (grmode == -1)
  688. lkb->lkb_grmode = lkb->lkb_rqmode;
  689. else
  690. lkb->lkb_grmode = grmode;
  691. }
  692. }
  693. /* We've become the new master for this rsb and waiting/converting locks may
  694. need to be granted in dlm_recover_grant() due to locks that may have
  695. existed from a removed node. */
  696. static void recover_grant(struct dlm_rsb *r)
  697. {
  698. if (!list_empty(&r->res_waitqueue) || !list_empty(&r->res_convertqueue))
  699. rsb_set_flag(r, RSB_RECOVER_GRANT);
  700. }
  701. void dlm_recover_rsbs(struct dlm_ls *ls)
  702. {
  703. struct dlm_rsb *r;
  704. unsigned int count = 0;
  705. down_read(&ls->ls_root_sem);
  706. list_for_each_entry(r, &ls->ls_root_list, res_root_list) {
  707. lock_rsb(r);
  708. if (is_master(r)) {
  709. if (rsb_flag(r, RSB_RECOVER_CONVERT))
  710. recover_conversion(r);
  711. if (rsb_flag(r, RSB_NEW_MASTER2))
  712. recover_grant(r);
  713. recover_lvb(r);
  714. count++;
  715. }
  716. rsb_clear_flag(r, RSB_RECOVER_CONVERT);
  717. rsb_clear_flag(r, RSB_NEW_MASTER2);
  718. unlock_rsb(r);
  719. }
  720. up_read(&ls->ls_root_sem);
  721. if (count)
  722. log_debug(ls, "dlm_recover_rsbs %d done", count);
  723. }
  724. /* Create a single list of all root rsb's to be used during recovery */
  725. int dlm_create_root_list(struct dlm_ls *ls)
  726. {
  727. struct rb_node *n;
  728. struct dlm_rsb *r;
  729. int i, error = 0;
  730. down_write(&ls->ls_root_sem);
  731. if (!list_empty(&ls->ls_root_list)) {
  732. log_error(ls, "root list not empty");
  733. error = -EINVAL;
  734. goto out;
  735. }
  736. for (i = 0; i < ls->ls_rsbtbl_size; i++) {
  737. spin_lock(&ls->ls_rsbtbl[i].lock);
  738. for (n = rb_first(&ls->ls_rsbtbl[i].keep); n; n = rb_next(n)) {
  739. r = rb_entry(n, struct dlm_rsb, res_hashnode);
  740. list_add(&r->res_root_list, &ls->ls_root_list);
  741. dlm_hold_rsb(r);
  742. }
  743. if (!RB_EMPTY_ROOT(&ls->ls_rsbtbl[i].toss))
  744. log_error(ls, "dlm_create_root_list toss not empty");
  745. spin_unlock(&ls->ls_rsbtbl[i].lock);
  746. }
  747. out:
  748. up_write(&ls->ls_root_sem);
  749. return error;
  750. }
  751. void dlm_release_root_list(struct dlm_ls *ls)
  752. {
  753. struct dlm_rsb *r, *safe;
  754. down_write(&ls->ls_root_sem);
  755. list_for_each_entry_safe(r, safe, &ls->ls_root_list, res_root_list) {
  756. list_del_init(&r->res_root_list);
  757. dlm_put_rsb(r);
  758. }
  759. up_write(&ls->ls_root_sem);
  760. }
  761. void dlm_clear_toss(struct dlm_ls *ls)
  762. {
  763. struct rb_node *n, *next;
  764. struct dlm_rsb *r;
  765. unsigned int count = 0;
  766. int i;
  767. for (i = 0; i < ls->ls_rsbtbl_size; i++) {
  768. spin_lock(&ls->ls_rsbtbl[i].lock);
  769. for (n = rb_first(&ls->ls_rsbtbl[i].toss); n; n = next) {
  770. next = rb_next(n);
  771. r = rb_entry(n, struct dlm_rsb, res_hashnode);
  772. rb_erase(n, &ls->ls_rsbtbl[i].toss);
  773. dlm_free_rsb(r);
  774. count++;
  775. }
  776. spin_unlock(&ls->ls_rsbtbl[i].lock);
  777. }
  778. if (count)
  779. log_debug(ls, "dlm_clear_toss %u done", count);
  780. }