recover.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  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;
  261. idr_preload(GFP_NOFS);
  262. spin_lock(&ls->ls_recover_idr_lock);
  263. if (r->res_id) {
  264. rv = -1;
  265. goto out_unlock;
  266. }
  267. rv = idr_alloc(&ls->ls_recover_idr, r, 1, 0, GFP_NOWAIT);
  268. if (rv < 0)
  269. goto out_unlock;
  270. r->res_id = rv;
  271. ls->ls_recover_list_count++;
  272. dlm_hold_rsb(r);
  273. rv = 0;
  274. out_unlock:
  275. spin_unlock(&ls->ls_recover_idr_lock);
  276. idr_preload_end();
  277. return rv;
  278. }
  279. static void recover_idr_del(struct dlm_rsb *r)
  280. {
  281. struct dlm_ls *ls = r->res_ls;
  282. spin_lock(&ls->ls_recover_idr_lock);
  283. idr_remove(&ls->ls_recover_idr, r->res_id);
  284. r->res_id = 0;
  285. ls->ls_recover_list_count--;
  286. spin_unlock(&ls->ls_recover_idr_lock);
  287. dlm_put_rsb(r);
  288. }
  289. static struct dlm_rsb *recover_idr_find(struct dlm_ls *ls, uint64_t id)
  290. {
  291. struct dlm_rsb *r;
  292. spin_lock(&ls->ls_recover_idr_lock);
  293. r = idr_find(&ls->ls_recover_idr, (int)id);
  294. spin_unlock(&ls->ls_recover_idr_lock);
  295. return r;
  296. }
  297. static void recover_idr_clear(struct dlm_ls *ls)
  298. {
  299. struct dlm_rsb *r;
  300. int id;
  301. spin_lock(&ls->ls_recover_idr_lock);
  302. idr_for_each_entry(&ls->ls_recover_idr, r, id) {
  303. idr_remove(&ls->ls_recover_idr, id);
  304. r->res_id = 0;
  305. r->res_recover_locks_count = 0;
  306. ls->ls_recover_list_count--;
  307. dlm_put_rsb(r);
  308. }
  309. if (ls->ls_recover_list_count != 0) {
  310. log_error(ls, "warning: recover_list_count %d",
  311. ls->ls_recover_list_count);
  312. ls->ls_recover_list_count = 0;
  313. }
  314. spin_unlock(&ls->ls_recover_idr_lock);
  315. }
  316. /* Master recovery: find new master node for rsb's that were
  317. mastered on nodes that have been removed.
  318. dlm_recover_masters
  319. recover_master
  320. dlm_send_rcom_lookup -> receive_rcom_lookup
  321. dlm_dir_lookup
  322. receive_rcom_lookup_reply <-
  323. dlm_recover_master_reply
  324. set_new_master
  325. set_master_lkbs
  326. set_lock_master
  327. */
  328. /*
  329. * Set the lock master for all LKBs in a lock queue
  330. * If we are the new master of the rsb, we may have received new
  331. * MSTCPY locks from other nodes already which we need to ignore
  332. * when setting the new nodeid.
  333. */
  334. static void set_lock_master(struct list_head *queue, int nodeid)
  335. {
  336. struct dlm_lkb *lkb;
  337. list_for_each_entry(lkb, queue, lkb_statequeue) {
  338. if (!(lkb->lkb_flags & DLM_IFL_MSTCPY)) {
  339. lkb->lkb_nodeid = nodeid;
  340. lkb->lkb_remid = 0;
  341. }
  342. }
  343. }
  344. static void set_master_lkbs(struct dlm_rsb *r)
  345. {
  346. set_lock_master(&r->res_grantqueue, r->res_nodeid);
  347. set_lock_master(&r->res_convertqueue, r->res_nodeid);
  348. set_lock_master(&r->res_waitqueue, r->res_nodeid);
  349. }
  350. /*
  351. * Propagate the new master nodeid to locks
  352. * The NEW_MASTER flag tells dlm_recover_locks() which rsb's to consider.
  353. * The NEW_MASTER2 flag tells recover_lvb() and recover_grant() which
  354. * rsb's to consider.
  355. */
  356. static void set_new_master(struct dlm_rsb *r)
  357. {
  358. set_master_lkbs(r);
  359. rsb_set_flag(r, RSB_NEW_MASTER);
  360. rsb_set_flag(r, RSB_NEW_MASTER2);
  361. }
  362. /*
  363. * We do async lookups on rsb's that need new masters. The rsb's
  364. * waiting for a lookup reply are kept on the recover_list.
  365. *
  366. * Another node recovering the master may have sent us a rcom lookup,
  367. * and our dlm_master_lookup() set it as the new master, along with
  368. * NEW_MASTER so that we'll recover it here (this implies dir_nodeid
  369. * equals our_nodeid below).
  370. */
  371. static int recover_master(struct dlm_rsb *r, unsigned int *count)
  372. {
  373. struct dlm_ls *ls = r->res_ls;
  374. int our_nodeid, dir_nodeid;
  375. int is_removed = 0;
  376. int error;
  377. if (is_master(r))
  378. return 0;
  379. is_removed = dlm_is_removed(ls, r->res_nodeid);
  380. if (!is_removed && !rsb_flag(r, RSB_NEW_MASTER))
  381. return 0;
  382. our_nodeid = dlm_our_nodeid();
  383. dir_nodeid = dlm_dir_nodeid(r);
  384. if (dir_nodeid == our_nodeid) {
  385. if (is_removed) {
  386. r->res_master_nodeid = our_nodeid;
  387. r->res_nodeid = 0;
  388. }
  389. /* set master of lkbs to ourself when is_removed, or to
  390. another new master which we set along with NEW_MASTER
  391. in dlm_master_lookup */
  392. set_new_master(r);
  393. error = 0;
  394. } else {
  395. recover_idr_add(r);
  396. error = dlm_send_rcom_lookup(r, dir_nodeid);
  397. }
  398. (*count)++;
  399. return error;
  400. }
  401. /*
  402. * All MSTCPY locks are purged and rebuilt, even if the master stayed the same.
  403. * This is necessary because recovery can be started, aborted and restarted,
  404. * causing the master nodeid to briefly change during the aborted recovery, and
  405. * change back to the original value in the second recovery. The MSTCPY locks
  406. * may or may not have been purged during the aborted recovery. Another node
  407. * with an outstanding request in waiters list and a request reply saved in the
  408. * requestqueue, cannot know whether it should ignore the reply and resend the
  409. * request, or accept the reply and complete the request. It must do the
  410. * former if the remote node purged MSTCPY locks, and it must do the later if
  411. * the remote node did not. This is solved by always purging MSTCPY locks, in
  412. * which case, the request reply would always be ignored and the request
  413. * resent.
  414. */
  415. static int recover_master_static(struct dlm_rsb *r, unsigned int *count)
  416. {
  417. int dir_nodeid = dlm_dir_nodeid(r);
  418. int new_master = dir_nodeid;
  419. if (dir_nodeid == dlm_our_nodeid())
  420. new_master = 0;
  421. dlm_purge_mstcpy_locks(r);
  422. r->res_master_nodeid = dir_nodeid;
  423. r->res_nodeid = new_master;
  424. set_new_master(r);
  425. (*count)++;
  426. return 0;
  427. }
  428. /*
  429. * Go through local root resources and for each rsb which has a master which
  430. * has departed, get the new master nodeid from the directory. The dir will
  431. * assign mastery to the first node to look up the new master. That means
  432. * we'll discover in this lookup if we're the new master of any rsb's.
  433. *
  434. * We fire off all the dir lookup requests individually and asynchronously to
  435. * the correct dir node.
  436. */
  437. int dlm_recover_masters(struct dlm_ls *ls)
  438. {
  439. struct dlm_rsb *r;
  440. unsigned int total = 0;
  441. unsigned int count = 0;
  442. int nodir = dlm_no_directory(ls);
  443. int error;
  444. log_rinfo(ls, "dlm_recover_masters");
  445. down_read(&ls->ls_root_sem);
  446. list_for_each_entry(r, &ls->ls_root_list, res_root_list) {
  447. if (dlm_recovery_stopped(ls)) {
  448. up_read(&ls->ls_root_sem);
  449. error = -EINTR;
  450. goto out;
  451. }
  452. lock_rsb(r);
  453. if (nodir)
  454. error = recover_master_static(r, &count);
  455. else
  456. error = recover_master(r, &count);
  457. unlock_rsb(r);
  458. cond_resched();
  459. total++;
  460. if (error) {
  461. up_read(&ls->ls_root_sem);
  462. goto out;
  463. }
  464. }
  465. up_read(&ls->ls_root_sem);
  466. log_rinfo(ls, "dlm_recover_masters %u of %u", count, total);
  467. error = dlm_wait_function(ls, &recover_idr_empty);
  468. out:
  469. if (error)
  470. recover_idr_clear(ls);
  471. return error;
  472. }
  473. int dlm_recover_master_reply(struct dlm_ls *ls, struct dlm_rcom *rc)
  474. {
  475. struct dlm_rsb *r;
  476. int ret_nodeid, new_master;
  477. r = recover_idr_find(ls, rc->rc_id);
  478. if (!r) {
  479. log_error(ls, "dlm_recover_master_reply no id %llx",
  480. (unsigned long long)rc->rc_id);
  481. goto out;
  482. }
  483. ret_nodeid = rc->rc_result;
  484. if (ret_nodeid == dlm_our_nodeid())
  485. new_master = 0;
  486. else
  487. new_master = ret_nodeid;
  488. lock_rsb(r);
  489. r->res_master_nodeid = ret_nodeid;
  490. r->res_nodeid = new_master;
  491. set_new_master(r);
  492. unlock_rsb(r);
  493. recover_idr_del(r);
  494. if (recover_idr_empty(ls))
  495. wake_up(&ls->ls_wait_general);
  496. out:
  497. return 0;
  498. }
  499. /* Lock recovery: rebuild the process-copy locks we hold on a
  500. remastered rsb on the new rsb master.
  501. dlm_recover_locks
  502. recover_locks
  503. recover_locks_queue
  504. dlm_send_rcom_lock -> receive_rcom_lock
  505. dlm_recover_master_copy
  506. receive_rcom_lock_reply <-
  507. dlm_recover_process_copy
  508. */
  509. /*
  510. * keep a count of the number of lkb's we send to the new master; when we get
  511. * an equal number of replies then recovery for the rsb is done
  512. */
  513. static int recover_locks_queue(struct dlm_rsb *r, struct list_head *head)
  514. {
  515. struct dlm_lkb *lkb;
  516. int error = 0;
  517. list_for_each_entry(lkb, head, lkb_statequeue) {
  518. error = dlm_send_rcom_lock(r, lkb);
  519. if (error)
  520. break;
  521. r->res_recover_locks_count++;
  522. }
  523. return error;
  524. }
  525. static int recover_locks(struct dlm_rsb *r)
  526. {
  527. int error = 0;
  528. lock_rsb(r);
  529. DLM_ASSERT(!r->res_recover_locks_count, dlm_dump_rsb(r););
  530. error = recover_locks_queue(r, &r->res_grantqueue);
  531. if (error)
  532. goto out;
  533. error = recover_locks_queue(r, &r->res_convertqueue);
  534. if (error)
  535. goto out;
  536. error = recover_locks_queue(r, &r->res_waitqueue);
  537. if (error)
  538. goto out;
  539. if (r->res_recover_locks_count)
  540. recover_list_add(r);
  541. else
  542. rsb_clear_flag(r, RSB_NEW_MASTER);
  543. out:
  544. unlock_rsb(r);
  545. return error;
  546. }
  547. int dlm_recover_locks(struct dlm_ls *ls)
  548. {
  549. struct dlm_rsb *r;
  550. int error, count = 0;
  551. down_read(&ls->ls_root_sem);
  552. list_for_each_entry(r, &ls->ls_root_list, res_root_list) {
  553. if (is_master(r)) {
  554. rsb_clear_flag(r, RSB_NEW_MASTER);
  555. continue;
  556. }
  557. if (!rsb_flag(r, RSB_NEW_MASTER))
  558. continue;
  559. if (dlm_recovery_stopped(ls)) {
  560. error = -EINTR;
  561. up_read(&ls->ls_root_sem);
  562. goto out;
  563. }
  564. error = recover_locks(r);
  565. if (error) {
  566. up_read(&ls->ls_root_sem);
  567. goto out;
  568. }
  569. count += r->res_recover_locks_count;
  570. }
  571. up_read(&ls->ls_root_sem);
  572. log_rinfo(ls, "dlm_recover_locks %d out", count);
  573. error = dlm_wait_function(ls, &recover_list_empty);
  574. out:
  575. if (error)
  576. recover_list_clear(ls);
  577. return error;
  578. }
  579. void dlm_recovered_lock(struct dlm_rsb *r)
  580. {
  581. DLM_ASSERT(rsb_flag(r, RSB_NEW_MASTER), dlm_dump_rsb(r););
  582. r->res_recover_locks_count--;
  583. if (!r->res_recover_locks_count) {
  584. rsb_clear_flag(r, RSB_NEW_MASTER);
  585. recover_list_del(r);
  586. }
  587. if (recover_list_empty(r->res_ls))
  588. wake_up(&r->res_ls->ls_wait_general);
  589. }
  590. /*
  591. * The lvb needs to be recovered on all master rsb's. This includes setting
  592. * the VALNOTVALID flag if necessary, and determining the correct lvb contents
  593. * based on the lvb's of the locks held on the rsb.
  594. *
  595. * RSB_VALNOTVALID is set in two cases:
  596. *
  597. * 1. we are master, but not new, and we purged an EX/PW lock held by a
  598. * failed node (in dlm_recover_purge which set RSB_RECOVER_LVB_INVAL)
  599. *
  600. * 2. we are a new master, and there are only NL/CR locks left.
  601. * (We could probably improve this by only invaliding in this way when
  602. * the previous master left uncleanly. VMS docs mention that.)
  603. *
  604. * The LVB contents are only considered for changing when this is a new master
  605. * of the rsb (NEW_MASTER2). Then, the rsb's lvb is taken from any lkb with
  606. * mode > CR. If no lkb's exist with mode above CR, the lvb contents are taken
  607. * from the lkb with the largest lvb sequence number.
  608. */
  609. static void recover_lvb(struct dlm_rsb *r)
  610. {
  611. struct dlm_lkb *lkb, *high_lkb = NULL;
  612. uint32_t high_seq = 0;
  613. int lock_lvb_exists = 0;
  614. int big_lock_exists = 0;
  615. int lvblen = r->res_ls->ls_lvblen;
  616. if (!rsb_flag(r, RSB_NEW_MASTER2) &&
  617. rsb_flag(r, RSB_RECOVER_LVB_INVAL)) {
  618. /* case 1 above */
  619. rsb_set_flag(r, RSB_VALNOTVALID);
  620. return;
  621. }
  622. if (!rsb_flag(r, RSB_NEW_MASTER2))
  623. return;
  624. /* we are the new master, so figure out if VALNOTVALID should
  625. be set, and set the rsb lvb from the best lkb available. */
  626. list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue) {
  627. if (!(lkb->lkb_exflags & DLM_LKF_VALBLK))
  628. continue;
  629. lock_lvb_exists = 1;
  630. if (lkb->lkb_grmode > DLM_LOCK_CR) {
  631. big_lock_exists = 1;
  632. goto setflag;
  633. }
  634. if (((int)lkb->lkb_lvbseq - (int)high_seq) >= 0) {
  635. high_lkb = lkb;
  636. high_seq = lkb->lkb_lvbseq;
  637. }
  638. }
  639. list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue) {
  640. if (!(lkb->lkb_exflags & DLM_LKF_VALBLK))
  641. continue;
  642. lock_lvb_exists = 1;
  643. if (lkb->lkb_grmode > DLM_LOCK_CR) {
  644. big_lock_exists = 1;
  645. goto setflag;
  646. }
  647. if (((int)lkb->lkb_lvbseq - (int)high_seq) >= 0) {
  648. high_lkb = lkb;
  649. high_seq = lkb->lkb_lvbseq;
  650. }
  651. }
  652. setflag:
  653. if (!lock_lvb_exists)
  654. goto out;
  655. /* lvb is invalidated if only NL/CR locks remain */
  656. if (!big_lock_exists)
  657. rsb_set_flag(r, RSB_VALNOTVALID);
  658. if (!r->res_lvbptr) {
  659. r->res_lvbptr = dlm_allocate_lvb(r->res_ls);
  660. if (!r->res_lvbptr)
  661. goto out;
  662. }
  663. if (big_lock_exists) {
  664. r->res_lvbseq = lkb->lkb_lvbseq;
  665. memcpy(r->res_lvbptr, lkb->lkb_lvbptr, lvblen);
  666. } else if (high_lkb) {
  667. r->res_lvbseq = high_lkb->lkb_lvbseq;
  668. memcpy(r->res_lvbptr, high_lkb->lkb_lvbptr, lvblen);
  669. } else {
  670. r->res_lvbseq = 0;
  671. memset(r->res_lvbptr, 0, lvblen);
  672. }
  673. out:
  674. return;
  675. }
  676. /* All master rsb's flagged RECOVER_CONVERT need to be looked at. The locks
  677. converting PR->CW or CW->PR need to have their lkb_grmode set. */
  678. static void recover_conversion(struct dlm_rsb *r)
  679. {
  680. struct dlm_ls *ls = r->res_ls;
  681. struct dlm_lkb *lkb;
  682. int grmode = -1;
  683. list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue) {
  684. if (lkb->lkb_grmode == DLM_LOCK_PR ||
  685. lkb->lkb_grmode == DLM_LOCK_CW) {
  686. grmode = lkb->lkb_grmode;
  687. break;
  688. }
  689. }
  690. list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue) {
  691. if (lkb->lkb_grmode != DLM_LOCK_IV)
  692. continue;
  693. if (grmode == -1) {
  694. log_debug(ls, "recover_conversion %x set gr to rq %d",
  695. lkb->lkb_id, lkb->lkb_rqmode);
  696. lkb->lkb_grmode = lkb->lkb_rqmode;
  697. } else {
  698. log_debug(ls, "recover_conversion %x set gr %d",
  699. lkb->lkb_id, grmode);
  700. lkb->lkb_grmode = grmode;
  701. }
  702. }
  703. }
  704. /* We've become the new master for this rsb and waiting/converting locks may
  705. need to be granted in dlm_recover_grant() due to locks that may have
  706. existed from a removed node. */
  707. static void recover_grant(struct dlm_rsb *r)
  708. {
  709. if (!list_empty(&r->res_waitqueue) || !list_empty(&r->res_convertqueue))
  710. rsb_set_flag(r, RSB_RECOVER_GRANT);
  711. }
  712. void dlm_recover_rsbs(struct dlm_ls *ls)
  713. {
  714. struct dlm_rsb *r;
  715. unsigned int count = 0;
  716. down_read(&ls->ls_root_sem);
  717. list_for_each_entry(r, &ls->ls_root_list, res_root_list) {
  718. lock_rsb(r);
  719. if (is_master(r)) {
  720. if (rsb_flag(r, RSB_RECOVER_CONVERT))
  721. recover_conversion(r);
  722. /* recover lvb before granting locks so the updated
  723. lvb/VALNOTVALID is presented in the completion */
  724. recover_lvb(r);
  725. if (rsb_flag(r, RSB_NEW_MASTER2))
  726. recover_grant(r);
  727. count++;
  728. } else {
  729. rsb_clear_flag(r, RSB_VALNOTVALID);
  730. }
  731. rsb_clear_flag(r, RSB_RECOVER_CONVERT);
  732. rsb_clear_flag(r, RSB_RECOVER_LVB_INVAL);
  733. rsb_clear_flag(r, RSB_NEW_MASTER2);
  734. unlock_rsb(r);
  735. }
  736. up_read(&ls->ls_root_sem);
  737. if (count)
  738. log_rinfo(ls, "dlm_recover_rsbs %d done", count);
  739. }
  740. /* Create a single list of all root rsb's to be used during recovery */
  741. int dlm_create_root_list(struct dlm_ls *ls)
  742. {
  743. struct rb_node *n;
  744. struct dlm_rsb *r;
  745. int i, error = 0;
  746. down_write(&ls->ls_root_sem);
  747. if (!list_empty(&ls->ls_root_list)) {
  748. log_error(ls, "root list not empty");
  749. error = -EINVAL;
  750. goto out;
  751. }
  752. for (i = 0; i < ls->ls_rsbtbl_size; i++) {
  753. spin_lock(&ls->ls_rsbtbl[i].lock);
  754. for (n = rb_first(&ls->ls_rsbtbl[i].keep); n; n = rb_next(n)) {
  755. r = rb_entry(n, struct dlm_rsb, res_hashnode);
  756. list_add(&r->res_root_list, &ls->ls_root_list);
  757. dlm_hold_rsb(r);
  758. }
  759. if (!RB_EMPTY_ROOT(&ls->ls_rsbtbl[i].toss))
  760. log_error(ls, "dlm_create_root_list toss not empty");
  761. spin_unlock(&ls->ls_rsbtbl[i].lock);
  762. }
  763. out:
  764. up_write(&ls->ls_root_sem);
  765. return error;
  766. }
  767. void dlm_release_root_list(struct dlm_ls *ls)
  768. {
  769. struct dlm_rsb *r, *safe;
  770. down_write(&ls->ls_root_sem);
  771. list_for_each_entry_safe(r, safe, &ls->ls_root_list, res_root_list) {
  772. list_del_init(&r->res_root_list);
  773. dlm_put_rsb(r);
  774. }
  775. up_write(&ls->ls_root_sem);
  776. }
  777. void dlm_clear_toss(struct dlm_ls *ls)
  778. {
  779. struct rb_node *n, *next;
  780. struct dlm_rsb *r;
  781. unsigned int count = 0;
  782. int i;
  783. for (i = 0; i < ls->ls_rsbtbl_size; i++) {
  784. spin_lock(&ls->ls_rsbtbl[i].lock);
  785. for (n = rb_first(&ls->ls_rsbtbl[i].toss); n; n = next) {
  786. next = rb_next(n);
  787. r = rb_entry(n, struct dlm_rsb, res_hashnode);
  788. rb_erase(n, &ls->ls_rsbtbl[i].toss);
  789. dlm_free_rsb(r);
  790. count++;
  791. }
  792. spin_unlock(&ls->ls_rsbtbl[i].lock);
  793. }
  794. if (count)
  795. log_rinfo(ls, "dlm_clear_toss %u done", count);
  796. }