dlmthread.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * dlmthread.c
  4. *
  5. * standalone DLM module
  6. *
  7. * Copyright (C) 2004 Oracle. All rights reserved.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/fs.h>
  11. #include <linux/types.h>
  12. #include <linux/highmem.h>
  13. #include <linux/init.h>
  14. #include <linux/sysctl.h>
  15. #include <linux/random.h>
  16. #include <linux/blkdev.h>
  17. #include <linux/socket.h>
  18. #include <linux/inet.h>
  19. #include <linux/timer.h>
  20. #include <linux/kthread.h>
  21. #include <linux/delay.h>
  22. #include "../cluster/heartbeat.h"
  23. #include "../cluster/nodemanager.h"
  24. #include "../cluster/tcp.h"
  25. #include "dlmapi.h"
  26. #include "dlmcommon.h"
  27. #include "dlmdomain.h"
  28. #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_THREAD)
  29. #include "../cluster/masklog.h"
  30. static int dlm_thread(void *data);
  31. static void dlm_flush_asts(struct dlm_ctxt *dlm);
  32. /* will exit holding res->spinlock, but may drop in function */
  33. /* waits until flags are cleared on res->state */
  34. void __dlm_wait_on_lockres_flags(struct dlm_lock_resource *res, int flags)
  35. {
  36. DECLARE_WAITQUEUE(wait, current);
  37. assert_spin_locked(&res->spinlock);
  38. add_wait_queue(&res->wq, &wait);
  39. repeat:
  40. set_current_state(TASK_UNINTERRUPTIBLE);
  41. if (res->state & flags) {
  42. spin_unlock(&res->spinlock);
  43. schedule();
  44. spin_lock(&res->spinlock);
  45. goto repeat;
  46. }
  47. remove_wait_queue(&res->wq, &wait);
  48. __set_current_state(TASK_RUNNING);
  49. }
  50. int __dlm_lockres_has_locks(struct dlm_lock_resource *res)
  51. {
  52. if (list_empty(&res->granted) &&
  53. list_empty(&res->converting) &&
  54. list_empty(&res->blocked))
  55. return 0;
  56. return 1;
  57. }
  58. /* "unused": the lockres has no locks, is not on the dirty list,
  59. * has no inflight locks (in the gap between mastery and acquiring
  60. * the first lock), and has no bits in its refmap.
  61. * truly ready to be freed. */
  62. int __dlm_lockres_unused(struct dlm_lock_resource *res)
  63. {
  64. int bit;
  65. assert_spin_locked(&res->spinlock);
  66. if (__dlm_lockres_has_locks(res))
  67. return 0;
  68. /* Locks are in the process of being created */
  69. if (res->inflight_locks)
  70. return 0;
  71. if (!list_empty(&res->dirty) || res->state & DLM_LOCK_RES_DIRTY)
  72. return 0;
  73. if (res->state & (DLM_LOCK_RES_RECOVERING|
  74. DLM_LOCK_RES_RECOVERY_WAITING))
  75. return 0;
  76. /* Another node has this resource with this node as the master */
  77. bit = find_first_bit(res->refmap, O2NM_MAX_NODES);
  78. if (bit < O2NM_MAX_NODES)
  79. return 0;
  80. return 1;
  81. }
  82. /* Call whenever you may have added or deleted something from one of
  83. * the lockres queue's. This will figure out whether it belongs on the
  84. * unused list or not and does the appropriate thing. */
  85. void __dlm_lockres_calc_usage(struct dlm_ctxt *dlm,
  86. struct dlm_lock_resource *res)
  87. {
  88. assert_spin_locked(&dlm->spinlock);
  89. assert_spin_locked(&res->spinlock);
  90. if (__dlm_lockres_unused(res)){
  91. if (list_empty(&res->purge)) {
  92. mlog(0, "%s: Adding res %.*s to purge list\n",
  93. dlm->name, res->lockname.len, res->lockname.name);
  94. res->last_used = jiffies;
  95. dlm_lockres_get(res);
  96. list_add_tail(&res->purge, &dlm->purge_list);
  97. dlm->purge_count++;
  98. }
  99. } else if (!list_empty(&res->purge)) {
  100. mlog(0, "%s: Removing res %.*s from purge list\n",
  101. dlm->name, res->lockname.len, res->lockname.name);
  102. list_del_init(&res->purge);
  103. dlm_lockres_put(res);
  104. dlm->purge_count--;
  105. }
  106. }
  107. void dlm_lockres_calc_usage(struct dlm_ctxt *dlm,
  108. struct dlm_lock_resource *res)
  109. {
  110. spin_lock(&dlm->spinlock);
  111. spin_lock(&res->spinlock);
  112. __dlm_lockres_calc_usage(dlm, res);
  113. spin_unlock(&res->spinlock);
  114. spin_unlock(&dlm->spinlock);
  115. }
  116. /*
  117. * Do the real purge work:
  118. * unhash the lockres, and
  119. * clear flag DLM_LOCK_RES_DROPPING_REF.
  120. * It requires dlm and lockres spinlock to be taken.
  121. */
  122. void __dlm_do_purge_lockres(struct dlm_ctxt *dlm,
  123. struct dlm_lock_resource *res)
  124. {
  125. assert_spin_locked(&dlm->spinlock);
  126. assert_spin_locked(&res->spinlock);
  127. if (!list_empty(&res->purge)) {
  128. mlog(0, "%s: Removing res %.*s from purgelist\n",
  129. dlm->name, res->lockname.len, res->lockname.name);
  130. list_del_init(&res->purge);
  131. dlm_lockres_put(res);
  132. dlm->purge_count--;
  133. }
  134. if (!__dlm_lockres_unused(res)) {
  135. mlog(ML_ERROR, "%s: res %.*s in use after deref\n",
  136. dlm->name, res->lockname.len, res->lockname.name);
  137. __dlm_print_one_lock_resource(res);
  138. BUG();
  139. }
  140. __dlm_unhash_lockres(dlm, res);
  141. spin_lock(&dlm->track_lock);
  142. if (!list_empty(&res->tracking))
  143. list_del_init(&res->tracking);
  144. else {
  145. mlog(ML_ERROR, "%s: Resource %.*s not on the Tracking list\n",
  146. dlm->name, res->lockname.len, res->lockname.name);
  147. __dlm_print_one_lock_resource(res);
  148. }
  149. spin_unlock(&dlm->track_lock);
  150. /*
  151. * lockres is not in the hash now. drop the flag and wake up
  152. * any processes waiting in dlm_get_lock_resource.
  153. */
  154. res->state &= ~DLM_LOCK_RES_DROPPING_REF;
  155. }
  156. static void dlm_purge_lockres(struct dlm_ctxt *dlm,
  157. struct dlm_lock_resource *res)
  158. {
  159. int master;
  160. int ret = 0;
  161. assert_spin_locked(&dlm->spinlock);
  162. assert_spin_locked(&res->spinlock);
  163. master = (res->owner == dlm->node_num);
  164. mlog(0, "%s: Purging res %.*s, master %d\n", dlm->name,
  165. res->lockname.len, res->lockname.name, master);
  166. if (!master) {
  167. if (res->state & DLM_LOCK_RES_DROPPING_REF) {
  168. mlog(ML_NOTICE, "%s: res %.*s already in DLM_LOCK_RES_DROPPING_REF state\n",
  169. dlm->name, res->lockname.len, res->lockname.name);
  170. spin_unlock(&res->spinlock);
  171. return;
  172. }
  173. res->state |= DLM_LOCK_RES_DROPPING_REF;
  174. /* drop spinlock... retake below */
  175. spin_unlock(&res->spinlock);
  176. spin_unlock(&dlm->spinlock);
  177. spin_lock(&res->spinlock);
  178. /* This ensures that clear refmap is sent after the set */
  179. __dlm_wait_on_lockres_flags(res, DLM_LOCK_RES_SETREF_INPROG);
  180. spin_unlock(&res->spinlock);
  181. /* clear our bit from the master's refmap, ignore errors */
  182. ret = dlm_drop_lockres_ref(dlm, res);
  183. if (ret < 0) {
  184. if (!dlm_is_host_down(ret))
  185. BUG();
  186. }
  187. spin_lock(&dlm->spinlock);
  188. spin_lock(&res->spinlock);
  189. }
  190. if (!list_empty(&res->purge)) {
  191. mlog(0, "%s: Removing res %.*s from purgelist, master %d\n",
  192. dlm->name, res->lockname.len, res->lockname.name, master);
  193. list_del_init(&res->purge);
  194. dlm_lockres_put(res);
  195. dlm->purge_count--;
  196. }
  197. if (!master && ret == DLM_DEREF_RESPONSE_INPROG) {
  198. mlog(0, "%s: deref %.*s in progress\n",
  199. dlm->name, res->lockname.len, res->lockname.name);
  200. spin_unlock(&res->spinlock);
  201. return;
  202. }
  203. if (!__dlm_lockres_unused(res)) {
  204. mlog(ML_ERROR, "%s: res %.*s in use after deref\n",
  205. dlm->name, res->lockname.len, res->lockname.name);
  206. __dlm_print_one_lock_resource(res);
  207. BUG();
  208. }
  209. __dlm_unhash_lockres(dlm, res);
  210. spin_lock(&dlm->track_lock);
  211. if (!list_empty(&res->tracking))
  212. list_del_init(&res->tracking);
  213. else {
  214. mlog(ML_ERROR, "Resource %.*s not on the Tracking list\n",
  215. res->lockname.len, res->lockname.name);
  216. __dlm_print_one_lock_resource(res);
  217. }
  218. spin_unlock(&dlm->track_lock);
  219. /* lockres is not in the hash now. drop the flag and wake up
  220. * any processes waiting in dlm_get_lock_resource. */
  221. if (!master) {
  222. res->state &= ~DLM_LOCK_RES_DROPPING_REF;
  223. spin_unlock(&res->spinlock);
  224. wake_up(&res->wq);
  225. } else
  226. spin_unlock(&res->spinlock);
  227. }
  228. static void dlm_run_purge_list(struct dlm_ctxt *dlm,
  229. int purge_now)
  230. {
  231. unsigned int run_max, unused;
  232. unsigned long purge_jiffies;
  233. struct dlm_lock_resource *lockres;
  234. spin_lock(&dlm->spinlock);
  235. run_max = dlm->purge_count;
  236. while(run_max && !list_empty(&dlm->purge_list)) {
  237. run_max--;
  238. lockres = list_entry(dlm->purge_list.next,
  239. struct dlm_lock_resource, purge);
  240. spin_lock(&lockres->spinlock);
  241. purge_jiffies = lockres->last_used +
  242. msecs_to_jiffies(DLM_PURGE_INTERVAL_MS);
  243. /* Make sure that we want to be processing this guy at
  244. * this time. */
  245. if (!purge_now && time_after(purge_jiffies, jiffies)) {
  246. /* Since resources are added to the purge list
  247. * in tail order, we can stop at the first
  248. * unpurgable resource -- anyone added after
  249. * him will have a greater last_used value */
  250. spin_unlock(&lockres->spinlock);
  251. break;
  252. }
  253. /* Status of the lockres *might* change so double
  254. * check. If the lockres is unused, holding the dlm
  255. * spinlock will prevent people from getting and more
  256. * refs on it. */
  257. unused = __dlm_lockres_unused(lockres);
  258. if (!unused ||
  259. (lockres->state & DLM_LOCK_RES_MIGRATING) ||
  260. (lockres->inflight_assert_workers != 0)) {
  261. mlog(0, "%s: res %.*s is in use or being remastered, "
  262. "used %d, state %d, assert master workers %u\n",
  263. dlm->name, lockres->lockname.len,
  264. lockres->lockname.name,
  265. !unused, lockres->state,
  266. lockres->inflight_assert_workers);
  267. list_move_tail(&lockres->purge, &dlm->purge_list);
  268. spin_unlock(&lockres->spinlock);
  269. continue;
  270. }
  271. dlm_lockres_get(lockres);
  272. dlm_purge_lockres(dlm, lockres);
  273. dlm_lockres_put(lockres);
  274. /* Avoid adding any scheduling latencies */
  275. cond_resched_lock(&dlm->spinlock);
  276. }
  277. spin_unlock(&dlm->spinlock);
  278. }
  279. static void dlm_shuffle_lists(struct dlm_ctxt *dlm,
  280. struct dlm_lock_resource *res)
  281. {
  282. struct dlm_lock *lock, *target;
  283. int can_grant = 1;
  284. /*
  285. * Because this function is called with the lockres
  286. * spinlock, and because we know that it is not migrating/
  287. * recovering/in-progress, it is fine to reserve asts and
  288. * basts right before queueing them all throughout
  289. */
  290. assert_spin_locked(&dlm->ast_lock);
  291. assert_spin_locked(&res->spinlock);
  292. BUG_ON((res->state & (DLM_LOCK_RES_MIGRATING|
  293. DLM_LOCK_RES_RECOVERING|
  294. DLM_LOCK_RES_IN_PROGRESS)));
  295. converting:
  296. if (list_empty(&res->converting))
  297. goto blocked;
  298. mlog(0, "%s: res %.*s has locks on the convert queue\n", dlm->name,
  299. res->lockname.len, res->lockname.name);
  300. target = list_entry(res->converting.next, struct dlm_lock, list);
  301. if (target->ml.convert_type == LKM_IVMODE) {
  302. mlog(ML_ERROR, "%s: res %.*s converting lock to invalid mode\n",
  303. dlm->name, res->lockname.len, res->lockname.name);
  304. BUG();
  305. }
  306. list_for_each_entry(lock, &res->granted, list) {
  307. if (lock==target)
  308. continue;
  309. if (!dlm_lock_compatible(lock->ml.type,
  310. target->ml.convert_type)) {
  311. can_grant = 0;
  312. /* queue the BAST if not already */
  313. if (lock->ml.highest_blocked == LKM_IVMODE) {
  314. __dlm_lockres_reserve_ast(res);
  315. __dlm_queue_bast(dlm, lock);
  316. }
  317. /* update the highest_blocked if needed */
  318. if (lock->ml.highest_blocked < target->ml.convert_type)
  319. lock->ml.highest_blocked =
  320. target->ml.convert_type;
  321. }
  322. }
  323. list_for_each_entry(lock, &res->converting, list) {
  324. if (lock==target)
  325. continue;
  326. if (!dlm_lock_compatible(lock->ml.type,
  327. target->ml.convert_type)) {
  328. can_grant = 0;
  329. if (lock->ml.highest_blocked == LKM_IVMODE) {
  330. __dlm_lockres_reserve_ast(res);
  331. __dlm_queue_bast(dlm, lock);
  332. }
  333. if (lock->ml.highest_blocked < target->ml.convert_type)
  334. lock->ml.highest_blocked =
  335. target->ml.convert_type;
  336. }
  337. }
  338. /* we can convert the lock */
  339. if (can_grant) {
  340. spin_lock(&target->spinlock);
  341. BUG_ON(target->ml.highest_blocked != LKM_IVMODE);
  342. mlog(0, "%s: res %.*s, AST for Converting lock %u:%llu, type "
  343. "%d => %d, node %u\n", dlm->name, res->lockname.len,
  344. res->lockname.name,
  345. dlm_get_lock_cookie_node(be64_to_cpu(target->ml.cookie)),
  346. dlm_get_lock_cookie_seq(be64_to_cpu(target->ml.cookie)),
  347. target->ml.type,
  348. target->ml.convert_type, target->ml.node);
  349. target->ml.type = target->ml.convert_type;
  350. target->ml.convert_type = LKM_IVMODE;
  351. list_move_tail(&target->list, &res->granted);
  352. BUG_ON(!target->lksb);
  353. target->lksb->status = DLM_NORMAL;
  354. spin_unlock(&target->spinlock);
  355. __dlm_lockres_reserve_ast(res);
  356. __dlm_queue_ast(dlm, target);
  357. /* go back and check for more */
  358. goto converting;
  359. }
  360. blocked:
  361. if (list_empty(&res->blocked))
  362. goto leave;
  363. target = list_entry(res->blocked.next, struct dlm_lock, list);
  364. list_for_each_entry(lock, &res->granted, list) {
  365. if (lock==target)
  366. continue;
  367. if (!dlm_lock_compatible(lock->ml.type, target->ml.type)) {
  368. can_grant = 0;
  369. if (lock->ml.highest_blocked == LKM_IVMODE) {
  370. __dlm_lockres_reserve_ast(res);
  371. __dlm_queue_bast(dlm, lock);
  372. }
  373. if (lock->ml.highest_blocked < target->ml.type)
  374. lock->ml.highest_blocked = target->ml.type;
  375. }
  376. }
  377. list_for_each_entry(lock, &res->converting, list) {
  378. if (lock==target)
  379. continue;
  380. if (!dlm_lock_compatible(lock->ml.type, target->ml.type)) {
  381. can_grant = 0;
  382. if (lock->ml.highest_blocked == LKM_IVMODE) {
  383. __dlm_lockres_reserve_ast(res);
  384. __dlm_queue_bast(dlm, lock);
  385. }
  386. if (lock->ml.highest_blocked < target->ml.type)
  387. lock->ml.highest_blocked = target->ml.type;
  388. }
  389. }
  390. /* we can grant the blocked lock (only
  391. * possible if converting list empty) */
  392. if (can_grant) {
  393. spin_lock(&target->spinlock);
  394. BUG_ON(target->ml.highest_blocked != LKM_IVMODE);
  395. mlog(0, "%s: res %.*s, AST for Blocked lock %u:%llu, type %d, "
  396. "node %u\n", dlm->name, res->lockname.len,
  397. res->lockname.name,
  398. dlm_get_lock_cookie_node(be64_to_cpu(target->ml.cookie)),
  399. dlm_get_lock_cookie_seq(be64_to_cpu(target->ml.cookie)),
  400. target->ml.type, target->ml.node);
  401. /* target->ml.type is already correct */
  402. list_move_tail(&target->list, &res->granted);
  403. BUG_ON(!target->lksb);
  404. target->lksb->status = DLM_NORMAL;
  405. spin_unlock(&target->spinlock);
  406. __dlm_lockres_reserve_ast(res);
  407. __dlm_queue_ast(dlm, target);
  408. /* go back and check for more */
  409. goto converting;
  410. }
  411. leave:
  412. return;
  413. }
  414. /* must have NO locks when calling this with res !=NULL * */
  415. void dlm_kick_thread(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
  416. {
  417. if (res) {
  418. spin_lock(&dlm->spinlock);
  419. spin_lock(&res->spinlock);
  420. __dlm_dirty_lockres(dlm, res);
  421. spin_unlock(&res->spinlock);
  422. spin_unlock(&dlm->spinlock);
  423. }
  424. wake_up(&dlm->dlm_thread_wq);
  425. }
  426. void __dlm_dirty_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
  427. {
  428. assert_spin_locked(&dlm->spinlock);
  429. assert_spin_locked(&res->spinlock);
  430. /* don't shuffle secondary queues */
  431. if (res->owner == dlm->node_num) {
  432. if (res->state & (DLM_LOCK_RES_MIGRATING |
  433. DLM_LOCK_RES_BLOCK_DIRTY))
  434. return;
  435. if (list_empty(&res->dirty)) {
  436. /* ref for dirty_list */
  437. dlm_lockres_get(res);
  438. list_add_tail(&res->dirty, &dlm->dirty_list);
  439. res->state |= DLM_LOCK_RES_DIRTY;
  440. }
  441. }
  442. mlog(0, "%s: res %.*s\n", dlm->name, res->lockname.len,
  443. res->lockname.name);
  444. }
  445. /* Launch the NM thread for the mounted volume */
  446. int dlm_launch_thread(struct dlm_ctxt *dlm)
  447. {
  448. mlog(0, "Starting dlm_thread...\n");
  449. dlm->dlm_thread_task = kthread_run(dlm_thread, dlm, "dlm-%s",
  450. dlm->name);
  451. if (IS_ERR(dlm->dlm_thread_task)) {
  452. mlog_errno(PTR_ERR(dlm->dlm_thread_task));
  453. dlm->dlm_thread_task = NULL;
  454. return -EINVAL;
  455. }
  456. return 0;
  457. }
  458. void dlm_complete_thread(struct dlm_ctxt *dlm)
  459. {
  460. if (dlm->dlm_thread_task) {
  461. mlog(ML_KTHREAD, "Waiting for dlm thread to exit\n");
  462. kthread_stop(dlm->dlm_thread_task);
  463. dlm->dlm_thread_task = NULL;
  464. }
  465. }
  466. static int dlm_dirty_list_empty(struct dlm_ctxt *dlm)
  467. {
  468. int empty;
  469. spin_lock(&dlm->spinlock);
  470. empty = list_empty(&dlm->dirty_list);
  471. spin_unlock(&dlm->spinlock);
  472. return empty;
  473. }
  474. static void dlm_flush_asts(struct dlm_ctxt *dlm)
  475. {
  476. int ret;
  477. struct dlm_lock *lock;
  478. struct dlm_lock_resource *res;
  479. u8 hi;
  480. spin_lock(&dlm->ast_lock);
  481. while (!list_empty(&dlm->pending_asts)) {
  482. lock = list_entry(dlm->pending_asts.next,
  483. struct dlm_lock, ast_list);
  484. /* get an extra ref on lock */
  485. dlm_lock_get(lock);
  486. res = lock->lockres;
  487. mlog(0, "%s: res %.*s, Flush AST for lock %u:%llu, type %d, "
  488. "node %u\n", dlm->name, res->lockname.len,
  489. res->lockname.name,
  490. dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
  491. dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)),
  492. lock->ml.type, lock->ml.node);
  493. BUG_ON(!lock->ast_pending);
  494. /* remove from list (including ref) */
  495. list_del_init(&lock->ast_list);
  496. dlm_lock_put(lock);
  497. spin_unlock(&dlm->ast_lock);
  498. if (lock->ml.node != dlm->node_num) {
  499. ret = dlm_do_remote_ast(dlm, res, lock);
  500. if (ret < 0)
  501. mlog_errno(ret);
  502. } else
  503. dlm_do_local_ast(dlm, res, lock);
  504. spin_lock(&dlm->ast_lock);
  505. /* possible that another ast was queued while
  506. * we were delivering the last one */
  507. if (!list_empty(&lock->ast_list)) {
  508. mlog(0, "%s: res %.*s, AST queued while flushing last "
  509. "one\n", dlm->name, res->lockname.len,
  510. res->lockname.name);
  511. } else
  512. lock->ast_pending = 0;
  513. /* drop the extra ref.
  514. * this may drop it completely. */
  515. dlm_lock_put(lock);
  516. dlm_lockres_release_ast(dlm, res);
  517. }
  518. while (!list_empty(&dlm->pending_basts)) {
  519. lock = list_entry(dlm->pending_basts.next,
  520. struct dlm_lock, bast_list);
  521. /* get an extra ref on lock */
  522. dlm_lock_get(lock);
  523. res = lock->lockres;
  524. BUG_ON(!lock->bast_pending);
  525. /* get the highest blocked lock, and reset */
  526. spin_lock(&lock->spinlock);
  527. BUG_ON(lock->ml.highest_blocked <= LKM_IVMODE);
  528. hi = lock->ml.highest_blocked;
  529. lock->ml.highest_blocked = LKM_IVMODE;
  530. spin_unlock(&lock->spinlock);
  531. /* remove from list (including ref) */
  532. list_del_init(&lock->bast_list);
  533. dlm_lock_put(lock);
  534. spin_unlock(&dlm->ast_lock);
  535. mlog(0, "%s: res %.*s, Flush BAST for lock %u:%llu, "
  536. "blocked %d, node %u\n",
  537. dlm->name, res->lockname.len, res->lockname.name,
  538. dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
  539. dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)),
  540. hi, lock->ml.node);
  541. if (lock->ml.node != dlm->node_num) {
  542. ret = dlm_send_proxy_bast(dlm, res, lock, hi);
  543. if (ret < 0)
  544. mlog_errno(ret);
  545. } else
  546. dlm_do_local_bast(dlm, res, lock, hi);
  547. spin_lock(&dlm->ast_lock);
  548. /* possible that another bast was queued while
  549. * we were delivering the last one */
  550. if (!list_empty(&lock->bast_list)) {
  551. mlog(0, "%s: res %.*s, BAST queued while flushing last "
  552. "one\n", dlm->name, res->lockname.len,
  553. res->lockname.name);
  554. } else
  555. lock->bast_pending = 0;
  556. /* drop the extra ref.
  557. * this may drop it completely. */
  558. dlm_lock_put(lock);
  559. dlm_lockres_release_ast(dlm, res);
  560. }
  561. wake_up(&dlm->ast_wq);
  562. spin_unlock(&dlm->ast_lock);
  563. }
  564. #define DLM_THREAD_TIMEOUT_MS (4 * 1000)
  565. #define DLM_THREAD_MAX_DIRTY 100
  566. static int dlm_thread(void *data)
  567. {
  568. struct dlm_lock_resource *res;
  569. struct dlm_ctxt *dlm = data;
  570. unsigned long timeout = msecs_to_jiffies(DLM_THREAD_TIMEOUT_MS);
  571. mlog(0, "dlm thread running for %s...\n", dlm->name);
  572. while (!kthread_should_stop()) {
  573. int n = DLM_THREAD_MAX_DIRTY;
  574. /* dlm_shutting_down is very point-in-time, but that
  575. * doesn't matter as we'll just loop back around if we
  576. * get false on the leading edge of a state
  577. * transition. */
  578. dlm_run_purge_list(dlm, dlm_shutting_down(dlm));
  579. /* We really don't want to hold dlm->spinlock while
  580. * calling dlm_shuffle_lists on each lockres that
  581. * needs to have its queues adjusted and AST/BASTs
  582. * run. So let's pull each entry off the dirty_list
  583. * and drop dlm->spinlock ASAP. Once off the list,
  584. * res->spinlock needs to be taken again to protect
  585. * the queues while calling dlm_shuffle_lists. */
  586. spin_lock(&dlm->spinlock);
  587. while (!list_empty(&dlm->dirty_list)) {
  588. int delay = 0;
  589. res = list_entry(dlm->dirty_list.next,
  590. struct dlm_lock_resource, dirty);
  591. /* peel a lockres off, remove it from the list,
  592. * unset the dirty flag and drop the dlm lock */
  593. BUG_ON(!res);
  594. dlm_lockres_get(res);
  595. spin_lock(&res->spinlock);
  596. /* We clear the DLM_LOCK_RES_DIRTY state once we shuffle lists below */
  597. list_del_init(&res->dirty);
  598. spin_unlock(&res->spinlock);
  599. spin_unlock(&dlm->spinlock);
  600. /* Drop dirty_list ref */
  601. dlm_lockres_put(res);
  602. /* lockres can be re-dirtied/re-added to the
  603. * dirty_list in this gap, but that is ok */
  604. spin_lock(&dlm->ast_lock);
  605. spin_lock(&res->spinlock);
  606. if (res->owner != dlm->node_num) {
  607. __dlm_print_one_lock_resource(res);
  608. mlog(ML_ERROR, "%s: inprog %d, mig %d, reco %d,"
  609. " dirty %d\n", dlm->name,
  610. !!(res->state & DLM_LOCK_RES_IN_PROGRESS),
  611. !!(res->state & DLM_LOCK_RES_MIGRATING),
  612. !!(res->state & DLM_LOCK_RES_RECOVERING),
  613. !!(res->state & DLM_LOCK_RES_DIRTY));
  614. }
  615. BUG_ON(res->owner != dlm->node_num);
  616. /* it is now ok to move lockreses in these states
  617. * to the dirty list, assuming that they will only be
  618. * dirty for a short while. */
  619. BUG_ON(res->state & DLM_LOCK_RES_MIGRATING);
  620. if (res->state & (DLM_LOCK_RES_IN_PROGRESS |
  621. DLM_LOCK_RES_RECOVERING |
  622. DLM_LOCK_RES_RECOVERY_WAITING)) {
  623. /* move it to the tail and keep going */
  624. res->state &= ~DLM_LOCK_RES_DIRTY;
  625. spin_unlock(&res->spinlock);
  626. spin_unlock(&dlm->ast_lock);
  627. mlog(0, "%s: res %.*s, inprogress, delay list "
  628. "shuffle, state %d\n", dlm->name,
  629. res->lockname.len, res->lockname.name,
  630. res->state);
  631. delay = 1;
  632. goto in_progress;
  633. }
  634. /* at this point the lockres is not migrating/
  635. * recovering/in-progress. we have the lockres
  636. * spinlock and do NOT have the dlm lock.
  637. * safe to reserve/queue asts and run the lists. */
  638. /* called while holding lockres lock */
  639. dlm_shuffle_lists(dlm, res);
  640. res->state &= ~DLM_LOCK_RES_DIRTY;
  641. spin_unlock(&res->spinlock);
  642. spin_unlock(&dlm->ast_lock);
  643. dlm_lockres_calc_usage(dlm, res);
  644. in_progress:
  645. spin_lock(&dlm->spinlock);
  646. /* if the lock was in-progress, stick
  647. * it on the back of the list */
  648. if (delay) {
  649. spin_lock(&res->spinlock);
  650. __dlm_dirty_lockres(dlm, res);
  651. spin_unlock(&res->spinlock);
  652. }
  653. dlm_lockres_put(res);
  654. /* unlikely, but we may need to give time to
  655. * other tasks */
  656. if (!--n) {
  657. mlog(0, "%s: Throttling dlm thread\n",
  658. dlm->name);
  659. break;
  660. }
  661. }
  662. spin_unlock(&dlm->spinlock);
  663. dlm_flush_asts(dlm);
  664. /* yield and continue right away if there is more work to do */
  665. if (!n) {
  666. cond_resched();
  667. continue;
  668. }
  669. wait_event_interruptible_timeout(dlm->dlm_thread_wq,
  670. !dlm_dirty_list_empty(dlm) ||
  671. kthread_should_stop(),
  672. timeout);
  673. }
  674. mlog(0, "quitting DLM thread\n");
  675. return 0;
  676. }