thread.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. if (lp->lksb.sb_status == -EDEADLOCK &&
  117. lp->ls->fsflags & LM_MFLAG_CONV_NODROP) {
  118. lp->req = lp->cur;
  119. acb.lc_ret |= LM_OUT_CONV_DEADLK;
  120. if (lp->cur == DLM_LOCK_IV)
  121. lp->lksb.sb_lkid = 0;
  122. goto out;
  123. } else
  124. return;
  125. }
  126. /*
  127. * This is an AST for an EX->EX conversion for sync_lvb from GFS.
  128. */
  129. if (test_and_clear_bit(LFL_SYNC_LVB, &lp->flags)) {
  130. wake_up_ast(lp);
  131. return;
  132. }
  133. /*
  134. * A lock has been demoted to NL because it initially completed during
  135. * BLOCK_LOCKS. Now it must be requested in the originally requested
  136. * mode.
  137. */
  138. if (test_and_clear_bit(LFL_REREQUEST, &lp->flags)) {
  139. gdlm_assert(lp->req == DLM_LOCK_NL, "%x,%llx",
  140. lp->lockname.ln_type,
  141. (unsigned long long)lp->lockname.ln_number);
  142. gdlm_assert(lp->prev_req > DLM_LOCK_NL, "%x,%llx",
  143. lp->lockname.ln_type,
  144. (unsigned long long)lp->lockname.ln_number);
  145. lp->cur = DLM_LOCK_NL;
  146. lp->req = lp->prev_req;
  147. lp->prev_req = DLM_LOCK_IV;
  148. lp->lkf &= ~DLM_LKF_CONVDEADLK;
  149. set_bit(LFL_NOCACHE, &lp->flags);
  150. if (test_bit(DFL_BLOCK_LOCKS, &ls->flags) &&
  151. !test_bit(LFL_NOBLOCK, &lp->flags))
  152. gdlm_queue_delayed(lp);
  153. else
  154. queue_submit(lp);
  155. return;
  156. }
  157. /*
  158. * A request is granted during dlm recovery. It may be granted
  159. * because the locks of a failed node were cleared. In that case,
  160. * there may be inconsistent data beneath this lock and we must wait
  161. * for recovery to complete to use it. When gfs recovery is done this
  162. * granted lock will be converted to NL and then reacquired in this
  163. * granted state.
  164. */
  165. if (test_bit(DFL_BLOCK_LOCKS, &ls->flags) &&
  166. !test_bit(LFL_NOBLOCK, &lp->flags) &&
  167. lp->req != DLM_LOCK_NL) {
  168. lp->cur = lp->req;
  169. lp->prev_req = lp->req;
  170. lp->req = DLM_LOCK_NL;
  171. lp->lkf |= DLM_LKF_CONVERT;
  172. lp->lkf &= ~DLM_LKF_CONVDEADLK;
  173. log_debug("rereq %x,%llx id %x %d,%d",
  174. lp->lockname.ln_type,
  175. (unsigned long long)lp->lockname.ln_number,
  176. lp->lksb.sb_lkid, lp->cur, lp->req);
  177. set_bit(LFL_REREQUEST, &lp->flags);
  178. queue_submit(lp);
  179. return;
  180. }
  181. /*
  182. * DLM demoted the lock to NL before it was granted so GFS must be
  183. * told it cannot cache data for this lock.
  184. */
  185. if (lp->lksb.sb_flags & DLM_SBF_DEMOTED)
  186. set_bit(LFL_NOCACHE, &lp->flags);
  187. out:
  188. /*
  189. * This is an internal lock_dlm lock
  190. */
  191. if (test_bit(LFL_INLOCK, &lp->flags)) {
  192. clear_bit(LFL_NOBLOCK, &lp->flags);
  193. lp->cur = lp->req;
  194. wake_up_ast(lp);
  195. return;
  196. }
  197. /*
  198. * Normal completion of a lock request. Tell GFS it now has the lock.
  199. */
  200. clear_bit(LFL_NOBLOCK, &lp->flags);
  201. lp->cur = lp->req;
  202. acb.lc_name = lp->lockname;
  203. acb.lc_ret |= gdlm_make_lmstate(lp->cur);
  204. if (!test_and_clear_bit(LFL_NOCACHE, &lp->flags) &&
  205. (lp->cur > DLM_LOCK_NL) && (prev_mode > DLM_LOCK_NL))
  206. acb.lc_ret |= LM_OUT_CACHEABLE;
  207. ls->fscb(ls->sdp, LM_CB_ASYNC, &acb);
  208. }
  209. static inline int no_work(struct gdlm_ls *ls, int blocking)
  210. {
  211. int ret;
  212. spin_lock(&ls->async_lock);
  213. ret = list_empty(&ls->complete) && list_empty(&ls->submit);
  214. if (ret && blocking)
  215. ret = list_empty(&ls->blocking);
  216. spin_unlock(&ls->async_lock);
  217. return ret;
  218. }
  219. static inline int check_drop(struct gdlm_ls *ls)
  220. {
  221. if (!ls->drop_locks_count)
  222. return 0;
  223. if (time_after(jiffies, ls->drop_time + ls->drop_locks_period * HZ)) {
  224. ls->drop_time = jiffies;
  225. if (ls->all_locks_count >= ls->drop_locks_count)
  226. return 1;
  227. }
  228. return 0;
  229. }
  230. static int gdlm_thread(void *data, int blist)
  231. {
  232. struct gdlm_ls *ls = (struct gdlm_ls *) data;
  233. struct gdlm_lock *lp = NULL;
  234. uint8_t complete, blocking, submit, drop;
  235. /* Only thread1 is allowed to do blocking callbacks since gfs
  236. may wait for a completion callback within a blocking cb. */
  237. while (!kthread_should_stop()) {
  238. wait_event_interruptible(ls->thread_wait,
  239. !no_work(ls, blist) || kthread_should_stop());
  240. complete = blocking = submit = drop = 0;
  241. spin_lock(&ls->async_lock);
  242. if (blist && !list_empty(&ls->blocking)) {
  243. lp = list_entry(ls->blocking.next, struct gdlm_lock,
  244. blist);
  245. list_del_init(&lp->blist);
  246. blocking = lp->bast_mode;
  247. lp->bast_mode = 0;
  248. } else if (!list_empty(&ls->complete)) {
  249. lp = list_entry(ls->complete.next, struct gdlm_lock,
  250. clist);
  251. list_del_init(&lp->clist);
  252. complete = 1;
  253. } else if (!list_empty(&ls->submit)) {
  254. lp = list_entry(ls->submit.next, struct gdlm_lock,
  255. delay_list);
  256. list_del_init(&lp->delay_list);
  257. submit = 1;
  258. }
  259. drop = check_drop(ls);
  260. spin_unlock(&ls->async_lock);
  261. if (complete)
  262. process_complete(lp);
  263. else if (blocking)
  264. process_blocking(lp, blocking);
  265. else if (submit)
  266. gdlm_do_lock(lp);
  267. if (drop)
  268. ls->fscb(ls->sdp, LM_CB_DROPLOCKS, NULL);
  269. schedule();
  270. }
  271. return 0;
  272. }
  273. static int gdlm_thread1(void *data)
  274. {
  275. return gdlm_thread(data, 1);
  276. }
  277. static int gdlm_thread2(void *data)
  278. {
  279. return gdlm_thread(data, 0);
  280. }
  281. int gdlm_init_threads(struct gdlm_ls *ls)
  282. {
  283. struct task_struct *p;
  284. int error;
  285. p = kthread_run(gdlm_thread1, ls, "lock_dlm1");
  286. error = IS_ERR(p);
  287. if (error) {
  288. log_error("can't start lock_dlm1 thread %d", error);
  289. return error;
  290. }
  291. ls->thread1 = p;
  292. p = kthread_run(gdlm_thread2, ls, "lock_dlm2");
  293. error = IS_ERR(p);
  294. if (error) {
  295. log_error("can't start lock_dlm2 thread %d", error);
  296. kthread_stop(ls->thread1);
  297. return error;
  298. }
  299. ls->thread2 = p;
  300. return 0;
  301. }
  302. void gdlm_release_threads(struct gdlm_ls *ls)
  303. {
  304. kthread_stop(ls->thread1);
  305. kthread_stop(ls->thread2);
  306. }