recover.c 22 KB

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