thread.c 8.2 KB

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