locking.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. static int nolock_mount(char *table_name, char *host_data,
  24. lm_callback_t cb, void *cb_data,
  25. unsigned int min_lvb_size, int flags,
  26. struct lm_lockstruct *lockstruct,
  27. struct kobject *fskobj);
  28. /* List of registered low-level locking protocols. A file system selects one
  29. of them by name at mount time, e.g. lock_nolock, lock_dlm. */
  30. static const struct lm_lockops nolock_ops = {
  31. .lm_proto_name = "lock_nolock",
  32. .lm_mount = nolock_mount,
  33. };
  34. static struct lmh_wrapper nolock_proto = {
  35. .lw_list = LIST_HEAD_INIT(nolock_proto.lw_list),
  36. .lw_ops = &nolock_ops,
  37. };
  38. static LIST_HEAD(lmh_list);
  39. static DEFINE_MUTEX(lmh_lock);
  40. static int nolock_mount(char *table_name, char *host_data,
  41. lm_callback_t cb, void *cb_data,
  42. unsigned int min_lvb_size, int flags,
  43. struct lm_lockstruct *lockstruct,
  44. struct kobject *fskobj)
  45. {
  46. char *c;
  47. unsigned int jid;
  48. c = strstr(host_data, "jid=");
  49. if (!c)
  50. jid = 0;
  51. else {
  52. c += 4;
  53. sscanf(c, "%u", &jid);
  54. }
  55. lockstruct->ls_jid = jid;
  56. lockstruct->ls_first = 1;
  57. lockstruct->ls_lvb_size = min_lvb_size;
  58. lockstruct->ls_ops = &nolock_ops;
  59. lockstruct->ls_flags = LM_LSFLAG_LOCAL;
  60. return 0;
  61. }
  62. /**
  63. * gfs2_register_lockproto - Register a low-level locking protocol
  64. * @proto: the protocol definition
  65. *
  66. * Returns: 0 on success, -EXXX on failure
  67. */
  68. int gfs2_register_lockproto(const struct lm_lockops *proto)
  69. {
  70. struct lmh_wrapper *lw;
  71. mutex_lock(&lmh_lock);
  72. list_for_each_entry(lw, &lmh_list, lw_list) {
  73. if (!strcmp(lw->lw_ops->lm_proto_name, proto->lm_proto_name)) {
  74. mutex_unlock(&lmh_lock);
  75. printk(KERN_INFO "GFS2: protocol %s already exists\n",
  76. proto->lm_proto_name);
  77. return -EEXIST;
  78. }
  79. }
  80. lw = kzalloc(sizeof(struct lmh_wrapper), GFP_KERNEL);
  81. if (!lw) {
  82. mutex_unlock(&lmh_lock);
  83. return -ENOMEM;
  84. }
  85. lw->lw_ops = proto;
  86. list_add(&lw->lw_list, &lmh_list);
  87. mutex_unlock(&lmh_lock);
  88. return 0;
  89. }
  90. /**
  91. * gfs2_unregister_lockproto - Unregister a low-level locking protocol
  92. * @proto: the protocol definition
  93. *
  94. */
  95. void gfs2_unregister_lockproto(const struct lm_lockops *proto)
  96. {
  97. struct lmh_wrapper *lw;
  98. mutex_lock(&lmh_lock);
  99. list_for_each_entry(lw, &lmh_list, lw_list) {
  100. if (!strcmp(lw->lw_ops->lm_proto_name, proto->lm_proto_name)) {
  101. list_del(&lw->lw_list);
  102. mutex_unlock(&lmh_lock);
  103. kfree(lw);
  104. return;
  105. }
  106. }
  107. mutex_unlock(&lmh_lock);
  108. printk(KERN_WARNING "GFS2: can't unregister lock protocol %s\n",
  109. proto->lm_proto_name);
  110. }
  111. /**
  112. * gfs2_mount_lockproto - Mount a lock protocol
  113. * @proto_name - the name of the protocol
  114. * @table_name - the name of the lock space
  115. * @host_data - data specific to this host
  116. * @cb - the callback to the code using the lock module
  117. * @sdp - The GFS2 superblock
  118. * @min_lvb_size - the mininum LVB size that the caller can deal with
  119. * @flags - LM_MFLAG_*
  120. * @lockstruct - a structure returned describing the mount
  121. *
  122. * Returns: 0 on success, -EXXX on failure
  123. */
  124. int gfs2_mount_lockproto(char *proto_name, char *table_name, char *host_data,
  125. lm_callback_t cb, void *cb_data,
  126. unsigned int min_lvb_size, int flags,
  127. struct lm_lockstruct *lockstruct,
  128. struct kobject *fskobj)
  129. {
  130. struct lmh_wrapper *lw = NULL;
  131. int try = 0;
  132. int error, found;
  133. retry:
  134. mutex_lock(&lmh_lock);
  135. if (list_empty(&nolock_proto.lw_list))
  136. list_add(&nolock_proto.lw_list, &lmh_list);
  137. found = 0;
  138. list_for_each_entry(lw, &lmh_list, lw_list) {
  139. if (!strcmp(lw->lw_ops->lm_proto_name, proto_name)) {
  140. found = 1;
  141. break;
  142. }
  143. }
  144. if (!found) {
  145. if (!try && capable(CAP_SYS_MODULE)) {
  146. try = 1;
  147. mutex_unlock(&lmh_lock);
  148. request_module(proto_name);
  149. goto retry;
  150. }
  151. printk(KERN_INFO "GFS2: can't find protocol %s\n", proto_name);
  152. error = -ENOENT;
  153. goto out;
  154. }
  155. if (lw->lw_ops->lm_owner &&
  156. !try_module_get(lw->lw_ops->lm_owner)) {
  157. try = 0;
  158. mutex_unlock(&lmh_lock);
  159. msleep(1000);
  160. goto retry;
  161. }
  162. error = lw->lw_ops->lm_mount(table_name, host_data, cb, cb_data,
  163. min_lvb_size, flags, lockstruct, fskobj);
  164. if (error)
  165. module_put(lw->lw_ops->lm_owner);
  166. out:
  167. mutex_unlock(&lmh_lock);
  168. return error;
  169. }
  170. void gfs2_unmount_lockproto(struct lm_lockstruct *lockstruct)
  171. {
  172. mutex_lock(&lmh_lock);
  173. if (lockstruct->ls_ops->lm_unmount)
  174. lockstruct->ls_ops->lm_unmount(lockstruct->ls_lockspace);
  175. if (lockstruct->ls_ops->lm_owner)
  176. module_put(lockstruct->ls_ops->lm_owner);
  177. mutex_unlock(&lmh_lock);
  178. }
  179. /**
  180. * gfs2_withdraw_lockproto - abnormally unmount a lock module
  181. * @lockstruct: the lockstruct passed into mount
  182. *
  183. */
  184. void gfs2_withdraw_lockproto(struct lm_lockstruct *lockstruct)
  185. {
  186. mutex_lock(&lmh_lock);
  187. lockstruct->ls_ops->lm_withdraw(lockstruct->ls_lockspace);
  188. if (lockstruct->ls_ops->lm_owner)
  189. module_put(lockstruct->ls_ops->lm_owner);
  190. mutex_unlock(&lmh_lock);
  191. }
  192. EXPORT_SYMBOL_GPL(gfs2_register_lockproto);
  193. EXPORT_SYMBOL_GPL(gfs2_unregister_lockproto);