thread.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. #include "lock_dlm.h"
  10. /* A lock placed on this queue is re-submitted to DLM as soon as the lock_dlm
  11. thread gets to it. */
  12. static void queue_submit(struct gdlm_lock *lp)
  13. {
  14. struct gdlm_ls *ls = lp->ls;
  15. spin_lock(&ls->async_lock);
  16. list_add_tail(&lp->delay_list, &ls->submit);
  17. spin_unlock(&ls->async_lock);
  18. wake_up(&ls->thread_wait);
  19. }
  20. static void process_blocking(struct gdlm_lock *lp, int bast_mode)
  21. {
  22. struct gdlm_ls *ls = lp->ls;
  23. unsigned int cb = 0;
  24. switch (gdlm_make_lmstate(bast_mode)) {
  25. case LM_ST_EXCLUSIVE:
  26. cb = LM_CB_NEED_E;
  27. break;
  28. case LM_ST_DEFERRED:
  29. cb = LM_CB_NEED_D;
  30. break;
  31. case LM_ST_SHARED:
  32. cb = LM_CB_NEED_S;
  33. break;
  34. default:
  35. gdlm_assert(0, "unknown bast mode %u", lp->bast_mode);
  36. }
  37. ls->fscb(ls->sdp, cb, &lp->lockname);
  38. }
  39. static void wake_up_ast(struct gdlm_lock *lp)
  40. {
  41. clear_bit(LFL_AST_WAIT, &lp->flags);
  42. smp_mb__after_clear_bit();
  43. wake_up_bit(&lp->flags, LFL_AST_WAIT);
  44. }
  45. static void process_complete(struct gdlm_lock *lp)
  46. {
  47. struct gdlm_ls *ls = lp->ls;
  48. struct lm_async_cb acb;
  49. s16 prev_mode = lp->cur;
  50. memset(&acb, 0, sizeof(acb));
  51. if (lp->lksb.sb_status == -DLM_ECANCEL) {
  52. log_info("complete dlm cancel %x,%llx flags %lx",
  53. lp->lockname.ln_type,
  54. (unsigned long long)lp->lockname.ln_number,
  55. lp->flags);
  56. lp->req = lp->cur;
  57. acb.lc_ret |= LM_OUT_CANCELED;
  58. if (lp->cur == DLM_LOCK_IV)
  59. lp->lksb.sb_lkid = 0;
  60. goto out;
  61. }
  62. if (test_and_clear_bit(LFL_DLM_UNLOCK, &lp->flags)) {
  63. if (lp->lksb.sb_status != -DLM_EUNLOCK) {
  64. log_info("unlock sb_status %d %x,%llx flags %lx",
  65. lp->lksb.sb_status, lp->lockname.ln_type,
  66. (unsigned long long)lp->lockname.ln_number,
  67. lp->flags);
  68. return;
  69. }
  70. lp->cur = DLM_LOCK_IV;
  71. lp->req = DLM_LOCK_IV;
  72. lp->lksb.sb_lkid = 0;
  73. if (test_and_clear_bit(LFL_UNLOCK_DELETE, &lp->flags)) {
  74. gdlm_delete_lp(lp);
  75. return;
  76. }
  77. goto out;
  78. }
  79. if (lp->lksb.sb_flags & DLM_SBF_VALNOTVALID)
  80. memset(lp->lksb.sb_lvbptr, 0, GDLM_LVB_SIZE);
  81. if (lp->lksb.sb_flags & DLM_SBF_ALTMODE) {
  82. if (lp->req == DLM_LOCK_PR)
  83. lp->req = DLM_LOCK_CW;
  84. else if (lp->req == DLM_LOCK_CW)
  85. lp->req = DLM_LOCK_PR;
  86. }
  87. /*
  88. * A canceled lock request. The lock was just taken off the delayed
  89. * list and was never even submitted to dlm.
  90. */
  91. if (test_and_clear_bit(LFL_CANCEL, &lp->flags)) {
  92. log_info("complete internal cancel %x,%llx",
  93. lp->lockname.ln_type,
  94. (unsigned long long)lp->lockname.ln_number);
  95. lp->req = lp->cur;
  96. acb.lc_ret |= LM_OUT_CANCELED;
  97. goto out;
  98. }
  99. /*
  100. * An error occured.
  101. */
  102. if (lp->lksb.sb_status) {
  103. /* a "normal" error */
  104. if ((lp->lksb.sb_status == -EAGAIN) &&
  105. (lp->lkf & DLM_LKF_NOQUEUE)) {
  106. lp->req = lp->cur;
  107. if (lp->cur == DLM_LOCK_IV)
  108. lp->lksb.sb_lkid = 0;
  109. goto out;
  110. }
  111. /* this could only happen with cancels I think */
  112. log_info("ast sb_status %d %x,%llx flags %lx",
  113. lp->lksb.sb_status, lp->lockname.ln_type,
  114. (unsigned long long)lp->lockname.ln_number,
  115. lp->flags);
  116. return;
  117. }
  118. /*
  119. * This is an AST for an EX->EX conversion for sync_lvb from GFS.
  120. */
  121. if (test_and_clear_bit(LFL_SYNC_LVB, &lp->flags)) {
  122. wake_up_ast(lp);
  123. return;
  124. }
  125. /*
  126. * A lock has been demoted to NL because it initially completed during
  127. * BLOCK_LOCKS. Now it must be requested in the originally requested
  128. * mode.
  129. */
  130. if (test_and_clear_bit(LFL_REREQUEST, &lp->flags)) {
  131. gdlm_assert(lp->req == DLM_LOCK_NL, "%x,%llx",
  132. lp->lockname.ln_type,
  133. (unsigned long long)lp->lockname.ln_number);
  134. gdlm_assert(lp->prev_req > DLM_LOCK_NL, "%x,%llx",
  135. lp->lockname.ln_type,
  136. (unsigned long long)lp->lockname.ln_number);
  137. lp->cur = DLM_LOCK_NL;
  138. lp->req = lp->prev_req;
  139. lp->prev_req = DLM_LOCK_IV;
  140. lp->lkf &= ~DLM_LKF_CONVDEADLK;
  141. set_bit(LFL_NOCACHE, &lp->flags);
  142. if (test_bit(DFL_BLOCK_LOCKS, &ls->flags) &&
  143. !test_bit(LFL_NOBLOCK, &lp->flags))
  144. gdlm_queue_delayed(lp);
  145. else
  146. queue_submit(lp);
  147. return;
  148. }
  149. /*
  150. * A request is granted during dlm recovery. It may be granted
  151. * because the locks of a failed node were cleared. In that case,
  152. * there may be inconsistent data beneath this lock and we must wait
  153. * for recovery to complete to use it. When gfs recovery is done this
  154. * granted lock will be converted to NL and then reacquired in this
  155. * granted state.
  156. */
  157. if (test_bit(DFL_BLOCK_LOCKS, &ls->flags) &&
  158. !test_bit(LFL_NOBLOCK, &lp->flags) &&
  159. lp->req != DLM_LOCK_NL) {
  160. lp->cur = lp->req;
  161. lp->prev_req = lp->req;
  162. lp->req = DLM_LOCK_NL;
  163. lp->lkf |= DLM_LKF_CONVERT;
  164. lp->lkf &= ~DLM_LKF_CONVDEADLK;
  165. log_debug("rereq %x,%llx id %x %d,%d",
  166. lp->lockname.ln_type,
  167. (unsigned long long)lp->lockname.ln_number,
  168. lp->lksb.sb_lkid, lp->cur, lp->req);
  169. set_bit(LFL_REREQUEST, &lp->flags);
  170. queue_submit(lp);
  171. return;
  172. }
  173. /*
  174. * DLM demoted the lock to NL before it was granted so GFS must be
  175. * told it cannot cache data for this lock.
  176. */
  177. if (lp->lksb.sb_flags & DLM_SBF_DEMOTED)
  178. set_bit(LFL_NOCACHE, &lp->flags);
  179. out:
  180. /*
  181. * This is an internal lock_dlm lock
  182. */
  183. if (test_bit(LFL_INLOCK, &lp->flags)) {
  184. clear_bit(LFL_NOBLOCK, &lp->flags);
  185. lp->cur = lp->req;
  186. wake_up_ast(lp);
  187. return;
  188. }
  189. /*
  190. * Normal completion of a lock request. Tell GFS it now has the lock.
  191. */
  192. clear_bit(LFL_NOBLOCK, &lp->flags);
  193. lp->cur = lp->req;
  194. acb.lc_name = lp->lockname;
  195. acb.lc_ret |= gdlm_make_lmstate(lp->cur);
  196. if (!test_and_clear_bit(LFL_NOCACHE, &lp->flags) &&
  197. (lp->cur > DLM_LOCK_NL) && (prev_mode > DLM_LOCK_NL))
  198. acb.lc_ret |= LM_OUT_CACHEABLE;
  199. ls->fscb(ls->sdp, LM_CB_ASYNC, &acb);
  200. }
  201. static inline int no_work(struct gdlm_ls *ls, int blocking)
  202. {
  203. int ret;
  204. spin_lock(&ls->async_lock);
  205. ret = list_empty(&ls->complete) && list_empty(&ls->submit);
  206. if (ret && blocking)
  207. ret = list_empty(&ls->blocking);
  208. spin_unlock(&ls->async_lock);
  209. return ret;
  210. }
  211. static inline int check_drop(struct gdlm_ls *ls)
  212. {
  213. if (!ls->drop_locks_count)
  214. return 0;
  215. if (time_after(jiffies, ls->drop_time + ls->drop_locks_period * HZ)) {
  216. ls->drop_time = jiffies;
  217. if (ls->all_locks_count >= ls->drop_locks_count)
  218. return 1;
  219. }
  220. return 0;
  221. }
  222. static int gdlm_thread(void *data, int blist)
  223. {
  224. struct gdlm_ls *ls = (struct gdlm_ls *) data;
  225. struct gdlm_lock *lp = NULL;
  226. uint8_t complete, blocking, submit, drop;
  227. DECLARE_WAITQUEUE(wait, current);
  228. /* Only thread1 is allowed to do blocking callbacks since gfs
  229. may wait for a completion callback within a blocking cb. */
  230. while (!kthread_should_stop()) {
  231. set_current_state(TASK_INTERRUPTIBLE);
  232. add_wait_queue(&ls->thread_wait, &wait);
  233. if (no_work(ls, blist))
  234. schedule();
  235. remove_wait_queue(&ls->thread_wait, &wait);
  236. set_current_state(TASK_RUNNING);
  237. complete = blocking = submit = drop = 0;
  238. spin_lock(&ls->async_lock);
  239. if (blist && !list_empty(&ls->blocking)) {
  240. lp = list_entry(ls->blocking.next, struct gdlm_lock,
  241. blist);
  242. list_del_init(&lp->blist);
  243. blocking = lp->bast_mode;
  244. lp->bast_mode = 0;
  245. } else if (!list_empty(&ls->complete)) {
  246. lp = list_entry(ls->complete.next, struct gdlm_lock,
  247. clist);
  248. list_del_init(&lp->clist);
  249. complete = 1;
  250. } else if (!list_empty(&ls->submit)) {
  251. lp = list_entry(ls->submit.next, struct gdlm_lock,
  252. delay_list);
  253. list_del_init(&lp->delay_list);
  254. submit = 1;
  255. }
  256. drop = check_drop(ls);
  257. spin_unlock(&ls->async_lock);
  258. if (complete)
  259. process_complete(lp);
  260. else if (blocking)
  261. process_blocking(lp, blocking);
  262. else if (submit)
  263. gdlm_do_lock(lp);
  264. if (drop)
  265. ls->fscb(ls->sdp, LM_CB_DROPLOCKS, NULL);
  266. schedule();
  267. }
  268. return 0;
  269. }
  270. static int gdlm_thread1(void *data)
  271. {
  272. return gdlm_thread(data, 1);
  273. }
  274. static int gdlm_thread2(void *data)
  275. {
  276. return gdlm_thread(data, 0);
  277. }
  278. int gdlm_init_threads(struct gdlm_ls *ls)
  279. {
  280. struct task_struct *p;
  281. int error;
  282. p = kthread_run(gdlm_thread1, ls, "lock_dlm1");
  283. error = IS_ERR(p);
  284. if (error) {
  285. log_error("can't start lock_dlm1 thread %d", error);
  286. return error;
  287. }
  288. ls->thread1 = p;
  289. p = kthread_run(gdlm_thread2, ls, "lock_dlm2");
  290. error = IS_ERR(p);
  291. if (error) {
  292. log_error("can't start lock_dlm2 thread %d", error);
  293. kthread_stop(ls->thread1);
  294. return error;
  295. }
  296. ls->thread2 = p;
  297. return 0;
  298. }
  299. void gdlm_release_threads(struct gdlm_ls *ls)
  300. {
  301. kthread_stop(ls->thread1);
  302. kthread_stop(ls->thread2);
  303. }