thread.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /******************************************************************************
  2. *******************************************************************************
  3. **
  4. ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  5. ** Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
  6. **
  7. ** This copyrighted material is made available to anyone wishing to use,
  8. ** modify, copy, or redistribute it subject to the terms and conditions
  9. ** of the GNU General Public License v.2.
  10. **
  11. *******************************************************************************
  12. ******************************************************************************/
  13. #include "lock_dlm.h"
  14. /* A lock placed on this queue is re-submitted to DLM as soon as the lock_dlm
  15. thread gets to it. */
  16. static void queue_submit(struct gdlm_lock *lp)
  17. {
  18. struct gdlm_ls *ls = lp->ls;
  19. spin_lock(&ls->async_lock);
  20. list_add_tail(&lp->delay_list, &ls->submit);
  21. spin_unlock(&ls->async_lock);
  22. wake_up(&ls->thread_wait);
  23. }
  24. static void process_submit(struct gdlm_lock *lp)
  25. {
  26. gdlm_do_lock(lp, NULL);
  27. }
  28. static void process_blocking(struct gdlm_lock *lp, int bast_mode)
  29. {
  30. struct gdlm_ls *ls = lp->ls;
  31. unsigned int cb;
  32. switch (gdlm_make_lmstate(bast_mode)) {
  33. case LM_ST_EXCLUSIVE:
  34. cb = LM_CB_NEED_E;
  35. break;
  36. case LM_ST_DEFERRED:
  37. cb = LM_CB_NEED_D;
  38. break;
  39. case LM_ST_SHARED:
  40. cb = LM_CB_NEED_S;
  41. break;
  42. default:
  43. GDLM_ASSERT(0, printk("unknown bast mode %u\n",lp->bast_mode););
  44. }
  45. ls->fscb(ls->fsdata, cb, &lp->lockname);
  46. }
  47. static void process_complete(struct gdlm_lock *lp)
  48. {
  49. struct gdlm_ls *ls = lp->ls;
  50. struct lm_async_cb acb;
  51. int16_t prev_mode = lp->cur;
  52. memset(&acb, 0, sizeof(acb));
  53. if (lp->lksb.sb_status == -DLM_ECANCEL) {
  54. log_all("complete dlm cancel %x,%"PRIx64" flags %lx",
  55. lp->lockname.ln_type, lp->lockname.ln_number,
  56. lp->flags);
  57. lp->req = lp->cur;
  58. acb.lc_ret |= LM_OUT_CANCELED;
  59. if (lp->cur == DLM_LOCK_IV)
  60. lp->lksb.sb_lkid = 0;
  61. goto out;
  62. }
  63. if (test_and_clear_bit(LFL_DLM_UNLOCK, &lp->flags)) {
  64. if (lp->lksb.sb_status != -DLM_EUNLOCK) {
  65. log_all("unlock sb_status %d %x,%"PRIx64" flags %lx",
  66. lp->lksb.sb_status, lp->lockname.ln_type,
  67. lp->lockname.ln_number, 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_all("complete internal cancel %x,%"PRIx64"",
  93. lp->lockname.ln_type, lp->lockname.ln_number);
  94. lp->req = lp->cur;
  95. acb.lc_ret |= LM_OUT_CANCELED;
  96. goto out;
  97. }
  98. /*
  99. * An error occured.
  100. */
  101. if (lp->lksb.sb_status) {
  102. /* a "normal" error */
  103. if ((lp->lksb.sb_status == -EAGAIN) &&
  104. (lp->lkf & DLM_LKF_NOQUEUE)) {
  105. lp->req = lp->cur;
  106. if (lp->cur == DLM_LOCK_IV)
  107. lp->lksb.sb_lkid = 0;
  108. goto out;
  109. }
  110. /* this could only happen with cancels I think */
  111. log_all("ast sb_status %d %x,%"PRIx64" flags %lx",
  112. lp->lksb.sb_status, lp->lockname.ln_type,
  113. lp->lockname.ln_number, lp->flags);
  114. return;
  115. }
  116. /*
  117. * This is an AST for an EX->EX conversion for sync_lvb from GFS.
  118. */
  119. if (test_and_clear_bit(LFL_SYNC_LVB, &lp->flags)) {
  120. complete(&lp->ast_wait);
  121. return;
  122. }
  123. /*
  124. * A lock has been demoted to NL because it initially completed during
  125. * BLOCK_LOCKS. Now it must be requested in the originally requested
  126. * mode.
  127. */
  128. if (test_and_clear_bit(LFL_REREQUEST, &lp->flags)) {
  129. GDLM_ASSERT(lp->req == DLM_LOCK_NL,);
  130. GDLM_ASSERT(lp->prev_req > DLM_LOCK_NL,);
  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,%"PRIx64" id %x %d,%d",
  160. lp->lockname.ln_type, lp->lockname.ln_number,
  161. lp->lksb.sb_lkid, lp->cur, lp->req);
  162. set_bit(LFL_REREQUEST, &lp->flags);
  163. queue_submit(lp);
  164. return;
  165. }
  166. /*
  167. * DLM demoted the lock to NL before it was granted so GFS must be
  168. * told it cannot cache data for this lock.
  169. */
  170. if (lp->lksb.sb_flags & DLM_SBF_DEMOTED)
  171. set_bit(LFL_NOCACHE, &lp->flags);
  172. out:
  173. /*
  174. * This is an internal lock_dlm lock
  175. */
  176. if (test_bit(LFL_INLOCK, &lp->flags)) {
  177. clear_bit(LFL_NOBLOCK, &lp->flags);
  178. lp->cur = lp->req;
  179. complete(&lp->ast_wait);
  180. return;
  181. }
  182. /*
  183. * Normal completion of a lock request. Tell GFS it now has the lock.
  184. */
  185. clear_bit(LFL_NOBLOCK, &lp->flags);
  186. lp->cur = lp->req;
  187. acb.lc_name = lp->lockname;
  188. acb.lc_ret |= gdlm_make_lmstate(lp->cur);
  189. if (!test_and_clear_bit(LFL_NOCACHE, &lp->flags) &&
  190. (lp->cur > DLM_LOCK_NL) && (prev_mode > DLM_LOCK_NL))
  191. acb.lc_ret |= LM_OUT_CACHEABLE;
  192. ls->fscb(ls->fsdata, LM_CB_ASYNC, &acb);
  193. }
  194. static inline int no_work(struct gdlm_ls *ls, int blocking)
  195. {
  196. int ret;
  197. spin_lock(&ls->async_lock);
  198. ret = list_empty(&ls->complete) && list_empty(&ls->submit);
  199. if (ret && blocking)
  200. ret = list_empty(&ls->blocking);
  201. spin_unlock(&ls->async_lock);
  202. return ret;
  203. }
  204. static inline int check_drop(struct gdlm_ls *ls)
  205. {
  206. if (!ls->drop_locks_count)
  207. return 0;
  208. if (time_after(jiffies, ls->drop_time + ls->drop_locks_period * HZ)) {
  209. ls->drop_time = jiffies;
  210. if (ls->all_locks_count >= ls->drop_locks_count)
  211. return 1;
  212. }
  213. return 0;
  214. }
  215. static int gdlm_thread(void *data)
  216. {
  217. struct gdlm_ls *ls = (struct gdlm_ls *) data;
  218. struct gdlm_lock *lp = NULL;
  219. int blist = 0;
  220. uint8_t complete, blocking, submit, drop;
  221. DECLARE_WAITQUEUE(wait, current);
  222. /* Only thread1 is allowed to do blocking callbacks since gfs
  223. may wait for a completion callback within a blocking cb. */
  224. if (current == ls->thread1)
  225. blist = 1;
  226. while (!kthread_should_stop()) {
  227. set_current_state(TASK_INTERRUPTIBLE);
  228. add_wait_queue(&ls->thread_wait, &wait);
  229. if (no_work(ls, blist))
  230. schedule();
  231. remove_wait_queue(&ls->thread_wait, &wait);
  232. set_current_state(TASK_RUNNING);
  233. complete = blocking = submit = drop = 0;
  234. spin_lock(&ls->async_lock);
  235. if (blist && !list_empty(&ls->blocking)) {
  236. lp = list_entry(ls->blocking.next, struct gdlm_lock,
  237. blist);
  238. list_del_init(&lp->blist);
  239. blocking = lp->bast_mode;
  240. lp->bast_mode = 0;
  241. } else if (!list_empty(&ls->complete)) {
  242. lp = list_entry(ls->complete.next, struct gdlm_lock,
  243. clist);
  244. list_del_init(&lp->clist);
  245. complete = 1;
  246. } else if (!list_empty(&ls->submit)) {
  247. lp = list_entry(ls->submit.next, struct gdlm_lock,
  248. delay_list);
  249. list_del_init(&lp->delay_list);
  250. submit = 1;
  251. }
  252. drop = check_drop(ls);
  253. spin_unlock(&ls->async_lock);
  254. if (complete)
  255. process_complete(lp);
  256. else if (blocking)
  257. process_blocking(lp, blocking);
  258. else if (submit)
  259. process_submit(lp);
  260. if (drop)
  261. ls->fscb(ls->fsdata, LM_CB_DROPLOCKS, NULL);
  262. schedule();
  263. }
  264. return 0;
  265. }
  266. int gdlm_init_threads(struct gdlm_ls *ls)
  267. {
  268. struct task_struct *p;
  269. int error;
  270. p = kthread_run(gdlm_thread, ls, "lock_dlm1");
  271. error = IS_ERR(p);
  272. if (error) {
  273. log_all("can't start lock_dlm1 thread %d", error);
  274. return error;
  275. }
  276. ls->thread1 = p;
  277. p = kthread_run(gdlm_thread, ls, "lock_dlm2");
  278. error = IS_ERR(p);
  279. if (error) {
  280. log_all("can't start lock_dlm2 thread %d", error);
  281. kthread_stop(ls->thread1);
  282. return error;
  283. }
  284. ls->thread2 = p;
  285. return 0;
  286. }
  287. void gdlm_release_threads(struct gdlm_ls *ls)
  288. {
  289. kthread_stop(ls->thread1);
  290. kthread_stop(ls->thread2);
  291. }