recover.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  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. if (test_bit(LSFL_RCOM_WAIT, &ls->ls_flags)) {
  50. log_debug(ls, "dlm_wait_function timed out");
  51. return -ETIMEDOUT;
  52. }
  53. }
  54. if (dlm_recovery_stopped(ls)) {
  55. log_debug(ls, "dlm_wait_function aborted");
  56. error = -EINTR;
  57. }
  58. return error;
  59. }
  60. /*
  61. * An efficient way for all nodes to wait for all others to have a certain
  62. * status. The node with the lowest nodeid polls all the others for their
  63. * status (wait_status_all) and all the others poll the node with the low id
  64. * for its accumulated result (wait_status_low). When all nodes have set
  65. * status flag X, then status flag X_ALL will be set on the low nodeid.
  66. */
  67. uint32_t dlm_recover_status(struct dlm_ls *ls)
  68. {
  69. uint32_t status;
  70. spin_lock(&ls->ls_recover_lock);
  71. status = ls->ls_recover_status;
  72. spin_unlock(&ls->ls_recover_lock);
  73. return status;
  74. }
  75. static void _set_recover_status(struct dlm_ls *ls, uint32_t status)
  76. {
  77. ls->ls_recover_status |= status;
  78. }
  79. void dlm_set_recover_status(struct dlm_ls *ls, uint32_t status)
  80. {
  81. spin_lock(&ls->ls_recover_lock);
  82. _set_recover_status(ls, status);
  83. spin_unlock(&ls->ls_recover_lock);
  84. }
  85. static int wait_status_all(struct dlm_ls *ls, uint32_t wait_status,
  86. int save_slots)
  87. {
  88. struct dlm_rcom *rc = ls->ls_recover_buf;
  89. struct dlm_member *memb;
  90. int error = 0, delay;
  91. list_for_each_entry(memb, &ls->ls_nodes, list) {
  92. delay = 0;
  93. for (;;) {
  94. if (dlm_recovery_stopped(ls)) {
  95. error = -EINTR;
  96. goto out;
  97. }
  98. error = dlm_rcom_status(ls, memb->nodeid, 0);
  99. if (error)
  100. goto out;
  101. if (save_slots)
  102. dlm_slot_save(ls, rc, memb);
  103. if (rc->rc_result & wait_status)
  104. break;
  105. if (delay < 1000)
  106. delay += 20;
  107. msleep(delay);
  108. }
  109. }
  110. out:
  111. return error;
  112. }
  113. static int wait_status_low(struct dlm_ls *ls, uint32_t wait_status,
  114. uint32_t status_flags)
  115. {
  116. struct dlm_rcom *rc = ls->ls_recover_buf;
  117. int error = 0, delay = 0, nodeid = ls->ls_low_nodeid;
  118. for (;;) {
  119. if (dlm_recovery_stopped(ls)) {
  120. error = -EINTR;
  121. goto out;
  122. }
  123. error = dlm_rcom_status(ls, nodeid, status_flags);
  124. if (error)
  125. break;
  126. if (rc->rc_result & wait_status)
  127. break;
  128. if (delay < 1000)
  129. delay += 20;
  130. msleep(delay);
  131. }
  132. out:
  133. return error;
  134. }
  135. static int wait_status(struct dlm_ls *ls, uint32_t status)
  136. {
  137. uint32_t status_all = status << 1;
  138. int error;
  139. if (ls->ls_low_nodeid == dlm_our_nodeid()) {
  140. error = wait_status_all(ls, status, 0);
  141. if (!error)
  142. dlm_set_recover_status(ls, status_all);
  143. } else
  144. error = wait_status_low(ls, status_all, 0);
  145. return error;
  146. }
  147. int dlm_recover_members_wait(struct dlm_ls *ls)
  148. {
  149. struct dlm_member *memb;
  150. struct dlm_slot *slots;
  151. int num_slots, slots_size;
  152. int error, rv;
  153. uint32_t gen;
  154. list_for_each_entry(memb, &ls->ls_nodes, list) {
  155. memb->slot = -1;
  156. memb->generation = 0;
  157. }
  158. if (ls->ls_low_nodeid == dlm_our_nodeid()) {
  159. error = wait_status_all(ls, DLM_RS_NODES, 1);
  160. if (error)
  161. goto out;
  162. /* slots array is sparse, slots_size may be > num_slots */
  163. rv = dlm_slots_assign(ls, &num_slots, &slots_size, &slots, &gen);
  164. if (!rv) {
  165. spin_lock(&ls->ls_recover_lock);
  166. _set_recover_status(ls, DLM_RS_NODES_ALL);
  167. ls->ls_num_slots = num_slots;
  168. ls->ls_slots_size = slots_size;
  169. ls->ls_slots = slots;
  170. ls->ls_generation = gen;
  171. spin_unlock(&ls->ls_recover_lock);
  172. } else {
  173. dlm_set_recover_status(ls, DLM_RS_NODES_ALL);
  174. }
  175. } else {
  176. error = wait_status_low(ls, DLM_RS_NODES_ALL, DLM_RSF_NEED_SLOTS);
  177. if (error)
  178. goto out;
  179. dlm_slots_copy_in(ls);
  180. }
  181. out:
  182. return error;
  183. }
  184. int dlm_recover_directory_wait(struct dlm_ls *ls)
  185. {
  186. return wait_status(ls, DLM_RS_DIR);
  187. }
  188. int dlm_recover_locks_wait(struct dlm_ls *ls)
  189. {
  190. return wait_status(ls, DLM_RS_LOCKS);
  191. }
  192. int dlm_recover_done_wait(struct dlm_ls *ls)
  193. {
  194. return wait_status(ls, DLM_RS_DONE);
  195. }
  196. /*
  197. * The recover_list contains all the rsb's for which we've requested the new
  198. * master nodeid. As replies are returned from the resource directories the
  199. * rsb's are removed from the list. When the list is empty we're done.
  200. *
  201. * The recover_list is later similarly used for all rsb's for which we've sent
  202. * new lkb's and need to receive new corresponding lkid's.
  203. *
  204. * We use the address of the rsb struct as a simple local identifier for the
  205. * rsb so we can match an rcom reply with the rsb it was sent for.
  206. */
  207. static int recover_list_empty(struct dlm_ls *ls)
  208. {
  209. int empty;
  210. spin_lock(&ls->ls_recover_list_lock);
  211. empty = list_empty(&ls->ls_recover_list);
  212. spin_unlock(&ls->ls_recover_list_lock);
  213. return empty;
  214. }
  215. static void recover_list_add(struct dlm_rsb *r)
  216. {
  217. struct dlm_ls *ls = r->res_ls;
  218. spin_lock(&ls->ls_recover_list_lock);
  219. if (list_empty(&r->res_recover_list)) {
  220. list_add_tail(&r->res_recover_list, &ls->ls_recover_list);
  221. ls->ls_recover_list_count++;
  222. dlm_hold_rsb(r);
  223. }
  224. spin_unlock(&ls->ls_recover_list_lock);
  225. }
  226. static void recover_list_del(struct dlm_rsb *r)
  227. {
  228. struct dlm_ls *ls = r->res_ls;
  229. spin_lock(&ls->ls_recover_list_lock);
  230. list_del_init(&r->res_recover_list);
  231. ls->ls_recover_list_count--;
  232. spin_unlock(&ls->ls_recover_list_lock);
  233. dlm_put_rsb(r);
  234. }
  235. static void recover_list_clear(struct dlm_ls *ls)
  236. {
  237. struct dlm_rsb *r, *s;
  238. spin_lock(&ls->ls_recover_list_lock);
  239. list_for_each_entry_safe(r, s, &ls->ls_recover_list, res_recover_list) {
  240. list_del_init(&r->res_recover_list);
  241. r->res_recover_locks_count = 0;
  242. dlm_put_rsb(r);
  243. ls->ls_recover_list_count--;
  244. }
  245. if (ls->ls_recover_list_count != 0) {
  246. log_error(ls, "warning: recover_list_count %d",
  247. ls->ls_recover_list_count);
  248. ls->ls_recover_list_count = 0;
  249. }
  250. spin_unlock(&ls->ls_recover_list_lock);
  251. }
  252. static int recover_idr_empty(struct dlm_ls *ls)
  253. {
  254. int empty = 1;
  255. spin_lock(&ls->ls_recover_idr_lock);
  256. if (ls->ls_recover_list_count)
  257. empty = 0;
  258. spin_unlock(&ls->ls_recover_idr_lock);
  259. return empty;
  260. }
  261. static int recover_idr_add(struct dlm_rsb *r)
  262. {
  263. struct dlm_ls *ls = r->res_ls;
  264. int rv;
  265. idr_preload(GFP_NOFS);
  266. spin_lock(&ls->ls_recover_idr_lock);
  267. if (r->res_id) {
  268. rv = -1;
  269. goto out_unlock;
  270. }
  271. rv = idr_alloc(&ls->ls_recover_idr, r, 1, 0, GFP_NOWAIT);
  272. if (rv < 0)
  273. goto out_unlock;
  274. r->res_id = rv;
  275. ls->ls_recover_list_count++;
  276. dlm_hold_rsb(r);
  277. rv = 0;
  278. out_unlock:
  279. spin_unlock(&ls->ls_recover_idr_lock);
  280. idr_preload_end();
  281. return rv;
  282. }
  283. static void recover_idr_del(struct dlm_rsb *r)
  284. {
  285. struct dlm_ls *ls = r->res_ls;
  286. spin_lock(&ls->ls_recover_idr_lock);
  287. idr_remove(&ls->ls_recover_idr, r->res_id);
  288. r->res_id = 0;
  289. ls->ls_recover_list_count--;
  290. spin_unlock(&ls->ls_recover_idr_lock);
  291. dlm_put_rsb(r);
  292. }
  293. static struct dlm_rsb *recover_idr_find(struct dlm_ls *ls, uint64_t id)
  294. {
  295. struct dlm_rsb *r;
  296. spin_lock(&ls->ls_recover_idr_lock);
  297. r = idr_find(&ls->ls_recover_idr, (int)id);
  298. spin_unlock(&ls->ls_recover_idr_lock);
  299. return r;
  300. }
  301. static void recover_idr_clear(struct dlm_ls *ls)
  302. {
  303. struct dlm_rsb *r;
  304. int id;
  305. spin_lock(&ls->ls_recover_idr_lock);
  306. idr_for_each_entry(&ls->ls_recover_idr, r, id) {
  307. idr_remove(&ls->ls_recover_idr, id);
  308. r->res_id = 0;
  309. r->res_recover_locks_count = 0;
  310. ls->ls_recover_list_count--;
  311. dlm_put_rsb(r);
  312. }
  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_rinfo(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_rinfo(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_rinfo(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 in two cases:
  600. *
  601. * 1. we are master, but not new, and we purged an EX/PW lock held by a
  602. * failed node (in dlm_recover_purge which set RSB_RECOVER_LVB_INVAL)
  603. *
  604. * 2. we are a new master, and there are only NL/CR locks left.
  605. * (We could probably improve this by only invaliding in this way when
  606. * the previous master left uncleanly. VMS docs mention that.)
  607. *
  608. * The LVB contents are only considered for changing when this is a new master
  609. * of the rsb (NEW_MASTER2). Then, the rsb's lvb is taken from any lkb with
  610. * mode > CR. If no lkb's exist with mode above CR, the lvb contents are taken
  611. * from the lkb with the largest lvb sequence number.
  612. */
  613. static void recover_lvb(struct dlm_rsb *r)
  614. {
  615. struct dlm_lkb *lkb, *high_lkb = NULL;
  616. uint32_t high_seq = 0;
  617. int lock_lvb_exists = 0;
  618. int big_lock_exists = 0;
  619. int lvblen = r->res_ls->ls_lvblen;
  620. if (!rsb_flag(r, RSB_NEW_MASTER2) &&
  621. rsb_flag(r, RSB_RECOVER_LVB_INVAL)) {
  622. /* case 1 above */
  623. rsb_set_flag(r, RSB_VALNOTVALID);
  624. return;
  625. }
  626. if (!rsb_flag(r, RSB_NEW_MASTER2))
  627. return;
  628. /* we are the new master, so figure out if VALNOTVALID should
  629. be set, and set the rsb lvb from the best lkb available. */
  630. list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue) {
  631. if (!(lkb->lkb_exflags & DLM_LKF_VALBLK))
  632. continue;
  633. lock_lvb_exists = 1;
  634. if (lkb->lkb_grmode > DLM_LOCK_CR) {
  635. big_lock_exists = 1;
  636. goto setflag;
  637. }
  638. if (((int)lkb->lkb_lvbseq - (int)high_seq) >= 0) {
  639. high_lkb = lkb;
  640. high_seq = lkb->lkb_lvbseq;
  641. }
  642. }
  643. list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue) {
  644. if (!(lkb->lkb_exflags & DLM_LKF_VALBLK))
  645. continue;
  646. lock_lvb_exists = 1;
  647. if (lkb->lkb_grmode > DLM_LOCK_CR) {
  648. big_lock_exists = 1;
  649. goto setflag;
  650. }
  651. if (((int)lkb->lkb_lvbseq - (int)high_seq) >= 0) {
  652. high_lkb = lkb;
  653. high_seq = lkb->lkb_lvbseq;
  654. }
  655. }
  656. setflag:
  657. if (!lock_lvb_exists)
  658. goto out;
  659. /* lvb is invalidated if only NL/CR locks remain */
  660. if (!big_lock_exists)
  661. rsb_set_flag(r, RSB_VALNOTVALID);
  662. if (!r->res_lvbptr) {
  663. r->res_lvbptr = dlm_allocate_lvb(r->res_ls);
  664. if (!r->res_lvbptr)
  665. goto out;
  666. }
  667. if (big_lock_exists) {
  668. r->res_lvbseq = lkb->lkb_lvbseq;
  669. memcpy(r->res_lvbptr, lkb->lkb_lvbptr, lvblen);
  670. } else if (high_lkb) {
  671. r->res_lvbseq = high_lkb->lkb_lvbseq;
  672. memcpy(r->res_lvbptr, high_lkb->lkb_lvbptr, lvblen);
  673. } else {
  674. r->res_lvbseq = 0;
  675. memset(r->res_lvbptr, 0, lvblen);
  676. }
  677. out:
  678. return;
  679. }
  680. /* All master rsb's flagged RECOVER_CONVERT need to be looked at. The locks
  681. converting PR->CW or CW->PR need to have their lkb_grmode set. */
  682. static void recover_conversion(struct dlm_rsb *r)
  683. {
  684. struct dlm_ls *ls = r->res_ls;
  685. struct dlm_lkb *lkb;
  686. int grmode = -1;
  687. list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue) {
  688. if (lkb->lkb_grmode == DLM_LOCK_PR ||
  689. lkb->lkb_grmode == DLM_LOCK_CW) {
  690. grmode = lkb->lkb_grmode;
  691. break;
  692. }
  693. }
  694. list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue) {
  695. if (lkb->lkb_grmode != DLM_LOCK_IV)
  696. continue;
  697. if (grmode == -1) {
  698. log_debug(ls, "recover_conversion %x set gr to rq %d",
  699. lkb->lkb_id, lkb->lkb_rqmode);
  700. lkb->lkb_grmode = lkb->lkb_rqmode;
  701. } else {
  702. log_debug(ls, "recover_conversion %x set gr %d",
  703. lkb->lkb_id, grmode);
  704. lkb->lkb_grmode = grmode;
  705. }
  706. }
  707. }
  708. /* We've become the new master for this rsb and waiting/converting locks may
  709. need to be granted in dlm_recover_grant() due to locks that may have
  710. existed from a removed node. */
  711. static void recover_grant(struct dlm_rsb *r)
  712. {
  713. if (!list_empty(&r->res_waitqueue) || !list_empty(&r->res_convertqueue))
  714. rsb_set_flag(r, RSB_RECOVER_GRANT);
  715. }
  716. void dlm_recover_rsbs(struct dlm_ls *ls)
  717. {
  718. struct dlm_rsb *r;
  719. unsigned int count = 0;
  720. down_read(&ls->ls_root_sem);
  721. list_for_each_entry(r, &ls->ls_root_list, res_root_list) {
  722. lock_rsb(r);
  723. if (is_master(r)) {
  724. if (rsb_flag(r, RSB_RECOVER_CONVERT))
  725. recover_conversion(r);
  726. /* recover lvb before granting locks so the updated
  727. lvb/VALNOTVALID is presented in the completion */
  728. recover_lvb(r);
  729. if (rsb_flag(r, RSB_NEW_MASTER2))
  730. recover_grant(r);
  731. count++;
  732. } else {
  733. rsb_clear_flag(r, RSB_VALNOTVALID);
  734. }
  735. rsb_clear_flag(r, RSB_RECOVER_CONVERT);
  736. rsb_clear_flag(r, RSB_RECOVER_LVB_INVAL);
  737. rsb_clear_flag(r, RSB_NEW_MASTER2);
  738. unlock_rsb(r);
  739. }
  740. up_read(&ls->ls_root_sem);
  741. if (count)
  742. log_rinfo(ls, "dlm_recover_rsbs %d done", count);
  743. }
  744. /* Create a single list of all root rsb's to be used during recovery */
  745. int dlm_create_root_list(struct dlm_ls *ls)
  746. {
  747. struct rb_node *n;
  748. struct dlm_rsb *r;
  749. int i, error = 0;
  750. down_write(&ls->ls_root_sem);
  751. if (!list_empty(&ls->ls_root_list)) {
  752. log_error(ls, "root list not empty");
  753. error = -EINVAL;
  754. goto out;
  755. }
  756. for (i = 0; i < ls->ls_rsbtbl_size; i++) {
  757. spin_lock(&ls->ls_rsbtbl[i].lock);
  758. for (n = rb_first(&ls->ls_rsbtbl[i].keep); n; n = rb_next(n)) {
  759. r = rb_entry(n, struct dlm_rsb, res_hashnode);
  760. list_add(&r->res_root_list, &ls->ls_root_list);
  761. dlm_hold_rsb(r);
  762. }
  763. if (!RB_EMPTY_ROOT(&ls->ls_rsbtbl[i].toss))
  764. log_error(ls, "dlm_create_root_list toss not empty");
  765. spin_unlock(&ls->ls_rsbtbl[i].lock);
  766. }
  767. out:
  768. up_write(&ls->ls_root_sem);
  769. return error;
  770. }
  771. void dlm_release_root_list(struct dlm_ls *ls)
  772. {
  773. struct dlm_rsb *r, *safe;
  774. down_write(&ls->ls_root_sem);
  775. list_for_each_entry_safe(r, safe, &ls->ls_root_list, res_root_list) {
  776. list_del_init(&r->res_root_list);
  777. dlm_put_rsb(r);
  778. }
  779. up_write(&ls->ls_root_sem);
  780. }
  781. void dlm_clear_toss(struct dlm_ls *ls)
  782. {
  783. struct rb_node *n, *next;
  784. struct dlm_rsb *r;
  785. unsigned int count = 0;
  786. int i;
  787. for (i = 0; i < ls->ls_rsbtbl_size; i++) {
  788. spin_lock(&ls->ls_rsbtbl[i].lock);
  789. for (n = rb_first(&ls->ls_rsbtbl[i].toss); n; n = next) {
  790. next = rb_next(n);
  791. r = rb_entry(n, struct dlm_rsb, res_hashnode);
  792. rb_erase(n, &ls->ls_rsbtbl[i].toss);
  793. dlm_free_rsb(r);
  794. count++;
  795. }
  796. spin_unlock(&ls->ls_rsbtbl[i].lock);
  797. }
  798. if (count)
  799. log_rinfo(ls, "dlm_clear_toss %u done", count);
  800. }