dlmthread.c 23 KB

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