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