thread.c 8.1 KB

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