stackglue.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * stackglue.c
  5. *
  6. * Code which implements an OCFS2 specific interface to underlying
  7. * cluster stacks.
  8. *
  9. * Copyright (C) 2007 Oracle. All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public
  13. * License as published by the Free Software Foundation, version 2.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. */
  20. #include <linux/list.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/kmod.h>
  25. #include <linux/fs.h>
  26. #include <linux/kobject.h>
  27. #include <linux/sysfs.h>
  28. #include "ocfs2_fs.h"
  29. #include "stackglue.h"
  30. #define OCFS2_STACK_PLUGIN_O2CB "o2cb"
  31. #define OCFS2_STACK_PLUGIN_USER "user"
  32. static struct ocfs2_locking_protocol *lproto;
  33. static DEFINE_SPINLOCK(ocfs2_stack_lock);
  34. static LIST_HEAD(ocfs2_stack_list);
  35. static char cluster_stack_name[OCFS2_STACK_LABEL_LEN + 1];
  36. /*
  37. * The stack currently in use. If not null, active_stack->sp_count > 0,
  38. * the module is pinned, and the locking protocol cannot be changed.
  39. */
  40. static struct ocfs2_stack_plugin *active_stack;
  41. static struct ocfs2_stack_plugin *ocfs2_stack_lookup(const char *name)
  42. {
  43. struct ocfs2_stack_plugin *p;
  44. assert_spin_locked(&ocfs2_stack_lock);
  45. list_for_each_entry(p, &ocfs2_stack_list, sp_list) {
  46. if (!strcmp(p->sp_name, name))
  47. return p;
  48. }
  49. return NULL;
  50. }
  51. static int ocfs2_stack_driver_request(const char *stack_name,
  52. const char *plugin_name)
  53. {
  54. int rc;
  55. struct ocfs2_stack_plugin *p;
  56. spin_lock(&ocfs2_stack_lock);
  57. /*
  58. * If the stack passed by the filesystem isn't the selected one,
  59. * we can't continue.
  60. */
  61. if (strcmp(stack_name, cluster_stack_name)) {
  62. rc = -EBUSY;
  63. goto out;
  64. }
  65. if (active_stack) {
  66. /*
  67. * If the active stack isn't the one we want, it cannot
  68. * be selected right now.
  69. */
  70. if (!strcmp(active_stack->sp_name, plugin_name))
  71. rc = 0;
  72. else
  73. rc = -EBUSY;
  74. goto out;
  75. }
  76. p = ocfs2_stack_lookup(plugin_name);
  77. if (!p || !try_module_get(p->sp_owner)) {
  78. rc = -ENOENT;
  79. goto out;
  80. }
  81. /* Ok, the stack is pinned */
  82. p->sp_count++;
  83. active_stack = p;
  84. rc = 0;
  85. out:
  86. spin_unlock(&ocfs2_stack_lock);
  87. return rc;
  88. }
  89. /*
  90. * This function looks up the appropriate stack and makes it active. If
  91. * there is no stack, it tries to load it. It will fail if the stack still
  92. * cannot be found. It will also fail if a different stack is in use.
  93. */
  94. static int ocfs2_stack_driver_get(const char *stack_name)
  95. {
  96. int rc;
  97. char *plugin_name = OCFS2_STACK_PLUGIN_O2CB;
  98. /*
  99. * Classic stack does not pass in a stack name. This is
  100. * compatible with older tools as well.
  101. */
  102. if (!stack_name || !*stack_name)
  103. stack_name = OCFS2_STACK_PLUGIN_O2CB;
  104. if (strlen(stack_name) != OCFS2_STACK_LABEL_LEN) {
  105. printk(KERN_ERR
  106. "ocfs2 passed an invalid cluster stack label: \"%s\"\n",
  107. stack_name);
  108. return -EINVAL;
  109. }
  110. /* Anything that isn't the classic stack is a user stack */
  111. if (strcmp(stack_name, OCFS2_STACK_PLUGIN_O2CB))
  112. plugin_name = OCFS2_STACK_PLUGIN_USER;
  113. rc = ocfs2_stack_driver_request(stack_name, plugin_name);
  114. if (rc == -ENOENT) {
  115. request_module("ocfs2_stack_%s", plugin_name);
  116. rc = ocfs2_stack_driver_request(stack_name, plugin_name);
  117. }
  118. if (rc == -ENOENT) {
  119. printk(KERN_ERR
  120. "ocfs2: Cluster stack driver \"%s\" cannot be found\n",
  121. plugin_name);
  122. } else if (rc == -EBUSY) {
  123. printk(KERN_ERR
  124. "ocfs2: A different cluster stack is in use\n");
  125. }
  126. return rc;
  127. }
  128. static void ocfs2_stack_driver_put(void)
  129. {
  130. spin_lock(&ocfs2_stack_lock);
  131. BUG_ON(active_stack == NULL);
  132. BUG_ON(active_stack->sp_count == 0);
  133. active_stack->sp_count--;
  134. if (!active_stack->sp_count) {
  135. module_put(active_stack->sp_owner);
  136. active_stack = NULL;
  137. }
  138. spin_unlock(&ocfs2_stack_lock);
  139. }
  140. int ocfs2_stack_glue_register(struct ocfs2_stack_plugin *plugin)
  141. {
  142. int rc;
  143. spin_lock(&ocfs2_stack_lock);
  144. if (!ocfs2_stack_lookup(plugin->sp_name)) {
  145. plugin->sp_count = 0;
  146. plugin->sp_proto = lproto;
  147. list_add(&plugin->sp_list, &ocfs2_stack_list);
  148. printk(KERN_INFO "ocfs2: Registered cluster interface %s\n",
  149. plugin->sp_name);
  150. rc = 0;
  151. } else {
  152. printk(KERN_ERR "ocfs2: Stack \"%s\" already registered\n",
  153. plugin->sp_name);
  154. rc = -EEXIST;
  155. }
  156. spin_unlock(&ocfs2_stack_lock);
  157. return rc;
  158. }
  159. EXPORT_SYMBOL_GPL(ocfs2_stack_glue_register);
  160. void ocfs2_stack_glue_unregister(struct ocfs2_stack_plugin *plugin)
  161. {
  162. struct ocfs2_stack_plugin *p;
  163. spin_lock(&ocfs2_stack_lock);
  164. p = ocfs2_stack_lookup(plugin->sp_name);
  165. if (p) {
  166. BUG_ON(p != plugin);
  167. BUG_ON(plugin == active_stack);
  168. BUG_ON(plugin->sp_count != 0);
  169. list_del_init(&plugin->sp_list);
  170. printk(KERN_INFO "ocfs2: Unregistered cluster interface %s\n",
  171. plugin->sp_name);
  172. } else {
  173. printk(KERN_ERR "Stack \"%s\" is not registered\n",
  174. plugin->sp_name);
  175. }
  176. spin_unlock(&ocfs2_stack_lock);
  177. }
  178. EXPORT_SYMBOL_GPL(ocfs2_stack_glue_unregister);
  179. void ocfs2_stack_glue_set_locking_protocol(struct ocfs2_locking_protocol *proto)
  180. {
  181. struct ocfs2_stack_plugin *p;
  182. BUG_ON(proto == NULL);
  183. spin_lock(&ocfs2_stack_lock);
  184. BUG_ON(active_stack != NULL);
  185. lproto = proto;
  186. list_for_each_entry(p, &ocfs2_stack_list, sp_list) {
  187. p->sp_proto = lproto;
  188. }
  189. spin_unlock(&ocfs2_stack_lock);
  190. }
  191. EXPORT_SYMBOL_GPL(ocfs2_stack_glue_set_locking_protocol);
  192. /*
  193. * The ocfs2_dlm_lock() and ocfs2_dlm_unlock() functions take
  194. * "struct ocfs2_lock_res *astarg" instead of "void *astarg" because the
  195. * underlying stack plugins need to pilfer the lksb off of the lock_res.
  196. * If some other structure needs to be passed as an astarg, the plugins
  197. * will need to be given a different avenue to the lksb.
  198. */
  199. int ocfs2_dlm_lock(struct ocfs2_cluster_connection *conn,
  200. int mode,
  201. union ocfs2_dlm_lksb *lksb,
  202. u32 flags,
  203. void *name,
  204. unsigned int namelen,
  205. struct ocfs2_lock_res *astarg)
  206. {
  207. BUG_ON(lproto == NULL);
  208. return active_stack->sp_ops->dlm_lock(conn, mode, lksb, flags,
  209. name, namelen, astarg);
  210. }
  211. EXPORT_SYMBOL_GPL(ocfs2_dlm_lock);
  212. int ocfs2_dlm_unlock(struct ocfs2_cluster_connection *conn,
  213. union ocfs2_dlm_lksb *lksb,
  214. u32 flags,
  215. struct ocfs2_lock_res *astarg)
  216. {
  217. BUG_ON(lproto == NULL);
  218. return active_stack->sp_ops->dlm_unlock(conn, lksb, flags, astarg);
  219. }
  220. EXPORT_SYMBOL_GPL(ocfs2_dlm_unlock);
  221. int ocfs2_dlm_lock_status(union ocfs2_dlm_lksb *lksb)
  222. {
  223. return active_stack->sp_ops->lock_status(lksb);
  224. }
  225. EXPORT_SYMBOL_GPL(ocfs2_dlm_lock_status);
  226. /*
  227. * Why don't we cast to ocfs2_meta_lvb? The "clean" answer is that we
  228. * don't cast at the glue level. The real answer is that the header
  229. * ordering is nigh impossible.
  230. */
  231. void *ocfs2_dlm_lvb(union ocfs2_dlm_lksb *lksb)
  232. {
  233. return active_stack->sp_ops->lock_lvb(lksb);
  234. }
  235. EXPORT_SYMBOL_GPL(ocfs2_dlm_lvb);
  236. void ocfs2_dlm_dump_lksb(union ocfs2_dlm_lksb *lksb)
  237. {
  238. active_stack->sp_ops->dump_lksb(lksb);
  239. }
  240. EXPORT_SYMBOL_GPL(ocfs2_dlm_dump_lksb);
  241. int ocfs2_cluster_connect(const char *stack_name,
  242. const char *group,
  243. int grouplen,
  244. void (*recovery_handler)(int node_num,
  245. void *recovery_data),
  246. void *recovery_data,
  247. struct ocfs2_cluster_connection **conn)
  248. {
  249. int rc = 0;
  250. struct ocfs2_cluster_connection *new_conn;
  251. BUG_ON(group == NULL);
  252. BUG_ON(conn == NULL);
  253. BUG_ON(recovery_handler == NULL);
  254. if (grouplen > GROUP_NAME_MAX) {
  255. rc = -EINVAL;
  256. goto out;
  257. }
  258. new_conn = kzalloc(sizeof(struct ocfs2_cluster_connection),
  259. GFP_KERNEL);
  260. if (!new_conn) {
  261. rc = -ENOMEM;
  262. goto out;
  263. }
  264. memcpy(new_conn->cc_name, group, grouplen);
  265. new_conn->cc_namelen = grouplen;
  266. new_conn->cc_recovery_handler = recovery_handler;
  267. new_conn->cc_recovery_data = recovery_data;
  268. /* Start the new connection at our maximum compatibility level */
  269. new_conn->cc_version = lproto->lp_max_version;
  270. /* This will pin the stack driver if successful */
  271. rc = ocfs2_stack_driver_get(stack_name);
  272. if (rc)
  273. goto out_free;
  274. rc = active_stack->sp_ops->connect(new_conn);
  275. if (rc) {
  276. ocfs2_stack_driver_put();
  277. goto out_free;
  278. }
  279. *conn = new_conn;
  280. out_free:
  281. if (rc)
  282. kfree(new_conn);
  283. out:
  284. return rc;
  285. }
  286. EXPORT_SYMBOL_GPL(ocfs2_cluster_connect);
  287. /* If hangup_pending is 0, the stack driver will be dropped */
  288. int ocfs2_cluster_disconnect(struct ocfs2_cluster_connection *conn,
  289. int hangup_pending)
  290. {
  291. int ret;
  292. BUG_ON(conn == NULL);
  293. ret = active_stack->sp_ops->disconnect(conn, hangup_pending);
  294. /* XXX Should we free it anyway? */
  295. if (!ret) {
  296. kfree(conn);
  297. if (!hangup_pending)
  298. ocfs2_stack_driver_put();
  299. }
  300. return ret;
  301. }
  302. EXPORT_SYMBOL_GPL(ocfs2_cluster_disconnect);
  303. void ocfs2_cluster_hangup(const char *group, int grouplen)
  304. {
  305. BUG_ON(group == NULL);
  306. BUG_ON(group[grouplen] != '\0');
  307. if (active_stack->sp_ops->hangup)
  308. active_stack->sp_ops->hangup(group, grouplen);
  309. /* cluster_disconnect() was called with hangup_pending==1 */
  310. ocfs2_stack_driver_put();
  311. }
  312. EXPORT_SYMBOL_GPL(ocfs2_cluster_hangup);
  313. int ocfs2_cluster_this_node(unsigned int *node)
  314. {
  315. return active_stack->sp_ops->this_node(node);
  316. }
  317. EXPORT_SYMBOL_GPL(ocfs2_cluster_this_node);
  318. /*
  319. * Sysfs bits
  320. */
  321. static ssize_t ocfs2_max_locking_protocol_show(struct kobject *kobj,
  322. struct kobj_attribute *attr,
  323. char *buf)
  324. {
  325. ssize_t ret = 0;
  326. spin_lock(&ocfs2_stack_lock);
  327. if (lproto)
  328. ret = snprintf(buf, PAGE_SIZE, "%u.%u\n",
  329. lproto->lp_max_version.pv_major,
  330. lproto->lp_max_version.pv_minor);
  331. spin_unlock(&ocfs2_stack_lock);
  332. return ret;
  333. }
  334. static struct kobj_attribute ocfs2_attr_max_locking_protocol =
  335. __ATTR(max_locking_protocol, S_IFREG | S_IRUGO,
  336. ocfs2_max_locking_protocol_show, NULL);
  337. static ssize_t ocfs2_loaded_cluster_plugins_show(struct kobject *kobj,
  338. struct kobj_attribute *attr,
  339. char *buf)
  340. {
  341. ssize_t ret = 0, total = 0, remain = PAGE_SIZE;
  342. struct ocfs2_stack_plugin *p;
  343. spin_lock(&ocfs2_stack_lock);
  344. list_for_each_entry(p, &ocfs2_stack_list, sp_list) {
  345. ret = snprintf(buf, remain, "%s\n",
  346. p->sp_name);
  347. if (ret < 0) {
  348. total = ret;
  349. break;
  350. }
  351. if (ret == remain) {
  352. /* snprintf() didn't fit */
  353. total = -E2BIG;
  354. break;
  355. }
  356. total += ret;
  357. remain -= ret;
  358. }
  359. spin_unlock(&ocfs2_stack_lock);
  360. return total;
  361. }
  362. static struct kobj_attribute ocfs2_attr_loaded_cluster_plugins =
  363. __ATTR(loaded_cluster_plugins, S_IFREG | S_IRUGO,
  364. ocfs2_loaded_cluster_plugins_show, NULL);
  365. static ssize_t ocfs2_active_cluster_plugin_show(struct kobject *kobj,
  366. struct kobj_attribute *attr,
  367. char *buf)
  368. {
  369. ssize_t ret = 0;
  370. spin_lock(&ocfs2_stack_lock);
  371. if (active_stack) {
  372. ret = snprintf(buf, PAGE_SIZE, "%s\n",
  373. active_stack->sp_name);
  374. if (ret == PAGE_SIZE)
  375. ret = -E2BIG;
  376. }
  377. spin_unlock(&ocfs2_stack_lock);
  378. return ret;
  379. }
  380. static struct kobj_attribute ocfs2_attr_active_cluster_plugin =
  381. __ATTR(active_cluster_plugin, S_IFREG | S_IRUGO,
  382. ocfs2_active_cluster_plugin_show, NULL);
  383. static ssize_t ocfs2_cluster_stack_show(struct kobject *kobj,
  384. struct kobj_attribute *attr,
  385. char *buf)
  386. {
  387. ssize_t ret;
  388. spin_lock(&ocfs2_stack_lock);
  389. ret = snprintf(buf, PAGE_SIZE, "%s\n", cluster_stack_name);
  390. spin_unlock(&ocfs2_stack_lock);
  391. return ret;
  392. }
  393. static ssize_t ocfs2_cluster_stack_store(struct kobject *kobj,
  394. struct kobj_attribute *attr,
  395. const char *buf, size_t count)
  396. {
  397. size_t len = count;
  398. ssize_t ret;
  399. if (len == 0)
  400. return len;
  401. if (buf[len - 1] == '\n')
  402. len--;
  403. if ((len != OCFS2_STACK_LABEL_LEN) ||
  404. (strnlen(buf, len) != len))
  405. return -EINVAL;
  406. spin_lock(&ocfs2_stack_lock);
  407. if (active_stack) {
  408. if (!strncmp(buf, cluster_stack_name, len))
  409. ret = count;
  410. else
  411. ret = -EBUSY;
  412. } else {
  413. memcpy(cluster_stack_name, buf, len);
  414. ret = count;
  415. }
  416. spin_unlock(&ocfs2_stack_lock);
  417. return ret;
  418. }
  419. static struct kobj_attribute ocfs2_attr_cluster_stack =
  420. __ATTR(cluster_stack, S_IFREG | S_IRUGO | S_IWUSR,
  421. ocfs2_cluster_stack_show,
  422. ocfs2_cluster_stack_store);
  423. static struct attribute *ocfs2_attrs[] = {
  424. &ocfs2_attr_max_locking_protocol.attr,
  425. &ocfs2_attr_loaded_cluster_plugins.attr,
  426. &ocfs2_attr_active_cluster_plugin.attr,
  427. &ocfs2_attr_cluster_stack.attr,
  428. NULL,
  429. };
  430. static struct attribute_group ocfs2_attr_group = {
  431. .attrs = ocfs2_attrs,
  432. };
  433. static struct kset *ocfs2_kset;
  434. static void ocfs2_sysfs_exit(void)
  435. {
  436. kset_unregister(ocfs2_kset);
  437. }
  438. static int ocfs2_sysfs_init(void)
  439. {
  440. int ret;
  441. ocfs2_kset = kset_create_and_add("ocfs2", NULL, fs_kobj);
  442. if (!ocfs2_kset)
  443. return -ENOMEM;
  444. ret = sysfs_create_group(&ocfs2_kset->kobj, &ocfs2_attr_group);
  445. if (ret)
  446. goto error;
  447. return 0;
  448. error:
  449. kset_unregister(ocfs2_kset);
  450. return ret;
  451. }
  452. static int __init ocfs2_stack_glue_init(void)
  453. {
  454. strcpy(cluster_stack_name, OCFS2_STACK_PLUGIN_O2CB);
  455. return ocfs2_sysfs_init();
  456. }
  457. static void __exit ocfs2_stack_glue_exit(void)
  458. {
  459. lproto = NULL;
  460. ocfs2_sysfs_exit();
  461. }
  462. MODULE_AUTHOR("Oracle");
  463. MODULE_DESCRIPTION("ocfs2 cluter stack glue layer");
  464. MODULE_LICENSE("GPL");
  465. module_init(ocfs2_stack_glue_init);
  466. module_exit(ocfs2_stack_glue_exit);