lock_dlm.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2009 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 <linux/fs.h>
  10. #include <linux/dlm.h>
  11. #include <linux/slab.h>
  12. #include <linux/types.h>
  13. #include <linux/gfs2_ondisk.h>
  14. #include "incore.h"
  15. #include "glock.h"
  16. #include "util.h"
  17. static void gdlm_ast(void *arg)
  18. {
  19. struct gfs2_glock *gl = arg;
  20. unsigned ret = gl->gl_state;
  21. struct gfs2_sbd *sdp = gl->gl_sbd;
  22. BUG_ON(gl->gl_lksb.sb_flags & DLM_SBF_DEMOTED);
  23. if (gl->gl_lksb.sb_flags & DLM_SBF_VALNOTVALID)
  24. memset(gl->gl_lvb, 0, GDLM_LVB_SIZE);
  25. switch (gl->gl_lksb.sb_status) {
  26. case -DLM_EUNLOCK: /* Unlocked, so glock can be freed */
  27. if (gl->gl_ops->go_flags & GLOF_ASPACE)
  28. kmem_cache_free(gfs2_glock_aspace_cachep, gl);
  29. else
  30. kmem_cache_free(gfs2_glock_cachep, gl);
  31. if (atomic_dec_and_test(&sdp->sd_glock_disposal))
  32. wake_up(&sdp->sd_glock_wait);
  33. return;
  34. case -DLM_ECANCEL: /* Cancel while getting lock */
  35. ret |= LM_OUT_CANCELED;
  36. goto out;
  37. case -EAGAIN: /* Try lock fails */
  38. goto out;
  39. case -EINVAL: /* Invalid */
  40. case -ENOMEM: /* Out of memory */
  41. ret |= LM_OUT_ERROR;
  42. goto out;
  43. case 0: /* Success */
  44. break;
  45. default: /* Something unexpected */
  46. BUG();
  47. }
  48. ret = gl->gl_req;
  49. if (gl->gl_lksb.sb_flags & DLM_SBF_ALTMODE) {
  50. if (gl->gl_req == LM_ST_SHARED)
  51. ret = LM_ST_DEFERRED;
  52. else if (gl->gl_req == LM_ST_DEFERRED)
  53. ret = LM_ST_SHARED;
  54. else
  55. BUG();
  56. }
  57. set_bit(GLF_INITIAL, &gl->gl_flags);
  58. gfs2_glock_complete(gl, ret);
  59. return;
  60. out:
  61. if (!test_bit(GLF_INITIAL, &gl->gl_flags))
  62. gl->gl_lksb.sb_lkid = 0;
  63. gfs2_glock_complete(gl, ret);
  64. }
  65. static void gdlm_bast(void *arg, int mode)
  66. {
  67. struct gfs2_glock *gl = arg;
  68. switch (mode) {
  69. case DLM_LOCK_EX:
  70. gfs2_glock_cb(gl, LM_ST_UNLOCKED);
  71. break;
  72. case DLM_LOCK_CW:
  73. gfs2_glock_cb(gl, LM_ST_DEFERRED);
  74. break;
  75. case DLM_LOCK_PR:
  76. gfs2_glock_cb(gl, LM_ST_SHARED);
  77. break;
  78. default:
  79. printk(KERN_ERR "unknown bast mode %d", mode);
  80. BUG();
  81. }
  82. }
  83. /* convert gfs lock-state to dlm lock-mode */
  84. static int make_mode(const unsigned int lmstate)
  85. {
  86. switch (lmstate) {
  87. case LM_ST_UNLOCKED:
  88. return DLM_LOCK_NL;
  89. case LM_ST_EXCLUSIVE:
  90. return DLM_LOCK_EX;
  91. case LM_ST_DEFERRED:
  92. return DLM_LOCK_CW;
  93. case LM_ST_SHARED:
  94. return DLM_LOCK_PR;
  95. }
  96. printk(KERN_ERR "unknown LM state %d", lmstate);
  97. BUG();
  98. return -1;
  99. }
  100. static u32 make_flags(const u32 lkid, const unsigned int gfs_flags,
  101. const int req)
  102. {
  103. u32 lkf = 0;
  104. if (gfs_flags & LM_FLAG_TRY)
  105. lkf |= DLM_LKF_NOQUEUE;
  106. if (gfs_flags & LM_FLAG_TRY_1CB) {
  107. lkf |= DLM_LKF_NOQUEUE;
  108. lkf |= DLM_LKF_NOQUEUEBAST;
  109. }
  110. if (gfs_flags & LM_FLAG_PRIORITY) {
  111. lkf |= DLM_LKF_NOORDER;
  112. lkf |= DLM_LKF_HEADQUE;
  113. }
  114. if (gfs_flags & LM_FLAG_ANY) {
  115. if (req == DLM_LOCK_PR)
  116. lkf |= DLM_LKF_ALTCW;
  117. else if (req == DLM_LOCK_CW)
  118. lkf |= DLM_LKF_ALTPR;
  119. else
  120. BUG();
  121. }
  122. if (lkid != 0)
  123. lkf |= DLM_LKF_CONVERT;
  124. lkf |= DLM_LKF_VALBLK;
  125. return lkf;
  126. }
  127. static unsigned int gdlm_lock(struct gfs2_glock *gl,
  128. unsigned int req_state, unsigned int flags)
  129. {
  130. struct lm_lockstruct *ls = &gl->gl_sbd->sd_lockstruct;
  131. int error;
  132. int req;
  133. u32 lkf;
  134. gl->gl_req = req_state;
  135. req = make_mode(req_state);
  136. lkf = make_flags(gl->gl_lksb.sb_lkid, flags, req);
  137. /*
  138. * Submit the actual lock request.
  139. */
  140. error = dlm_lock(ls->ls_dlm, req, &gl->gl_lksb, lkf, gl->gl_strname,
  141. GDLM_STRNAME_BYTES - 1, 0, gdlm_ast, gl, gdlm_bast);
  142. if (error == -EAGAIN)
  143. return 0;
  144. if (error)
  145. return LM_OUT_ERROR;
  146. return LM_OUT_ASYNC;
  147. }
  148. static void gdlm_put_lock(struct kmem_cache *cachep, struct gfs2_glock *gl)
  149. {
  150. struct gfs2_sbd *sdp = gl->gl_sbd;
  151. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  152. int error;
  153. if (gl->gl_lksb.sb_lkid == 0) {
  154. kmem_cache_free(cachep, gl);
  155. if (atomic_dec_and_test(&sdp->sd_glock_disposal))
  156. wake_up(&sdp->sd_glock_wait);
  157. return;
  158. }
  159. error = dlm_unlock(ls->ls_dlm, gl->gl_lksb.sb_lkid, DLM_LKF_VALBLK,
  160. NULL, gl);
  161. if (error) {
  162. printk(KERN_ERR "gdlm_unlock %x,%llx err=%d\n",
  163. gl->gl_name.ln_type,
  164. (unsigned long long)gl->gl_name.ln_number, error);
  165. return;
  166. }
  167. }
  168. static void gdlm_cancel(struct gfs2_glock *gl)
  169. {
  170. struct lm_lockstruct *ls = &gl->gl_sbd->sd_lockstruct;
  171. dlm_unlock(ls->ls_dlm, gl->gl_lksb.sb_lkid, DLM_LKF_CANCEL, NULL, gl);
  172. }
  173. static int gdlm_mount(struct gfs2_sbd *sdp, const char *fsname)
  174. {
  175. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  176. int error;
  177. if (fsname == NULL) {
  178. fs_info(sdp, "no fsname found\n");
  179. return -EINVAL;
  180. }
  181. error = dlm_new_lockspace(fsname, strlen(fsname), &ls->ls_dlm,
  182. DLM_LSFL_FS | DLM_LSFL_NEWEXCL |
  183. (ls->ls_nodir ? DLM_LSFL_NODIR : 0),
  184. GDLM_LVB_SIZE);
  185. if (error)
  186. printk(KERN_ERR "dlm_new_lockspace error %d", error);
  187. return error;
  188. }
  189. static void gdlm_unmount(struct gfs2_sbd *sdp)
  190. {
  191. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  192. if (ls->ls_dlm) {
  193. dlm_release_lockspace(ls->ls_dlm, 2);
  194. ls->ls_dlm = NULL;
  195. }
  196. }
  197. static const match_table_t dlm_tokens = {
  198. { Opt_jid, "jid=%d"},
  199. { Opt_id, "id=%d"},
  200. { Opt_first, "first=%d"},
  201. { Opt_nodir, "nodir=%d"},
  202. { Opt_err, NULL },
  203. };
  204. const struct lm_lockops gfs2_dlm_ops = {
  205. .lm_proto_name = "lock_dlm",
  206. .lm_mount = gdlm_mount,
  207. .lm_unmount = gdlm_unmount,
  208. .lm_put_lock = gdlm_put_lock,
  209. .lm_lock = gdlm_lock,
  210. .lm_cancel = gdlm_cancel,
  211. .lm_tokens = &dlm_tokens,
  212. };