lock.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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(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. lp->ls = ls;
  141. lp->cur = DLM_LOCK_IV;
  142. lp->lvb = NULL;
  143. lp->hold_null = NULL;
  144. init_completion(&lp->ast_wait);
  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. struct gdlm_strname str;
  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. make_strname(&lp->lockname, &str);
  206. set_bit(LFL_ACTIVE, &lp->flags);
  207. log_debug("lk %x,%llx id %x %d,%d %x", lp->lockname.ln_type,
  208. (unsigned long long)lp->lockname.ln_number, lp->lksb.sb_lkid,
  209. lp->cur, lp->req, lp->lkf);
  210. error = dlm_lock(ls->dlm_lockspace, lp->req, &lp->lksb, lp->lkf,
  211. str.name, str.namelen, 0, gdlm_ast, lp,
  212. bast ? gdlm_bast : NULL);
  213. if ((error == -EAGAIN) && (lp->lkf & DLM_LKF_NOQUEUE)) {
  214. lp->lksb.sb_status = -EAGAIN;
  215. queue_complete(lp);
  216. error = 0;
  217. }
  218. if (error) {
  219. log_debug("%s: gdlm_lock %x,%llx err=%d cur=%d req=%d lkf=%x "
  220. "flags=%lx", ls->fsname, lp->lockname.ln_type,
  221. (unsigned long long)lp->lockname.ln_number, error,
  222. lp->cur, lp->req, lp->lkf, lp->flags);
  223. return LM_OUT_ERROR;
  224. }
  225. return LM_OUT_ASYNC;
  226. }
  227. static unsigned int gdlm_do_unlock(struct gdlm_lock *lp)
  228. {
  229. struct gdlm_ls *ls = lp->ls;
  230. unsigned int lkf = 0;
  231. int error;
  232. set_bit(LFL_DLM_UNLOCK, &lp->flags);
  233. set_bit(LFL_ACTIVE, &lp->flags);
  234. if (lp->lvb)
  235. lkf = DLM_LKF_VALBLK;
  236. log_debug("un %x,%llx %x %d %x", lp->lockname.ln_type,
  237. (unsigned long long)lp->lockname.ln_number,
  238. lp->lksb.sb_lkid, lp->cur, lkf);
  239. error = dlm_unlock(ls->dlm_lockspace, lp->lksb.sb_lkid, lkf, NULL, lp);
  240. if (error) {
  241. log_debug("%s: gdlm_unlock %x,%llx err=%d cur=%d req=%d lkf=%x "
  242. "flags=%lx", ls->fsname, lp->lockname.ln_type,
  243. (unsigned long long)lp->lockname.ln_number, error,
  244. lp->cur, lp->req, lp->lkf, lp->flags);
  245. return LM_OUT_ERROR;
  246. }
  247. return LM_OUT_ASYNC;
  248. }
  249. unsigned int gdlm_lock(void *lock, unsigned int cur_state,
  250. unsigned int req_state, unsigned int flags)
  251. {
  252. struct gdlm_lock *lp = lock;
  253. clear_bit(LFL_DLM_CANCEL, &lp->flags);
  254. if (flags & LM_FLAG_NOEXP)
  255. set_bit(LFL_NOBLOCK, &lp->flags);
  256. check_cur_state(lp, cur_state);
  257. lp->req = make_mode(req_state);
  258. lp->lkf = make_flags(lp, flags, lp->cur, lp->req);
  259. return gdlm_do_lock(lp);
  260. }
  261. unsigned int gdlm_unlock(void *lock, unsigned int cur_state)
  262. {
  263. struct gdlm_lock *lp = lock;
  264. clear_bit(LFL_DLM_CANCEL, &lp->flags);
  265. if (lp->cur == DLM_LOCK_IV)
  266. return 0;
  267. return gdlm_do_unlock(lp);
  268. }
  269. void gdlm_cancel(void *lock)
  270. {
  271. struct gdlm_lock *lp = lock;
  272. struct gdlm_ls *ls = lp->ls;
  273. int error, delay_list = 0;
  274. if (test_bit(LFL_DLM_CANCEL, &lp->flags))
  275. return;
  276. log_info("gdlm_cancel %x,%llx flags %lx", lp->lockname.ln_type,
  277. (unsigned long long)lp->lockname.ln_number, lp->flags);
  278. spin_lock(&ls->async_lock);
  279. if (!list_empty(&lp->delay_list)) {
  280. list_del_init(&lp->delay_list);
  281. delay_list = 1;
  282. }
  283. spin_unlock(&ls->async_lock);
  284. if (delay_list) {
  285. set_bit(LFL_CANCEL, &lp->flags);
  286. set_bit(LFL_ACTIVE, &lp->flags);
  287. queue_complete(lp);
  288. return;
  289. }
  290. if (!test_bit(LFL_ACTIVE, &lp->flags) ||
  291. test_bit(LFL_DLM_UNLOCK, &lp->flags)) {
  292. log_info("gdlm_cancel skip %x,%llx flags %lx",
  293. lp->lockname.ln_type,
  294. (unsigned long long)lp->lockname.ln_number, lp->flags);
  295. return;
  296. }
  297. /* the lock is blocked in the dlm */
  298. set_bit(LFL_DLM_CANCEL, &lp->flags);
  299. set_bit(LFL_ACTIVE, &lp->flags);
  300. error = dlm_unlock(ls->dlm_lockspace, lp->lksb.sb_lkid, DLM_LKF_CANCEL,
  301. NULL, lp);
  302. log_info("gdlm_cancel rv %d %x,%llx flags %lx", error,
  303. lp->lockname.ln_type,
  304. (unsigned long long)lp->lockname.ln_number, lp->flags);
  305. if (error == -EBUSY)
  306. clear_bit(LFL_DLM_CANCEL, &lp->flags);
  307. }
  308. static int gdlm_add_lvb(struct gdlm_lock *lp)
  309. {
  310. char *lvb;
  311. lvb = kzalloc(GDLM_LVB_SIZE, GFP_KERNEL);
  312. if (!lvb)
  313. return -ENOMEM;
  314. lp->lksb.sb_lvbptr = lvb;
  315. lp->lvb = lvb;
  316. return 0;
  317. }
  318. static void gdlm_del_lvb(struct gdlm_lock *lp)
  319. {
  320. kfree(lp->lvb);
  321. lp->lvb = NULL;
  322. lp->lksb.sb_lvbptr = NULL;
  323. }
  324. /* This can do a synchronous dlm request (requiring a lock_dlm thread to get
  325. the completion) because gfs won't call hold_lvb() during a callback (from
  326. the context of a lock_dlm thread). */
  327. static int hold_null_lock(struct gdlm_lock *lp)
  328. {
  329. struct gdlm_lock *lpn = NULL;
  330. int error;
  331. if (lp->hold_null) {
  332. printk(KERN_INFO "lock_dlm: lvb already held\n");
  333. return 0;
  334. }
  335. error = gdlm_create_lp(lp->ls, &lp->lockname, &lpn);
  336. if (error)
  337. goto out;
  338. lpn->lksb.sb_lvbptr = junk_lvb;
  339. lpn->lvb = junk_lvb;
  340. lpn->req = DLM_LOCK_NL;
  341. lpn->lkf = DLM_LKF_VALBLK | DLM_LKF_EXPEDITE;
  342. set_bit(LFL_NOBAST, &lpn->flags);
  343. set_bit(LFL_INLOCK, &lpn->flags);
  344. init_completion(&lpn->ast_wait);
  345. gdlm_do_lock(lpn);
  346. wait_for_completion(&lpn->ast_wait);
  347. error = lpn->lksb.sb_status;
  348. if (error) {
  349. printk(KERN_INFO "lock_dlm: hold_null_lock dlm error %d\n",
  350. error);
  351. gdlm_delete_lp(lpn);
  352. lpn = NULL;
  353. }
  354. out:
  355. lp->hold_null = lpn;
  356. return error;
  357. }
  358. /* This cannot do a synchronous dlm request (requiring a lock_dlm thread to get
  359. the completion) because gfs may call unhold_lvb() during a callback (from
  360. the context of a lock_dlm thread) which could cause a deadlock since the
  361. other lock_dlm thread could be engaged in recovery. */
  362. static void unhold_null_lock(struct gdlm_lock *lp)
  363. {
  364. struct gdlm_lock *lpn = lp->hold_null;
  365. gdlm_assert(lpn, "%x,%llx", lp->lockname.ln_type,
  366. (unsigned long long)lp->lockname.ln_number);
  367. lpn->lksb.sb_lvbptr = NULL;
  368. lpn->lvb = NULL;
  369. set_bit(LFL_UNLOCK_DELETE, &lpn->flags);
  370. gdlm_do_unlock(lpn);
  371. lp->hold_null = NULL;
  372. }
  373. /* Acquire a NL lock because gfs requires the value block to remain
  374. intact on the resource while the lvb is "held" even if it's holding no locks
  375. on the resource. */
  376. int gdlm_hold_lvb(void *lock, char **lvbp)
  377. {
  378. struct gdlm_lock *lp = lock;
  379. int error;
  380. error = gdlm_add_lvb(lp);
  381. if (error)
  382. return error;
  383. *lvbp = lp->lvb;
  384. error = hold_null_lock(lp);
  385. if (error)
  386. gdlm_del_lvb(lp);
  387. return error;
  388. }
  389. void gdlm_unhold_lvb(void *lock, char *lvb)
  390. {
  391. struct gdlm_lock *lp = lock;
  392. unhold_null_lock(lp);
  393. gdlm_del_lvb(lp);
  394. }
  395. void gdlm_submit_delayed(struct gdlm_ls *ls)
  396. {
  397. struct gdlm_lock *lp, *safe;
  398. spin_lock(&ls->async_lock);
  399. list_for_each_entry_safe(lp, safe, &ls->delayed, delay_list) {
  400. list_del_init(&lp->delay_list);
  401. list_add_tail(&lp->delay_list, &ls->submit);
  402. }
  403. spin_unlock(&ls->async_lock);
  404. wake_up(&ls->thread_wait);
  405. }
  406. int gdlm_release_all_locks(struct gdlm_ls *ls)
  407. {
  408. struct gdlm_lock *lp, *safe;
  409. int count = 0;
  410. spin_lock(&ls->async_lock);
  411. list_for_each_entry_safe(lp, safe, &ls->all_locks, all_list) {
  412. list_del_init(&lp->all_list);
  413. if (lp->lvb && lp->lvb != junk_lvb)
  414. kfree(lp->lvb);
  415. kfree(lp);
  416. count++;
  417. }
  418. spin_unlock(&ls->async_lock);
  419. return count;
  420. }