dlmthread.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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. assert_spin_locked(&res->spinlock);
  84. if (__dlm_lockres_has_locks(res))
  85. return 0;
  86. /* Locks are in the process of being created */
  87. if (res->inflight_locks)
  88. return 0;
  89. if (!list_empty(&res->dirty) || res->state & DLM_LOCK_RES_DIRTY)
  90. return 0;
  91. if (res->state & DLM_LOCK_RES_RECOVERING)
  92. return 0;
  93. /* Another node has this resource with this node as the master */
  94. bit = find_next_bit(res->refmap, O2NM_MAX_NODES, 0);
  95. if (bit < O2NM_MAX_NODES)
  96. return 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. 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, "%s: Adding res %.*s to purge list\n",
  110. dlm->name, res->lockname.len, res->lockname.name);
  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, "%s: Removing res %.*s from purge list\n",
  118. dlm->name, res->lockname.len, res->lockname.name);
  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. 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 void dlm_purge_lockres(struct dlm_ctxt *dlm,
  134. struct dlm_lock_resource *res)
  135. {
  136. int master;
  137. int ret = 0;
  138. assert_spin_locked(&dlm->spinlock);
  139. assert_spin_locked(&res->spinlock);
  140. master = (res->owner == dlm->node_num);
  141. mlog(0, "%s: Purging res %.*s, master %d\n", dlm->name,
  142. res->lockname.len, res->lockname.name, master);
  143. if (!master) {
  144. res->state |= DLM_LOCK_RES_DROPPING_REF;
  145. /* drop spinlock... retake below */
  146. spin_unlock(&res->spinlock);
  147. spin_unlock(&dlm->spinlock);
  148. spin_lock(&res->spinlock);
  149. /* This ensures that clear refmap is sent after the set */
  150. __dlm_wait_on_lockres_flags(res, DLM_LOCK_RES_SETREF_INPROG);
  151. spin_unlock(&res->spinlock);
  152. /* clear our bit from the master's refmap, ignore errors */
  153. ret = dlm_drop_lockres_ref(dlm, res);
  154. if (ret < 0) {
  155. if (!dlm_is_host_down(ret))
  156. BUG();
  157. }
  158. spin_lock(&dlm->spinlock);
  159. spin_lock(&res->spinlock);
  160. }
  161. if (!list_empty(&res->purge)) {
  162. mlog(0, "%s: Removing res %.*s from purgelist, master %d\n",
  163. dlm->name, res->lockname.len, res->lockname.name, master);
  164. list_del_init(&res->purge);
  165. dlm_lockres_put(res);
  166. dlm->purge_count--;
  167. }
  168. if (!__dlm_lockres_unused(res)) {
  169. mlog(ML_ERROR, "%s: res %.*s in use after deref\n",
  170. dlm->name, res->lockname.len, res->lockname.name);
  171. __dlm_print_one_lock_resource(res);
  172. BUG();
  173. }
  174. __dlm_unhash_lockres(dlm, res);
  175. spin_lock(&dlm->track_lock);
  176. if (!list_empty(&res->tracking))
  177. list_del_init(&res->tracking);
  178. else {
  179. mlog(ML_ERROR, "Resource %.*s not on the Tracking list\n",
  180. res->lockname.len, res->lockname.name);
  181. __dlm_print_one_lock_resource(res);
  182. }
  183. spin_unlock(&dlm->track_lock);
  184. /* lockres is not in the hash now. drop the flag and wake up
  185. * any processes waiting in dlm_get_lock_resource. */
  186. if (!master) {
  187. res->state &= ~DLM_LOCK_RES_DROPPING_REF;
  188. spin_unlock(&res->spinlock);
  189. wake_up(&res->wq);
  190. } else
  191. spin_unlock(&res->spinlock);
  192. }
  193. static void dlm_run_purge_list(struct dlm_ctxt *dlm,
  194. int purge_now)
  195. {
  196. unsigned int run_max, unused;
  197. unsigned long purge_jiffies;
  198. struct dlm_lock_resource *lockres;
  199. spin_lock(&dlm->spinlock);
  200. run_max = dlm->purge_count;
  201. while(run_max && !list_empty(&dlm->purge_list)) {
  202. run_max--;
  203. lockres = list_entry(dlm->purge_list.next,
  204. struct dlm_lock_resource, purge);
  205. spin_lock(&lockres->spinlock);
  206. purge_jiffies = lockres->last_used +
  207. msecs_to_jiffies(DLM_PURGE_INTERVAL_MS);
  208. /* Make sure that we want to be processing this guy at
  209. * this time. */
  210. if (!purge_now && time_after(purge_jiffies, jiffies)) {
  211. /* Since resources are added to the purge list
  212. * in tail order, we can stop at the first
  213. * unpurgable resource -- anyone added after
  214. * him will have a greater last_used value */
  215. spin_unlock(&lockres->spinlock);
  216. break;
  217. }
  218. /* Status of the lockres *might* change so double
  219. * check. If the lockres is unused, holding the dlm
  220. * spinlock will prevent people from getting and more
  221. * refs on it. */
  222. unused = __dlm_lockres_unused(lockres);
  223. if (!unused ||
  224. (lockres->state & DLM_LOCK_RES_MIGRATING) ||
  225. (lockres->inflight_assert_workers != 0)) {
  226. mlog(0, "%s: res %.*s is in use or being remastered, "
  227. "used %d, state %d, assert master workers %u\n",
  228. dlm->name, lockres->lockname.len,
  229. lockres->lockname.name,
  230. !unused, lockres->state,
  231. lockres->inflight_assert_workers);
  232. list_move_tail(&lockres->purge, &dlm->purge_list);
  233. spin_unlock(&lockres->spinlock);
  234. continue;
  235. }
  236. dlm_lockres_get(lockres);
  237. dlm_purge_lockres(dlm, lockres);
  238. dlm_lockres_put(lockres);
  239. /* Avoid adding any scheduling latencies */
  240. cond_resched_lock(&dlm->spinlock);
  241. }
  242. spin_unlock(&dlm->spinlock);
  243. }
  244. static void dlm_shuffle_lists(struct dlm_ctxt *dlm,
  245. struct dlm_lock_resource *res)
  246. {
  247. struct dlm_lock *lock, *target;
  248. int can_grant = 1;
  249. /*
  250. * Because this function is called with the lockres
  251. * spinlock, and because we know that it is not migrating/
  252. * recovering/in-progress, it is fine to reserve asts and
  253. * basts right before queueing them all throughout
  254. */
  255. assert_spin_locked(&dlm->ast_lock);
  256. assert_spin_locked(&res->spinlock);
  257. BUG_ON((res->state & (DLM_LOCK_RES_MIGRATING|
  258. DLM_LOCK_RES_RECOVERING|
  259. DLM_LOCK_RES_IN_PROGRESS)));
  260. converting:
  261. if (list_empty(&res->converting))
  262. goto blocked;
  263. mlog(0, "%s: res %.*s has locks on the convert queue\n", dlm->name,
  264. res->lockname.len, res->lockname.name);
  265. target = list_entry(res->converting.next, struct dlm_lock, list);
  266. if (target->ml.convert_type == LKM_IVMODE) {
  267. mlog(ML_ERROR, "%s: res %.*s converting lock to invalid mode\n",
  268. dlm->name, res->lockname.len, res->lockname.name);
  269. BUG();
  270. }
  271. list_for_each_entry(lock, &res->granted, 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. list_for_each_entry(lock, &res->converting, list) {
  289. if (lock==target)
  290. continue;
  291. if (!dlm_lock_compatible(lock->ml.type,
  292. target->ml.convert_type)) {
  293. can_grant = 0;
  294. if (lock->ml.highest_blocked == LKM_IVMODE) {
  295. __dlm_lockres_reserve_ast(res);
  296. __dlm_queue_bast(dlm, lock);
  297. }
  298. if (lock->ml.highest_blocked < target->ml.convert_type)
  299. lock->ml.highest_blocked =
  300. target->ml.convert_type;
  301. }
  302. }
  303. /* we can convert the lock */
  304. if (can_grant) {
  305. spin_lock(&target->spinlock);
  306. BUG_ON(target->ml.highest_blocked != LKM_IVMODE);
  307. mlog(0, "%s: res %.*s, AST for Converting lock %u:%llu, type "
  308. "%d => %d, node %u\n", dlm->name, res->lockname.len,
  309. res->lockname.name,
  310. dlm_get_lock_cookie_node(be64_to_cpu(target->ml.cookie)),
  311. dlm_get_lock_cookie_seq(be64_to_cpu(target->ml.cookie)),
  312. target->ml.type,
  313. target->ml.convert_type, target->ml.node);
  314. target->ml.type = target->ml.convert_type;
  315. target->ml.convert_type = LKM_IVMODE;
  316. list_move_tail(&target->list, &res->granted);
  317. BUG_ON(!target->lksb);
  318. target->lksb->status = DLM_NORMAL;
  319. spin_unlock(&target->spinlock);
  320. __dlm_lockres_reserve_ast(res);
  321. __dlm_queue_ast(dlm, target);
  322. /* go back and check for more */
  323. goto converting;
  324. }
  325. blocked:
  326. if (list_empty(&res->blocked))
  327. goto leave;
  328. target = list_entry(res->blocked.next, struct dlm_lock, list);
  329. list_for_each_entry(lock, &res->granted, list) {
  330. if (lock==target)
  331. continue;
  332. if (!dlm_lock_compatible(lock->ml.type, target->ml.type)) {
  333. can_grant = 0;
  334. if (lock->ml.highest_blocked == LKM_IVMODE) {
  335. __dlm_lockres_reserve_ast(res);
  336. __dlm_queue_bast(dlm, lock);
  337. }
  338. if (lock->ml.highest_blocked < target->ml.type)
  339. lock->ml.highest_blocked = target->ml.type;
  340. }
  341. }
  342. list_for_each_entry(lock, &res->converting, list) {
  343. if (lock==target)
  344. continue;
  345. if (!dlm_lock_compatible(lock->ml.type, target->ml.type)) {
  346. can_grant = 0;
  347. if (lock->ml.highest_blocked == LKM_IVMODE) {
  348. __dlm_lockres_reserve_ast(res);
  349. __dlm_queue_bast(dlm, lock);
  350. }
  351. if (lock->ml.highest_blocked < target->ml.type)
  352. lock->ml.highest_blocked = target->ml.type;
  353. }
  354. }
  355. /* we can grant the blocked lock (only
  356. * possible if converting list empty) */
  357. if (can_grant) {
  358. spin_lock(&target->spinlock);
  359. BUG_ON(target->ml.highest_blocked != LKM_IVMODE);
  360. mlog(0, "%s: res %.*s, AST for Blocked lock %u:%llu, type %d, "
  361. "node %u\n", dlm->name, res->lockname.len,
  362. res->lockname.name,
  363. dlm_get_lock_cookie_node(be64_to_cpu(target->ml.cookie)),
  364. dlm_get_lock_cookie_seq(be64_to_cpu(target->ml.cookie)),
  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. if (res) {
  383. spin_lock(&dlm->spinlock);
  384. spin_lock(&res->spinlock);
  385. __dlm_dirty_lockres(dlm, res);
  386. spin_unlock(&res->spinlock);
  387. spin_unlock(&dlm->spinlock);
  388. }
  389. wake_up(&dlm->dlm_thread_wq);
  390. }
  391. void __dlm_dirty_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
  392. {
  393. assert_spin_locked(&dlm->spinlock);
  394. assert_spin_locked(&res->spinlock);
  395. /* don't shuffle secondary queues */
  396. if ((res->owner == dlm->node_num)) {
  397. if (res->state & (DLM_LOCK_RES_MIGRATING |
  398. DLM_LOCK_RES_BLOCK_DIRTY))
  399. return;
  400. if (list_empty(&res->dirty)) {
  401. /* ref for dirty_list */
  402. dlm_lockres_get(res);
  403. list_add_tail(&res->dirty, &dlm->dirty_list);
  404. res->state |= DLM_LOCK_RES_DIRTY;
  405. }
  406. }
  407. mlog(0, "%s: res %.*s\n", dlm->name, res->lockname.len,
  408. res->lockname.name);
  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-%s",
  415. dlm->name);
  416. if (IS_ERR(dlm->dlm_thread_task)) {
  417. mlog_errno(PTR_ERR(dlm->dlm_thread_task));
  418. dlm->dlm_thread_task = NULL;
  419. return -EINVAL;
  420. }
  421. return 0;
  422. }
  423. void dlm_complete_thread(struct dlm_ctxt *dlm)
  424. {
  425. if (dlm->dlm_thread_task) {
  426. mlog(ML_KTHREAD, "Waiting for dlm thread to exit\n");
  427. kthread_stop(dlm->dlm_thread_task);
  428. dlm->dlm_thread_task = NULL;
  429. }
  430. }
  431. static int dlm_dirty_list_empty(struct dlm_ctxt *dlm)
  432. {
  433. int empty;
  434. spin_lock(&dlm->spinlock);
  435. empty = list_empty(&dlm->dirty_list);
  436. spin_unlock(&dlm->spinlock);
  437. return empty;
  438. }
  439. static void dlm_flush_asts(struct dlm_ctxt *dlm)
  440. {
  441. int ret;
  442. struct dlm_lock *lock;
  443. struct dlm_lock_resource *res;
  444. u8 hi;
  445. spin_lock(&dlm->ast_lock);
  446. while (!list_empty(&dlm->pending_asts)) {
  447. lock = list_entry(dlm->pending_asts.next,
  448. struct dlm_lock, ast_list);
  449. /* get an extra ref on lock */
  450. dlm_lock_get(lock);
  451. res = lock->lockres;
  452. mlog(0, "%s: res %.*s, Flush AST for lock %u:%llu, type %d, "
  453. "node %u\n", dlm->name, res->lockname.len,
  454. res->lockname.name,
  455. dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
  456. dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)),
  457. lock->ml.type, lock->ml.node);
  458. BUG_ON(!lock->ast_pending);
  459. /* remove from list (including ref) */
  460. list_del_init(&lock->ast_list);
  461. dlm_lock_put(lock);
  462. spin_unlock(&dlm->ast_lock);
  463. if (lock->ml.node != dlm->node_num) {
  464. ret = dlm_do_remote_ast(dlm, res, lock);
  465. if (ret < 0)
  466. mlog_errno(ret);
  467. } else
  468. dlm_do_local_ast(dlm, res, lock);
  469. spin_lock(&dlm->ast_lock);
  470. /* possible that another ast was queued while
  471. * we were delivering the last one */
  472. if (!list_empty(&lock->ast_list)) {
  473. mlog(0, "%s: res %.*s, AST queued while flushing last "
  474. "one\n", dlm->name, res->lockname.len,
  475. res->lockname.name);
  476. } else
  477. lock->ast_pending = 0;
  478. /* drop the extra ref.
  479. * this may drop it completely. */
  480. dlm_lock_put(lock);
  481. dlm_lockres_release_ast(dlm, res);
  482. }
  483. while (!list_empty(&dlm->pending_basts)) {
  484. lock = list_entry(dlm->pending_basts.next,
  485. struct dlm_lock, bast_list);
  486. /* get an extra ref on lock */
  487. dlm_lock_get(lock);
  488. res = lock->lockres;
  489. BUG_ON(!lock->bast_pending);
  490. /* get the highest blocked lock, and reset */
  491. spin_lock(&lock->spinlock);
  492. BUG_ON(lock->ml.highest_blocked <= LKM_IVMODE);
  493. hi = lock->ml.highest_blocked;
  494. lock->ml.highest_blocked = LKM_IVMODE;
  495. spin_unlock(&lock->spinlock);
  496. /* remove from list (including ref) */
  497. list_del_init(&lock->bast_list);
  498. dlm_lock_put(lock);
  499. spin_unlock(&dlm->ast_lock);
  500. mlog(0, "%s: res %.*s, Flush BAST for lock %u:%llu, "
  501. "blocked %d, node %u\n",
  502. dlm->name, res->lockname.len, res->lockname.name,
  503. dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
  504. dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)),
  505. hi, lock->ml.node);
  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, "%s: res %.*s, BAST queued while flushing last "
  517. "one\n", dlm->name, res->lockname.len,
  518. res->lockname.name);
  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(&dlm->ast_lock);
  571. spin_lock(&res->spinlock);
  572. if (res->owner != dlm->node_num) {
  573. __dlm_print_one_lock_resource(res);
  574. mlog(ML_ERROR, "%s: inprog %d, mig %d, reco %d,"
  575. " dirty %d\n", dlm->name,
  576. !!(res->state & DLM_LOCK_RES_IN_PROGRESS),
  577. !!(res->state & DLM_LOCK_RES_MIGRATING),
  578. !!(res->state & DLM_LOCK_RES_RECOVERING),
  579. !!(res->state & DLM_LOCK_RES_DIRTY));
  580. }
  581. BUG_ON(res->owner != dlm->node_num);
  582. /* it is now ok to move lockreses in these states
  583. * to the dirty list, assuming that they will only be
  584. * dirty for a short while. */
  585. BUG_ON(res->state & DLM_LOCK_RES_MIGRATING);
  586. if (res->state & (DLM_LOCK_RES_IN_PROGRESS |
  587. DLM_LOCK_RES_RECOVERING)) {
  588. /* move it to the tail and keep going */
  589. res->state &= ~DLM_LOCK_RES_DIRTY;
  590. spin_unlock(&res->spinlock);
  591. spin_unlock(&dlm->ast_lock);
  592. mlog(0, "%s: res %.*s, inprogress, delay list "
  593. "shuffle, state %d\n", dlm->name,
  594. res->lockname.len, res->lockname.name,
  595. res->state);
  596. delay = 1;
  597. goto in_progress;
  598. }
  599. /* at this point the lockres is not migrating/
  600. * recovering/in-progress. we have the lockres
  601. * spinlock and do NOT have the dlm lock.
  602. * safe to reserve/queue asts and run the lists. */
  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. spin_unlock(&dlm->ast_lock);
  608. dlm_lockres_calc_usage(dlm, res);
  609. in_progress:
  610. spin_lock(&dlm->spinlock);
  611. /* if the lock was in-progress, stick
  612. * it on the back of the list */
  613. if (delay) {
  614. spin_lock(&res->spinlock);
  615. __dlm_dirty_lockres(dlm, res);
  616. spin_unlock(&res->spinlock);
  617. }
  618. dlm_lockres_put(res);
  619. /* unlikely, but we may need to give time to
  620. * other tasks */
  621. if (!--n) {
  622. mlog(0, "%s: Throttling dlm thread\n",
  623. dlm->name);
  624. break;
  625. }
  626. }
  627. spin_unlock(&dlm->spinlock);
  628. dlm_flush_asts(dlm);
  629. /* yield and continue right away if there is more work to do */
  630. if (!n) {
  631. cond_resched();
  632. continue;
  633. }
  634. wait_event_interruptible_timeout(dlm->dlm_thread_wq,
  635. !dlm_dirty_list_empty(dlm) ||
  636. kthread_should_stop(),
  637. timeout);
  638. }
  639. mlog(0, "quitting DLM thread\n");
  640. return 0;
  641. }