lock.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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_completion(&lp->ast_wait);
  146. INIT_LIST_HEAD(&lp->clist);
  147. INIT_LIST_HEAD(&lp->blist);
  148. INIT_LIST_HEAD(&lp->delay_list);
  149. spin_lock(&ls->async_lock);
  150. list_add(&lp->all_list, &ls->all_locks);
  151. ls->all_locks_count++;
  152. spin_unlock(&ls->async_lock);
  153. *lpp = lp;
  154. return 0;
  155. }
  156. void gdlm_delete_lp(struct gdlm_lock *lp)
  157. {
  158. struct gdlm_ls *ls = lp->ls;
  159. spin_lock(&ls->async_lock);
  160. if (!list_empty(&lp->clist))
  161. list_del_init(&lp->clist);
  162. if (!list_empty(&lp->blist))
  163. list_del_init(&lp->blist);
  164. if (!list_empty(&lp->delay_list))
  165. list_del_init(&lp->delay_list);
  166. gdlm_assert(!list_empty(&lp->all_list), "%x,%llx", lp->lockname.ln_type,
  167. (unsigned long long)lp->lockname.ln_number);
  168. list_del_init(&lp->all_list);
  169. ls->all_locks_count--;
  170. spin_unlock(&ls->async_lock);
  171. kfree(lp);
  172. }
  173. int gdlm_get_lock(void *lockspace, struct lm_lockname *name,
  174. void **lockp)
  175. {
  176. struct gdlm_lock *lp;
  177. int error;
  178. error = gdlm_create_lp(lockspace, name, &lp);
  179. *lockp = lp;
  180. return error;
  181. }
  182. void gdlm_put_lock(void *lock)
  183. {
  184. gdlm_delete_lp(lock);
  185. }
  186. unsigned int gdlm_do_lock(struct gdlm_lock *lp)
  187. {
  188. struct gdlm_ls *ls = lp->ls;
  189. int error, bast = 1;
  190. /*
  191. * When recovery is in progress, delay lock requests for submission
  192. * once recovery is done. Requests for recovery (NOEXP) and unlocks
  193. * can pass.
  194. */
  195. if (test_bit(DFL_BLOCK_LOCKS, &ls->flags) &&
  196. !test_bit(LFL_NOBLOCK, &lp->flags) && lp->req != DLM_LOCK_NL) {
  197. gdlm_queue_delayed(lp);
  198. return LM_OUT_ASYNC;
  199. }
  200. /*
  201. * Submit the actual lock request.
  202. */
  203. if (test_bit(LFL_NOBAST, &lp->flags))
  204. bast = 0;
  205. set_bit(LFL_ACTIVE, &lp->flags);
  206. log_debug("lk %x,%llx id %x %d,%d %x", lp->lockname.ln_type,
  207. (unsigned long long)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. lp->strname.name, lp->strname.namelen, 0, gdlm_ast,
  211. lp, 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_error("%s: gdlm_lock %x,%llx err=%d cur=%d req=%d lkf=%x "
  219. "flags=%lx", ls->fsname, lp->lockname.ln_type,
  220. (unsigned long long)lp->lockname.ln_number, error,
  221. lp->cur, lp->req, 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. (unsigned long long)lp->lockname.ln_number,
  237. lp->lksb.sb_lkid, lp->cur, lkf);
  238. error = dlm_unlock(ls->dlm_lockspace, lp->lksb.sb_lkid, lkf, NULL, lp);
  239. if (error) {
  240. log_error("%s: gdlm_unlock %x,%llx err=%d cur=%d req=%d lkf=%x "
  241. "flags=%lx", ls->fsname, lp->lockname.ln_type,
  242. (unsigned long long)lp->lockname.ln_number, error,
  243. lp->cur, lp->req, lp->lkf, lp->flags);
  244. return LM_OUT_ERROR;
  245. }
  246. return LM_OUT_ASYNC;
  247. }
  248. unsigned int gdlm_lock(void *lock, unsigned int cur_state,
  249. unsigned int req_state, unsigned int flags)
  250. {
  251. struct gdlm_lock *lp = lock;
  252. clear_bit(LFL_DLM_CANCEL, &lp->flags);
  253. if (flags & LM_FLAG_NOEXP)
  254. set_bit(LFL_NOBLOCK, &lp->flags);
  255. check_cur_state(lp, cur_state);
  256. lp->req = make_mode(req_state);
  257. lp->lkf = make_flags(lp, flags, lp->cur, lp->req);
  258. return gdlm_do_lock(lp);
  259. }
  260. unsigned int gdlm_unlock(void *lock, unsigned int cur_state)
  261. {
  262. struct gdlm_lock *lp = lock;
  263. clear_bit(LFL_DLM_CANCEL, &lp->flags);
  264. if (lp->cur == DLM_LOCK_IV)
  265. return 0;
  266. return gdlm_do_unlock(lp);
  267. }
  268. void gdlm_cancel(void *lock)
  269. {
  270. struct gdlm_lock *lp = lock;
  271. struct gdlm_ls *ls = lp->ls;
  272. int error, delay_list = 0;
  273. if (test_bit(LFL_DLM_CANCEL, &lp->flags))
  274. return;
  275. log_info("gdlm_cancel %x,%llx flags %lx", lp->lockname.ln_type,
  276. (unsigned long long)lp->lockname.ln_number, lp->flags);
  277. spin_lock(&ls->async_lock);
  278. if (!list_empty(&lp->delay_list)) {
  279. list_del_init(&lp->delay_list);
  280. delay_list = 1;
  281. }
  282. spin_unlock(&ls->async_lock);
  283. if (delay_list) {
  284. set_bit(LFL_CANCEL, &lp->flags);
  285. set_bit(LFL_ACTIVE, &lp->flags);
  286. queue_complete(lp);
  287. return;
  288. }
  289. if (!test_bit(LFL_ACTIVE, &lp->flags) ||
  290. test_bit(LFL_DLM_UNLOCK, &lp->flags)) {
  291. log_info("gdlm_cancel skip %x,%llx flags %lx",
  292. lp->lockname.ln_type,
  293. (unsigned long long)lp->lockname.ln_number, lp->flags);
  294. return;
  295. }
  296. /* the lock is blocked in the dlm */
  297. set_bit(LFL_DLM_CANCEL, &lp->flags);
  298. set_bit(LFL_ACTIVE, &lp->flags);
  299. error = dlm_unlock(ls->dlm_lockspace, lp->lksb.sb_lkid, DLM_LKF_CANCEL,
  300. NULL, lp);
  301. log_info("gdlm_cancel rv %d %x,%llx flags %lx", error,
  302. lp->lockname.ln_type,
  303. (unsigned long long)lp->lockname.ln_number, lp->flags);
  304. if (error == -EBUSY)
  305. clear_bit(LFL_DLM_CANCEL, &lp->flags);
  306. }
  307. static int gdlm_add_lvb(struct gdlm_lock *lp)
  308. {
  309. char *lvb;
  310. lvb = kzalloc(GDLM_LVB_SIZE, GFP_KERNEL);
  311. if (!lvb)
  312. return -ENOMEM;
  313. lp->lksb.sb_lvbptr = lvb;
  314. lp->lvb = lvb;
  315. return 0;
  316. }
  317. static void gdlm_del_lvb(struct gdlm_lock *lp)
  318. {
  319. kfree(lp->lvb);
  320. lp->lvb = NULL;
  321. lp->lksb.sb_lvbptr = NULL;
  322. }
  323. /* This can do a synchronous dlm request (requiring a lock_dlm thread to get
  324. the completion) because gfs won't call hold_lvb() during a callback (from
  325. the context of a lock_dlm thread). */
  326. static int hold_null_lock(struct gdlm_lock *lp)
  327. {
  328. struct gdlm_lock *lpn = NULL;
  329. int error;
  330. if (lp->hold_null) {
  331. printk(KERN_INFO "lock_dlm: lvb already held\n");
  332. return 0;
  333. }
  334. error = gdlm_create_lp(lp->ls, &lp->lockname, &lpn);
  335. if (error)
  336. goto out;
  337. lpn->lksb.sb_lvbptr = junk_lvb;
  338. lpn->lvb = junk_lvb;
  339. lpn->req = DLM_LOCK_NL;
  340. lpn->lkf = DLM_LKF_VALBLK | DLM_LKF_EXPEDITE;
  341. set_bit(LFL_NOBAST, &lpn->flags);
  342. set_bit(LFL_INLOCK, &lpn->flags);
  343. init_completion(&lpn->ast_wait);
  344. gdlm_do_lock(lpn);
  345. wait_for_completion(&lpn->ast_wait);
  346. error = lpn->lksb.sb_status;
  347. if (error) {
  348. printk(KERN_INFO "lock_dlm: hold_null_lock dlm error %d\n",
  349. error);
  350. gdlm_delete_lp(lpn);
  351. lpn = NULL;
  352. }
  353. out:
  354. lp->hold_null = lpn;
  355. return error;
  356. }
  357. /* This cannot do a synchronous dlm request (requiring a lock_dlm thread to get
  358. the completion) because gfs may call unhold_lvb() during a callback (from
  359. the context of a lock_dlm thread) which could cause a deadlock since the
  360. other lock_dlm thread could be engaged in recovery. */
  361. static void unhold_null_lock(struct gdlm_lock *lp)
  362. {
  363. struct gdlm_lock *lpn = lp->hold_null;
  364. gdlm_assert(lpn, "%x,%llx", lp->lockname.ln_type,
  365. (unsigned long long)lp->lockname.ln_number);
  366. lpn->lksb.sb_lvbptr = NULL;
  367. lpn->lvb = NULL;
  368. set_bit(LFL_UNLOCK_DELETE, &lpn->flags);
  369. gdlm_do_unlock(lpn);
  370. lp->hold_null = NULL;
  371. }
  372. /* Acquire a NL lock because gfs requires the value block to remain
  373. intact on the resource while the lvb is "held" even if it's holding no locks
  374. on the resource. */
  375. int gdlm_hold_lvb(void *lock, char **lvbp)
  376. {
  377. struct gdlm_lock *lp = lock;
  378. int error;
  379. error = gdlm_add_lvb(lp);
  380. if (error)
  381. return error;
  382. *lvbp = lp->lvb;
  383. error = hold_null_lock(lp);
  384. if (error)
  385. gdlm_del_lvb(lp);
  386. return error;
  387. }
  388. void gdlm_unhold_lvb(void *lock, char *lvb)
  389. {
  390. struct gdlm_lock *lp = lock;
  391. unhold_null_lock(lp);
  392. gdlm_del_lvb(lp);
  393. }
  394. void gdlm_submit_delayed(struct gdlm_ls *ls)
  395. {
  396. struct gdlm_lock *lp, *safe;
  397. spin_lock(&ls->async_lock);
  398. list_for_each_entry_safe(lp, safe, &ls->delayed, delay_list) {
  399. list_del_init(&lp->delay_list);
  400. list_add_tail(&lp->delay_list, &ls->submit);
  401. }
  402. spin_unlock(&ls->async_lock);
  403. wake_up(&ls->thread_wait);
  404. }
  405. int gdlm_release_all_locks(struct gdlm_ls *ls)
  406. {
  407. struct gdlm_lock *lp, *safe;
  408. int count = 0;
  409. spin_lock(&ls->async_lock);
  410. list_for_each_entry_safe(lp, safe, &ls->all_locks, all_list) {
  411. list_del_init(&lp->all_list);
  412. if (lp->lvb && lp->lvb != junk_lvb)
  413. kfree(lp->lvb);
  414. kfree(lp);
  415. count++;
  416. }
  417. spin_unlock(&ls->async_lock);
  418. return count;
  419. }