dlmthread.c 21 KB

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