dlmthread.c 21 KB

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