dlmthread.c 20 KB

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