lock.c 12 KB

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