lock.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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. static char junk_lvb[GDLM_LVB_SIZE];
  11. static void queue_complete(struct gdlm_lock *lp)
  12. {
  13. struct gdlm_ls *ls = lp->ls;
  14. clear_bit(LFL_ACTIVE, &lp->flags);
  15. spin_lock(&ls->async_lock);
  16. list_add_tail(&lp->clist, &ls->complete);
  17. spin_unlock(&ls->async_lock);
  18. wake_up(&ls->thread_wait);
  19. }
  20. static inline void gdlm_ast(void *astarg)
  21. {
  22. queue_complete(astarg);
  23. }
  24. static inline void gdlm_bast(void *astarg, int mode)
  25. {
  26. struct gdlm_lock *lp = astarg;
  27. struct gdlm_ls *ls = lp->ls;
  28. if (!mode) {
  29. printk(KERN_INFO "lock_dlm: bast mode zero %x,%llx\n",
  30. lp->lockname.ln_type,
  31. (unsigned long long)lp->lockname.ln_number);
  32. return;
  33. }
  34. spin_lock(&ls->async_lock);
  35. if (!lp->bast_mode) {
  36. list_add_tail(&lp->blist, &ls->blocking);
  37. lp->bast_mode = mode;
  38. } else if (lp->bast_mode < mode)
  39. lp->bast_mode = mode;
  40. spin_unlock(&ls->async_lock);
  41. wake_up(&ls->thread_wait);
  42. }
  43. void gdlm_queue_delayed(struct gdlm_lock *lp)
  44. {
  45. struct gdlm_ls *ls = lp->ls;
  46. spin_lock(&ls->async_lock);
  47. list_add_tail(&lp->delay_list, &ls->delayed);
  48. spin_unlock(&ls->async_lock);
  49. }
  50. /* convert gfs lock-state to dlm lock-mode */
  51. static s16 make_mode(s16 lmstate)
  52. {
  53. switch (lmstate) {
  54. case LM_ST_UNLOCKED:
  55. return DLM_LOCK_NL;
  56. case LM_ST_EXCLUSIVE:
  57. return DLM_LOCK_EX;
  58. case LM_ST_DEFERRED:
  59. return DLM_LOCK_CW;
  60. case LM_ST_SHARED:
  61. return DLM_LOCK_PR;
  62. }
  63. gdlm_assert(0, "unknown LM state %d", lmstate);
  64. return -1;
  65. }
  66. /* convert dlm lock-mode to gfs lock-state */
  67. s16 gdlm_make_lmstate(s16 dlmmode)
  68. {
  69. switch (dlmmode) {
  70. case DLM_LOCK_IV:
  71. case DLM_LOCK_NL:
  72. return LM_ST_UNLOCKED;
  73. case DLM_LOCK_EX:
  74. return LM_ST_EXCLUSIVE;
  75. case DLM_LOCK_CW:
  76. return LM_ST_DEFERRED;
  77. case DLM_LOCK_PR:
  78. return LM_ST_SHARED;
  79. }
  80. gdlm_assert(0, "unknown DLM mode %d", dlmmode);
  81. return -1;
  82. }
  83. /* verify agreement with GFS on the current lock state, NB: DLM_LOCK_NL and
  84. DLM_LOCK_IV are both considered LM_ST_UNLOCKED by GFS. */
  85. static void check_cur_state(struct gdlm_lock *lp, unsigned int cur_state)
  86. {
  87. s16 cur = make_mode(cur_state);
  88. if (lp->cur != DLM_LOCK_IV)
  89. gdlm_assert(lp->cur == cur, "%d, %d", lp->cur, cur);
  90. }
  91. static inline unsigned int make_flags(struct gdlm_lock *lp,
  92. unsigned int gfs_flags,
  93. s16 cur, s16 req)
  94. {
  95. unsigned int lkf = 0;
  96. if (gfs_flags & LM_FLAG_TRY)
  97. lkf |= DLM_LKF_NOQUEUE;
  98. if (gfs_flags & LM_FLAG_TRY_1CB) {
  99. lkf |= DLM_LKF_NOQUEUE;
  100. lkf |= DLM_LKF_NOQUEUEBAST;
  101. }
  102. if (gfs_flags & LM_FLAG_PRIORITY) {
  103. lkf |= DLM_LKF_NOORDER;
  104. lkf |= DLM_LKF_HEADQUE;
  105. }
  106. if (gfs_flags & LM_FLAG_ANY) {
  107. if (req == DLM_LOCK_PR)
  108. lkf |= DLM_LKF_ALTCW;
  109. else if (req == DLM_LOCK_CW)
  110. lkf |= DLM_LKF_ALTPR;
  111. }
  112. if (lp->lksb.sb_lkid != 0) {
  113. lkf |= DLM_LKF_CONVERT;
  114. /* Conversion deadlock avoidance by DLM */
  115. if (!test_bit(LFL_FORCE_PROMOTE, &lp->flags) &&
  116. !(lkf & DLM_LKF_NOQUEUE) &&
  117. cur > DLM_LOCK_NL && req > DLM_LOCK_NL && cur != req)
  118. lkf |= DLM_LKF_CONVDEADLK;
  119. }
  120. if (lp->lvb)
  121. lkf |= DLM_LKF_VALBLK;
  122. return lkf;
  123. }
  124. /* make_strname - convert GFS lock numbers to a string */
  125. static inline void make_strname(const struct lm_lockname *lockname,
  126. struct gdlm_strname *str)
  127. {
  128. sprintf(str->name, "%8x%16llx", lockname->ln_type,
  129. (unsigned long long)lockname->ln_number);
  130. str->namelen = GDLM_STRNAME_BYTES;
  131. }
  132. static int gdlm_create_lp(struct gdlm_ls *ls, struct lm_lockname *name,
  133. struct gdlm_lock **lpp)
  134. {
  135. struct gdlm_lock *lp;
  136. lp = kzalloc(sizeof(struct gdlm_lock), GFP_KERNEL);
  137. if (!lp)
  138. return -ENOMEM;
  139. lp->lockname = *name;
  140. make_strname(name, &lp->strname);
  141. lp->ls = ls;
  142. lp->cur = DLM_LOCK_IV;
  143. lp->lvb = NULL;
  144. lp->hold_null = NULL;
  145. INIT_LIST_HEAD(&lp->clist);
  146. INIT_LIST_HEAD(&lp->blist);
  147. INIT_LIST_HEAD(&lp->delay_list);
  148. spin_lock(&ls->async_lock);
  149. list_add(&lp->all_list, &ls->all_locks);
  150. ls->all_locks_count++;
  151. spin_unlock(&ls->async_lock);
  152. *lpp = lp;
  153. return 0;
  154. }
  155. void gdlm_delete_lp(struct gdlm_lock *lp)
  156. {
  157. struct gdlm_ls *ls = lp->ls;
  158. spin_lock(&ls->async_lock);
  159. if (!list_empty(&lp->clist))
  160. list_del_init(&lp->clist);
  161. if (!list_empty(&lp->blist))
  162. list_del_init(&lp->blist);
  163. if (!list_empty(&lp->delay_list))
  164. list_del_init(&lp->delay_list);
  165. gdlm_assert(!list_empty(&lp->all_list), "%x,%llx", lp->lockname.ln_type,
  166. (unsigned long long)lp->lockname.ln_number);
  167. list_del_init(&lp->all_list);
  168. ls->all_locks_count--;
  169. spin_unlock(&ls->async_lock);
  170. kfree(lp);
  171. }
  172. int gdlm_get_lock(void *lockspace, struct lm_lockname *name,
  173. void **lockp)
  174. {
  175. struct gdlm_lock *lp;
  176. int error;
  177. error = gdlm_create_lp(lockspace, name, &lp);
  178. *lockp = lp;
  179. return error;
  180. }
  181. void gdlm_put_lock(void *lock)
  182. {
  183. gdlm_delete_lp(lock);
  184. }
  185. unsigned int gdlm_do_lock(struct gdlm_lock *lp)
  186. {
  187. struct gdlm_ls *ls = lp->ls;
  188. int error, bast = 1;
  189. /*
  190. * When recovery is in progress, delay lock requests for submission
  191. * once recovery is done. Requests for recovery (NOEXP) and unlocks
  192. * can pass.
  193. */
  194. if (test_bit(DFL_BLOCK_LOCKS, &ls->flags) &&
  195. !test_bit(LFL_NOBLOCK, &lp->flags) && lp->req != DLM_LOCK_NL) {
  196. gdlm_queue_delayed(lp);
  197. return LM_OUT_ASYNC;
  198. }
  199. /*
  200. * Submit the actual lock request.
  201. */
  202. if (test_bit(LFL_NOBAST, &lp->flags))
  203. bast = 0;
  204. set_bit(LFL_ACTIVE, &lp->flags);
  205. log_debug("lk %x,%llx id %x %d,%d %x", lp->lockname.ln_type,
  206. (unsigned long long)lp->lockname.ln_number, lp->lksb.sb_lkid,
  207. lp->cur, lp->req, lp->lkf);
  208. error = dlm_lock(ls->dlm_lockspace, lp->req, &lp->lksb, lp->lkf,
  209. lp->strname.name, lp->strname.namelen, 0, gdlm_ast,
  210. lp, bast ? gdlm_bast : NULL);
  211. if ((error == -EAGAIN) && (lp->lkf & DLM_LKF_NOQUEUE)) {
  212. lp->lksb.sb_status = -EAGAIN;
  213. queue_complete(lp);
  214. error = 0;
  215. }
  216. if (error) {
  217. log_error("%s: gdlm_lock %x,%llx err=%d cur=%d req=%d lkf=%x "
  218. "flags=%lx", ls->fsname, lp->lockname.ln_type,
  219. (unsigned long long)lp->lockname.ln_number, error,
  220. lp->cur, lp->req, lp->lkf, lp->flags);
  221. return LM_OUT_ERROR;
  222. }
  223. return LM_OUT_ASYNC;
  224. }
  225. static unsigned int gdlm_do_unlock(struct gdlm_lock *lp)
  226. {
  227. struct gdlm_ls *ls = lp->ls;
  228. unsigned int lkf = 0;
  229. int error;
  230. set_bit(LFL_DLM_UNLOCK, &lp->flags);
  231. set_bit(LFL_ACTIVE, &lp->flags);
  232. if (lp->lvb)
  233. lkf = DLM_LKF_VALBLK;
  234. log_debug("un %x,%llx %x %d %x", lp->lockname.ln_type,
  235. (unsigned long long)lp->lockname.ln_number,
  236. lp->lksb.sb_lkid, lp->cur, lkf);
  237. error = dlm_unlock(ls->dlm_lockspace, lp->lksb.sb_lkid, lkf, NULL, lp);
  238. if (error) {
  239. log_error("%s: gdlm_unlock %x,%llx err=%d cur=%d req=%d lkf=%x "
  240. "flags=%lx", ls->fsname, lp->lockname.ln_type,
  241. (unsigned long long)lp->lockname.ln_number, error,
  242. lp->cur, lp->req, lp->lkf, lp->flags);
  243. return LM_OUT_ERROR;
  244. }
  245. return LM_OUT_ASYNC;
  246. }
  247. unsigned int gdlm_lock(void *lock, unsigned int cur_state,
  248. unsigned int req_state, unsigned int flags)
  249. {
  250. struct gdlm_lock *lp = lock;
  251. clear_bit(LFL_DLM_CANCEL, &lp->flags);
  252. if (flags & LM_FLAG_NOEXP)
  253. set_bit(LFL_NOBLOCK, &lp->flags);
  254. check_cur_state(lp, cur_state);
  255. lp->req = make_mode(req_state);
  256. lp->lkf = make_flags(lp, flags, lp->cur, lp->req);
  257. return gdlm_do_lock(lp);
  258. }
  259. unsigned int gdlm_unlock(void *lock, unsigned int cur_state)
  260. {
  261. struct gdlm_lock *lp = lock;
  262. clear_bit(LFL_DLM_CANCEL, &lp->flags);
  263. if (lp->cur == DLM_LOCK_IV)
  264. return 0;
  265. return gdlm_do_unlock(lp);
  266. }
  267. void gdlm_cancel(void *lock)
  268. {
  269. struct gdlm_lock *lp = lock;
  270. struct gdlm_ls *ls = lp->ls;
  271. int error, delay_list = 0;
  272. if (test_bit(LFL_DLM_CANCEL, &lp->flags))
  273. return;
  274. log_info("gdlm_cancel %x,%llx flags %lx", lp->lockname.ln_type,
  275. (unsigned long long)lp->lockname.ln_number, lp->flags);
  276. spin_lock(&ls->async_lock);
  277. if (!list_empty(&lp->delay_list)) {
  278. list_del_init(&lp->delay_list);
  279. delay_list = 1;
  280. }
  281. spin_unlock(&ls->async_lock);
  282. if (delay_list) {
  283. set_bit(LFL_CANCEL, &lp->flags);
  284. set_bit(LFL_ACTIVE, &lp->flags);
  285. queue_complete(lp);
  286. return;
  287. }
  288. if (!test_bit(LFL_ACTIVE, &lp->flags) ||
  289. test_bit(LFL_DLM_UNLOCK, &lp->flags)) {
  290. log_info("gdlm_cancel skip %x,%llx flags %lx",
  291. lp->lockname.ln_type,
  292. (unsigned long long)lp->lockname.ln_number, lp->flags);
  293. return;
  294. }
  295. /* the lock is blocked in the dlm */
  296. set_bit(LFL_DLM_CANCEL, &lp->flags);
  297. set_bit(LFL_ACTIVE, &lp->flags);
  298. error = dlm_unlock(ls->dlm_lockspace, lp->lksb.sb_lkid, DLM_LKF_CANCEL,
  299. NULL, lp);
  300. log_info("gdlm_cancel rv %d %x,%llx flags %lx", error,
  301. lp->lockname.ln_type,
  302. (unsigned long long)lp->lockname.ln_number, lp->flags);
  303. if (error == -EBUSY)
  304. clear_bit(LFL_DLM_CANCEL, &lp->flags);
  305. }
  306. static int gdlm_add_lvb(struct gdlm_lock *lp)
  307. {
  308. char *lvb;
  309. lvb = kzalloc(GDLM_LVB_SIZE, GFP_KERNEL);
  310. if (!lvb)
  311. return -ENOMEM;
  312. lp->lksb.sb_lvbptr = lvb;
  313. lp->lvb = lvb;
  314. return 0;
  315. }
  316. static void gdlm_del_lvb(struct gdlm_lock *lp)
  317. {
  318. kfree(lp->lvb);
  319. lp->lvb = NULL;
  320. lp->lksb.sb_lvbptr = NULL;
  321. }
  322. static int gdlm_ast_wait(void *word)
  323. {
  324. schedule();
  325. return 0;
  326. }
  327. /* This can do a synchronous dlm request (requiring a lock_dlm thread to get
  328. the completion) because gfs won't call hold_lvb() during a callback (from
  329. the context of a lock_dlm thread). */
  330. static int hold_null_lock(struct gdlm_lock *lp)
  331. {
  332. struct gdlm_lock *lpn = NULL;
  333. int error;
  334. if (lp->hold_null) {
  335. printk(KERN_INFO "lock_dlm: lvb already held\n");
  336. return 0;
  337. }
  338. error = gdlm_create_lp(lp->ls, &lp->lockname, &lpn);
  339. if (error)
  340. goto out;
  341. lpn->lksb.sb_lvbptr = junk_lvb;
  342. lpn->lvb = junk_lvb;
  343. lpn->req = DLM_LOCK_NL;
  344. lpn->lkf = DLM_LKF_VALBLK | DLM_LKF_EXPEDITE;
  345. set_bit(LFL_NOBAST, &lpn->flags);
  346. set_bit(LFL_INLOCK, &lpn->flags);
  347. set_bit(LFL_AST_WAIT, &lpn->flags);
  348. gdlm_do_lock(lpn);
  349. wait_on_bit(&lpn->flags, LFL_AST_WAIT, gdlm_ast_wait, TASK_UNINTERRUPTIBLE);
  350. error = lpn->lksb.sb_status;
  351. if (error) {
  352. printk(KERN_INFO "lock_dlm: hold_null_lock dlm error %d\n",
  353. error);
  354. gdlm_delete_lp(lpn);
  355. lpn = NULL;
  356. }
  357. out:
  358. lp->hold_null = lpn;
  359. return error;
  360. }
  361. /* This cannot do a synchronous dlm request (requiring a lock_dlm thread to get
  362. the completion) because gfs may call unhold_lvb() during a callback (from
  363. the context of a lock_dlm thread) which could cause a deadlock since the
  364. other lock_dlm thread could be engaged in recovery. */
  365. static void unhold_null_lock(struct gdlm_lock *lp)
  366. {
  367. struct gdlm_lock *lpn = lp->hold_null;
  368. gdlm_assert(lpn, "%x,%llx", lp->lockname.ln_type,
  369. (unsigned long long)lp->lockname.ln_number);
  370. lpn->lksb.sb_lvbptr = NULL;
  371. lpn->lvb = NULL;
  372. set_bit(LFL_UNLOCK_DELETE, &lpn->flags);
  373. gdlm_do_unlock(lpn);
  374. lp->hold_null = NULL;
  375. }
  376. /* Acquire a NL lock because gfs requires the value block to remain
  377. intact on the resource while the lvb is "held" even if it's holding no locks
  378. on the resource. */
  379. int gdlm_hold_lvb(void *lock, char **lvbp)
  380. {
  381. struct gdlm_lock *lp = lock;
  382. int error;
  383. error = gdlm_add_lvb(lp);
  384. if (error)
  385. return error;
  386. *lvbp = lp->lvb;
  387. error = hold_null_lock(lp);
  388. if (error)
  389. gdlm_del_lvb(lp);
  390. return error;
  391. }
  392. void gdlm_unhold_lvb(void *lock, char *lvb)
  393. {
  394. struct gdlm_lock *lp = lock;
  395. unhold_null_lock(lp);
  396. gdlm_del_lvb(lp);
  397. }
  398. void gdlm_submit_delayed(struct gdlm_ls *ls)
  399. {
  400. struct gdlm_lock *lp, *safe;
  401. spin_lock(&ls->async_lock);
  402. list_for_each_entry_safe(lp, safe, &ls->delayed, delay_list) {
  403. list_del_init(&lp->delay_list);
  404. list_add_tail(&lp->delay_list, &ls->submit);
  405. }
  406. spin_unlock(&ls->async_lock);
  407. wake_up(&ls->thread_wait);
  408. }
  409. int gdlm_release_all_locks(struct gdlm_ls *ls)
  410. {
  411. struct gdlm_lock *lp, *safe;
  412. int count = 0;
  413. spin_lock(&ls->async_lock);
  414. list_for_each_entry_safe(lp, safe, &ls->all_locks, all_list) {
  415. list_del_init(&lp->all_list);
  416. if (lp->lvb && lp->lvb != junk_lvb)
  417. kfree(lp->lvb);
  418. kfree(lp);
  419. count++;
  420. }
  421. spin_unlock(&ls->async_lock);
  422. return count;
  423. }