dlmthread.c 21 KB

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