locking.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2006 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/module.h>
  10. #include <linux/init.h>
  11. #include <linux/string.h>
  12. #include <linux/slab.h>
  13. #include <linux/wait.h>
  14. #include <linux/sched.h>
  15. #include <linux/kmod.h>
  16. #include <linux/fs.h>
  17. #include <linux/delay.h>
  18. #include <linux/lm_interface.h>
  19. struct lmh_wrapper {
  20. struct list_head lw_list;
  21. const struct lm_lockops *lw_ops;
  22. };
  23. struct nolock_lockspace {
  24. unsigned int nl_lvb_size;
  25. };
  26. /**
  27. * nolock_get_lock - get a lm_lock_t given a descripton of the lock
  28. * @lockspace: the lockspace the lock lives in
  29. * @name: the name of the lock
  30. * @lockp: return the lm_lock_t here
  31. *
  32. * Returns: 0 on success, -EXXX on failure
  33. */
  34. static int nolock_get_lock(void *lockspace, struct lm_lockname *name,
  35. void **lockp)
  36. {
  37. *lockp = lockspace;
  38. return 0;
  39. }
  40. /**
  41. * nolock_put_lock - get rid of a lock structure
  42. * @lock: the lock to throw away
  43. *
  44. */
  45. static void nolock_put_lock(void *lock)
  46. {
  47. }
  48. /**
  49. * nolock_hold_lvb - hold on to a lock value block
  50. * @lock: the lock the LVB is associated with
  51. * @lvbp: return the lm_lvb_t here
  52. *
  53. * Returns: 0 on success, -EXXX on failure
  54. */
  55. static int nolock_hold_lvb(void *lock, char **lvbp)
  56. {
  57. struct nolock_lockspace *nl = lock;
  58. int error = 0;
  59. *lvbp = kzalloc(nl->nl_lvb_size, GFP_KERNEL);
  60. if (!*lvbp)
  61. error = -ENOMEM;
  62. return error;
  63. }
  64. /**
  65. * nolock_unhold_lvb - release a LVB
  66. * @lock: the lock the LVB is associated with
  67. * @lvb: the lock value block
  68. *
  69. */
  70. static void nolock_unhold_lvb(void *lock, char *lvb)
  71. {
  72. kfree(lvb);
  73. }
  74. static int nolock_mount(char *table_name, char *host_data,
  75. lm_callback_t cb, void *cb_data,
  76. unsigned int min_lvb_size, int flags,
  77. struct lm_lockstruct *lockstruct,
  78. struct kobject *fskobj);
  79. static void nolock_unmount(void *lockspace);
  80. /* List of registered low-level locking protocols. A file system selects one
  81. of them by name at mount time, e.g. lock_nolock, lock_dlm. */
  82. static const struct lm_lockops nolock_ops = {
  83. .lm_proto_name = "lock_nolock",
  84. .lm_mount = nolock_mount,
  85. .lm_unmount = nolock_unmount,
  86. .lm_get_lock = nolock_get_lock,
  87. .lm_put_lock = nolock_put_lock,
  88. .lm_hold_lvb = nolock_hold_lvb,
  89. .lm_unhold_lvb = nolock_unhold_lvb,
  90. };
  91. static struct lmh_wrapper nolock_proto = {
  92. .lw_list = LIST_HEAD_INIT(nolock_proto.lw_list),
  93. .lw_ops = &nolock_ops,
  94. };
  95. static LIST_HEAD(lmh_list);
  96. static DEFINE_MUTEX(lmh_lock);
  97. static int nolock_mount(char *table_name, char *host_data,
  98. lm_callback_t cb, void *cb_data,
  99. unsigned int min_lvb_size, int flags,
  100. struct lm_lockstruct *lockstruct,
  101. struct kobject *fskobj)
  102. {
  103. char *c;
  104. unsigned int jid;
  105. struct nolock_lockspace *nl;
  106. c = strstr(host_data, "jid=");
  107. if (!c)
  108. jid = 0;
  109. else {
  110. c += 4;
  111. sscanf(c, "%u", &jid);
  112. }
  113. nl = kzalloc(sizeof(struct nolock_lockspace), GFP_KERNEL);
  114. if (!nl)
  115. return -ENOMEM;
  116. nl->nl_lvb_size = min_lvb_size;
  117. lockstruct->ls_jid = jid;
  118. lockstruct->ls_first = 1;
  119. lockstruct->ls_lvb_size = min_lvb_size;
  120. lockstruct->ls_lockspace = nl;
  121. lockstruct->ls_ops = &nolock_ops;
  122. lockstruct->ls_flags = LM_LSFLAG_LOCAL;
  123. return 0;
  124. }
  125. static void nolock_unmount(void *lockspace)
  126. {
  127. struct nolock_lockspace *nl = lockspace;
  128. kfree(nl);
  129. }
  130. /**
  131. * gfs2_register_lockproto - Register a low-level locking protocol
  132. * @proto: the protocol definition
  133. *
  134. * Returns: 0 on success, -EXXX on failure
  135. */
  136. int gfs2_register_lockproto(const struct lm_lockops *proto)
  137. {
  138. struct lmh_wrapper *lw;
  139. mutex_lock(&lmh_lock);
  140. list_for_each_entry(lw, &lmh_list, lw_list) {
  141. if (!strcmp(lw->lw_ops->lm_proto_name, proto->lm_proto_name)) {
  142. mutex_unlock(&lmh_lock);
  143. printk(KERN_INFO "GFS2: protocol %s already exists\n",
  144. proto->lm_proto_name);
  145. return -EEXIST;
  146. }
  147. }
  148. lw = kzalloc(sizeof(struct lmh_wrapper), GFP_KERNEL);
  149. if (!lw) {
  150. mutex_unlock(&lmh_lock);
  151. return -ENOMEM;
  152. }
  153. lw->lw_ops = proto;
  154. list_add(&lw->lw_list, &lmh_list);
  155. mutex_unlock(&lmh_lock);
  156. return 0;
  157. }
  158. /**
  159. * gfs2_unregister_lockproto - Unregister a low-level locking protocol
  160. * @proto: the protocol definition
  161. *
  162. */
  163. void gfs2_unregister_lockproto(const struct lm_lockops *proto)
  164. {
  165. struct lmh_wrapper *lw;
  166. mutex_lock(&lmh_lock);
  167. list_for_each_entry(lw, &lmh_list, lw_list) {
  168. if (!strcmp(lw->lw_ops->lm_proto_name, proto->lm_proto_name)) {
  169. list_del(&lw->lw_list);
  170. mutex_unlock(&lmh_lock);
  171. kfree(lw);
  172. return;
  173. }
  174. }
  175. mutex_unlock(&lmh_lock);
  176. printk(KERN_WARNING "GFS2: can't unregister lock protocol %s\n",
  177. proto->lm_proto_name);
  178. }
  179. /**
  180. * gfs2_mount_lockproto - Mount a lock protocol
  181. * @proto_name - the name of the protocol
  182. * @table_name - the name of the lock space
  183. * @host_data - data specific to this host
  184. * @cb - the callback to the code using the lock module
  185. * @sdp - The GFS2 superblock
  186. * @min_lvb_size - the mininum LVB size that the caller can deal with
  187. * @flags - LM_MFLAG_*
  188. * @lockstruct - a structure returned describing the mount
  189. *
  190. * Returns: 0 on success, -EXXX on failure
  191. */
  192. int gfs2_mount_lockproto(char *proto_name, char *table_name, char *host_data,
  193. lm_callback_t cb, void *cb_data,
  194. unsigned int min_lvb_size, int flags,
  195. struct lm_lockstruct *lockstruct,
  196. struct kobject *fskobj)
  197. {
  198. struct lmh_wrapper *lw = NULL;
  199. int try = 0;
  200. int error, found;
  201. retry:
  202. mutex_lock(&lmh_lock);
  203. if (list_empty(&nolock_proto.lw_list))
  204. list_add(&nolock_proto.lw_list, &lmh_list);
  205. found = 0;
  206. list_for_each_entry(lw, &lmh_list, lw_list) {
  207. if (!strcmp(lw->lw_ops->lm_proto_name, proto_name)) {
  208. found = 1;
  209. break;
  210. }
  211. }
  212. if (!found) {
  213. if (!try && capable(CAP_SYS_MODULE)) {
  214. try = 1;
  215. mutex_unlock(&lmh_lock);
  216. request_module(proto_name);
  217. goto retry;
  218. }
  219. printk(KERN_INFO "GFS2: can't find protocol %s\n", proto_name);
  220. error = -ENOENT;
  221. goto out;
  222. }
  223. if (lw->lw_ops->lm_owner &&
  224. !try_module_get(lw->lw_ops->lm_owner)) {
  225. try = 0;
  226. mutex_unlock(&lmh_lock);
  227. msleep(1000);
  228. goto retry;
  229. }
  230. error = lw->lw_ops->lm_mount(table_name, host_data, cb, cb_data,
  231. min_lvb_size, flags, lockstruct, fskobj);
  232. if (error)
  233. module_put(lw->lw_ops->lm_owner);
  234. out:
  235. mutex_unlock(&lmh_lock);
  236. return error;
  237. }
  238. void gfs2_unmount_lockproto(struct lm_lockstruct *lockstruct)
  239. {
  240. mutex_lock(&lmh_lock);
  241. if (lockstruct->ls_ops->lm_unmount)
  242. lockstruct->ls_ops->lm_unmount(lockstruct->ls_lockspace);
  243. if (lockstruct->ls_ops->lm_owner)
  244. module_put(lockstruct->ls_ops->lm_owner);
  245. mutex_unlock(&lmh_lock);
  246. }
  247. /**
  248. * gfs2_withdraw_lockproto - abnormally unmount a lock module
  249. * @lockstruct: the lockstruct passed into mount
  250. *
  251. */
  252. void gfs2_withdraw_lockproto(struct lm_lockstruct *lockstruct)
  253. {
  254. mutex_lock(&lmh_lock);
  255. lockstruct->ls_ops->lm_withdraw(lockstruct->ls_lockspace);
  256. if (lockstruct->ls_ops->lm_owner)
  257. module_put(lockstruct->ls_ops->lm_owner);
  258. mutex_unlock(&lmh_lock);
  259. }
  260. EXPORT_SYMBOL_GPL(gfs2_register_lockproto);
  261. EXPORT_SYMBOL_GPL(gfs2_unregister_lockproto);