stackglue.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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 <linux/sysctl.h>
  29. #include "ocfs2_fs.h"
  30. #include "stackglue.h"
  31. #define OCFS2_STACK_PLUGIN_O2CB "o2cb"
  32. #define OCFS2_STACK_PLUGIN_USER "user"
  33. static struct ocfs2_locking_protocol *lproto;
  34. static DEFINE_SPINLOCK(ocfs2_stack_lock);
  35. static LIST_HEAD(ocfs2_stack_list);
  36. static char cluster_stack_name[OCFS2_STACK_LABEL_LEN + 1];
  37. /*
  38. * The stack currently in use. If not null, active_stack->sp_count > 0,
  39. * the module is pinned, and the locking protocol cannot be changed.
  40. */
  41. static struct ocfs2_stack_plugin *active_stack;
  42. static struct ocfs2_stack_plugin *ocfs2_stack_lookup(const char *name)
  43. {
  44. struct ocfs2_stack_plugin *p;
  45. assert_spin_locked(&ocfs2_stack_lock);
  46. list_for_each_entry(p, &ocfs2_stack_list, sp_list) {
  47. if (!strcmp(p->sp_name, name))
  48. return p;
  49. }
  50. return NULL;
  51. }
  52. static int ocfs2_stack_driver_request(const char *stack_name,
  53. const char *plugin_name)
  54. {
  55. int rc;
  56. struct ocfs2_stack_plugin *p;
  57. spin_lock(&ocfs2_stack_lock);
  58. /*
  59. * If the stack passed by the filesystem isn't the selected one,
  60. * we can't continue.
  61. */
  62. if (strcmp(stack_name, cluster_stack_name)) {
  63. rc = -EBUSY;
  64. goto out;
  65. }
  66. if (active_stack) {
  67. /*
  68. * If the active stack isn't the one we want, it cannot
  69. * be selected right now.
  70. */
  71. if (!strcmp(active_stack->sp_name, plugin_name))
  72. rc = 0;
  73. else
  74. rc = -EBUSY;
  75. goto out;
  76. }
  77. p = ocfs2_stack_lookup(plugin_name);
  78. if (!p || !try_module_get(p->sp_owner)) {
  79. rc = -ENOENT;
  80. goto out;
  81. }
  82. /* Ok, the stack is pinned */
  83. p->sp_count++;
  84. active_stack = p;
  85. rc = 0;
  86. out:
  87. spin_unlock(&ocfs2_stack_lock);
  88. return rc;
  89. }
  90. /*
  91. * This function looks up the appropriate stack and makes it active. If
  92. * there is no stack, it tries to load it. It will fail if the stack still
  93. * cannot be found. It will also fail if a different stack is in use.
  94. */
  95. static int ocfs2_stack_driver_get(const char *stack_name)
  96. {
  97. int rc;
  98. char *plugin_name = OCFS2_STACK_PLUGIN_O2CB;
  99. /*
  100. * Classic stack does not pass in a stack name. This is
  101. * compatible with older tools as well.
  102. */
  103. if (!stack_name || !*stack_name)
  104. stack_name = OCFS2_STACK_PLUGIN_O2CB;
  105. if (strlen(stack_name) != OCFS2_STACK_LABEL_LEN) {
  106. printk(KERN_ERR
  107. "ocfs2 passed an invalid cluster stack label: \"%s\"\n",
  108. stack_name);
  109. return -EINVAL;
  110. }
  111. /* Anything that isn't the classic stack is a user stack */
  112. if (strcmp(stack_name, OCFS2_STACK_PLUGIN_O2CB))
  113. plugin_name = OCFS2_STACK_PLUGIN_USER;
  114. rc = ocfs2_stack_driver_request(stack_name, plugin_name);
  115. if (rc == -ENOENT) {
  116. request_module("ocfs2_stack_%s", plugin_name);
  117. rc = ocfs2_stack_driver_request(stack_name, plugin_name);
  118. }
  119. if (rc == -ENOENT) {
  120. printk(KERN_ERR
  121. "ocfs2: Cluster stack driver \"%s\" cannot be found\n",
  122. plugin_name);
  123. } else if (rc == -EBUSY) {
  124. printk(KERN_ERR
  125. "ocfs2: A different cluster stack is in use\n");
  126. }
  127. return rc;
  128. }
  129. static void ocfs2_stack_driver_put(void)
  130. {
  131. spin_lock(&ocfs2_stack_lock);
  132. BUG_ON(active_stack == NULL);
  133. BUG_ON(active_stack->sp_count == 0);
  134. active_stack->sp_count--;
  135. if (!active_stack->sp_count) {
  136. module_put(active_stack->sp_owner);
  137. active_stack = NULL;
  138. }
  139. spin_unlock(&ocfs2_stack_lock);
  140. }
  141. int ocfs2_stack_glue_register(struct ocfs2_stack_plugin *plugin)
  142. {
  143. int rc;
  144. spin_lock(&ocfs2_stack_lock);
  145. if (!ocfs2_stack_lookup(plugin->sp_name)) {
  146. plugin->sp_count = 0;
  147. plugin->sp_proto = lproto;
  148. list_add(&plugin->sp_list, &ocfs2_stack_list);
  149. printk(KERN_INFO "ocfs2: Registered cluster interface %s\n",
  150. plugin->sp_name);
  151. rc = 0;
  152. } else {
  153. printk(KERN_ERR "ocfs2: Stack \"%s\" already registered\n",
  154. plugin->sp_name);
  155. rc = -EEXIST;
  156. }
  157. spin_unlock(&ocfs2_stack_lock);
  158. return rc;
  159. }
  160. EXPORT_SYMBOL_GPL(ocfs2_stack_glue_register);
  161. void ocfs2_stack_glue_unregister(struct ocfs2_stack_plugin *plugin)
  162. {
  163. struct ocfs2_stack_plugin *p;
  164. spin_lock(&ocfs2_stack_lock);
  165. p = ocfs2_stack_lookup(plugin->sp_name);
  166. if (p) {
  167. BUG_ON(p != plugin);
  168. BUG_ON(plugin == active_stack);
  169. BUG_ON(plugin->sp_count != 0);
  170. list_del_init(&plugin->sp_list);
  171. printk(KERN_INFO "ocfs2: Unregistered cluster interface %s\n",
  172. plugin->sp_name);
  173. } else {
  174. printk(KERN_ERR "Stack \"%s\" is not registered\n",
  175. plugin->sp_name);
  176. }
  177. spin_unlock(&ocfs2_stack_lock);
  178. }
  179. EXPORT_SYMBOL_GPL(ocfs2_stack_glue_unregister);
  180. void ocfs2_stack_glue_set_locking_protocol(struct ocfs2_locking_protocol *proto)
  181. {
  182. struct ocfs2_stack_plugin *p;
  183. BUG_ON(proto == NULL);
  184. spin_lock(&ocfs2_stack_lock);
  185. BUG_ON(active_stack != NULL);
  186. lproto = proto;
  187. list_for_each_entry(p, &ocfs2_stack_list, sp_list) {
  188. p->sp_proto = lproto;
  189. }
  190. spin_unlock(&ocfs2_stack_lock);
  191. }
  192. EXPORT_SYMBOL_GPL(ocfs2_stack_glue_set_locking_protocol);
  193. /*
  194. * The ocfs2_dlm_lock() and ocfs2_dlm_unlock() functions take
  195. * "struct ocfs2_lock_res *astarg" instead of "void *astarg" because the
  196. * underlying stack plugins need to pilfer the lksb off of the lock_res.
  197. * If some other structure needs to be passed as an astarg, the plugins
  198. * will need to be given a different avenue to the lksb.
  199. */
  200. int ocfs2_dlm_lock(struct ocfs2_cluster_connection *conn,
  201. int mode,
  202. union ocfs2_dlm_lksb *lksb,
  203. u32 flags,
  204. void *name,
  205. unsigned int namelen,
  206. struct ocfs2_lock_res *astarg)
  207. {
  208. BUG_ON(lproto == NULL);
  209. return active_stack->sp_ops->dlm_lock(conn, mode, lksb, flags,
  210. name, namelen, astarg);
  211. }
  212. EXPORT_SYMBOL_GPL(ocfs2_dlm_lock);
  213. int ocfs2_dlm_unlock(struct ocfs2_cluster_connection *conn,
  214. union ocfs2_dlm_lksb *lksb,
  215. u32 flags,
  216. struct ocfs2_lock_res *astarg)
  217. {
  218. BUG_ON(lproto == NULL);
  219. return active_stack->sp_ops->dlm_unlock(conn, lksb, flags, astarg);
  220. }
  221. EXPORT_SYMBOL_GPL(ocfs2_dlm_unlock);
  222. int ocfs2_dlm_lock_status(union ocfs2_dlm_lksb *lksb)
  223. {
  224. return active_stack->sp_ops->lock_status(lksb);
  225. }
  226. EXPORT_SYMBOL_GPL(ocfs2_dlm_lock_status);
  227. /*
  228. * Why don't we cast to ocfs2_meta_lvb? The "clean" answer is that we
  229. * don't cast at the glue level. The real answer is that the header
  230. * ordering is nigh impossible.
  231. */
  232. void *ocfs2_dlm_lvb(union ocfs2_dlm_lksb *lksb)
  233. {
  234. return active_stack->sp_ops->lock_lvb(lksb);
  235. }
  236. EXPORT_SYMBOL_GPL(ocfs2_dlm_lvb);
  237. void ocfs2_dlm_dump_lksb(union ocfs2_dlm_lksb *lksb)
  238. {
  239. active_stack->sp_ops->dump_lksb(lksb);
  240. }
  241. EXPORT_SYMBOL_GPL(ocfs2_dlm_dump_lksb);
  242. int ocfs2_cluster_connect(const char *stack_name,
  243. const char *group,
  244. int grouplen,
  245. void (*recovery_handler)(int node_num,
  246. void *recovery_data),
  247. void *recovery_data,
  248. struct ocfs2_cluster_connection **conn)
  249. {
  250. int rc = 0;
  251. struct ocfs2_cluster_connection *new_conn;
  252. BUG_ON(group == NULL);
  253. BUG_ON(conn == NULL);
  254. BUG_ON(recovery_handler == NULL);
  255. if (grouplen > GROUP_NAME_MAX) {
  256. rc = -EINVAL;
  257. goto out;
  258. }
  259. new_conn = kzalloc(sizeof(struct ocfs2_cluster_connection),
  260. GFP_KERNEL);
  261. if (!new_conn) {
  262. rc = -ENOMEM;
  263. goto out;
  264. }
  265. memcpy(new_conn->cc_name, group, grouplen);
  266. new_conn->cc_namelen = grouplen;
  267. new_conn->cc_recovery_handler = recovery_handler;
  268. new_conn->cc_recovery_data = recovery_data;
  269. /* Start the new connection at our maximum compatibility level */
  270. new_conn->cc_version = lproto->lp_max_version;
  271. /* This will pin the stack driver if successful */
  272. rc = ocfs2_stack_driver_get(stack_name);
  273. if (rc)
  274. goto out_free;
  275. rc = active_stack->sp_ops->connect(new_conn);
  276. if (rc) {
  277. ocfs2_stack_driver_put();
  278. goto out_free;
  279. }
  280. *conn = new_conn;
  281. out_free:
  282. if (rc)
  283. kfree(new_conn);
  284. out:
  285. return rc;
  286. }
  287. EXPORT_SYMBOL_GPL(ocfs2_cluster_connect);
  288. /* If hangup_pending is 0, the stack driver will be dropped */
  289. int ocfs2_cluster_disconnect(struct ocfs2_cluster_connection *conn,
  290. int hangup_pending)
  291. {
  292. int ret;
  293. BUG_ON(conn == NULL);
  294. ret = active_stack->sp_ops->disconnect(conn, hangup_pending);
  295. /* XXX Should we free it anyway? */
  296. if (!ret) {
  297. kfree(conn);
  298. if (!hangup_pending)
  299. ocfs2_stack_driver_put();
  300. }
  301. return ret;
  302. }
  303. EXPORT_SYMBOL_GPL(ocfs2_cluster_disconnect);
  304. void ocfs2_cluster_hangup(const char *group, int grouplen)
  305. {
  306. BUG_ON(group == NULL);
  307. BUG_ON(group[grouplen] != '\0');
  308. if (active_stack->sp_ops->hangup)
  309. active_stack->sp_ops->hangup(group, grouplen);
  310. /* cluster_disconnect() was called with hangup_pending==1 */
  311. ocfs2_stack_driver_put();
  312. }
  313. EXPORT_SYMBOL_GPL(ocfs2_cluster_hangup);
  314. int ocfs2_cluster_this_node(unsigned int *node)
  315. {
  316. return active_stack->sp_ops->this_node(node);
  317. }
  318. EXPORT_SYMBOL_GPL(ocfs2_cluster_this_node);
  319. /*
  320. * Sysfs bits
  321. */
  322. static ssize_t ocfs2_max_locking_protocol_show(struct kobject *kobj,
  323. struct kobj_attribute *attr,
  324. char *buf)
  325. {
  326. ssize_t ret = 0;
  327. spin_lock(&ocfs2_stack_lock);
  328. if (lproto)
  329. ret = snprintf(buf, PAGE_SIZE, "%u.%u\n",
  330. lproto->lp_max_version.pv_major,
  331. lproto->lp_max_version.pv_minor);
  332. spin_unlock(&ocfs2_stack_lock);
  333. return ret;
  334. }
  335. static struct kobj_attribute ocfs2_attr_max_locking_protocol =
  336. __ATTR(max_locking_protocol, S_IFREG | S_IRUGO,
  337. ocfs2_max_locking_protocol_show, NULL);
  338. static ssize_t ocfs2_loaded_cluster_plugins_show(struct kobject *kobj,
  339. struct kobj_attribute *attr,
  340. char *buf)
  341. {
  342. ssize_t ret = 0, total = 0, remain = PAGE_SIZE;
  343. struct ocfs2_stack_plugin *p;
  344. spin_lock(&ocfs2_stack_lock);
  345. list_for_each_entry(p, &ocfs2_stack_list, sp_list) {
  346. ret = snprintf(buf, remain, "%s\n",
  347. p->sp_name);
  348. if (ret < 0) {
  349. total = ret;
  350. break;
  351. }
  352. if (ret == remain) {
  353. /* snprintf() didn't fit */
  354. total = -E2BIG;
  355. break;
  356. }
  357. total += ret;
  358. remain -= ret;
  359. }
  360. spin_unlock(&ocfs2_stack_lock);
  361. return total;
  362. }
  363. static struct kobj_attribute ocfs2_attr_loaded_cluster_plugins =
  364. __ATTR(loaded_cluster_plugins, S_IFREG | S_IRUGO,
  365. ocfs2_loaded_cluster_plugins_show, NULL);
  366. static ssize_t ocfs2_active_cluster_plugin_show(struct kobject *kobj,
  367. struct kobj_attribute *attr,
  368. char *buf)
  369. {
  370. ssize_t ret = 0;
  371. spin_lock(&ocfs2_stack_lock);
  372. if (active_stack) {
  373. ret = snprintf(buf, PAGE_SIZE, "%s\n",
  374. active_stack->sp_name);
  375. if (ret == PAGE_SIZE)
  376. ret = -E2BIG;
  377. }
  378. spin_unlock(&ocfs2_stack_lock);
  379. return ret;
  380. }
  381. static struct kobj_attribute ocfs2_attr_active_cluster_plugin =
  382. __ATTR(active_cluster_plugin, S_IFREG | S_IRUGO,
  383. ocfs2_active_cluster_plugin_show, NULL);
  384. static ssize_t ocfs2_cluster_stack_show(struct kobject *kobj,
  385. struct kobj_attribute *attr,
  386. char *buf)
  387. {
  388. ssize_t ret;
  389. spin_lock(&ocfs2_stack_lock);
  390. ret = snprintf(buf, PAGE_SIZE, "%s\n", cluster_stack_name);
  391. spin_unlock(&ocfs2_stack_lock);
  392. return ret;
  393. }
  394. static ssize_t ocfs2_cluster_stack_store(struct kobject *kobj,
  395. struct kobj_attribute *attr,
  396. const char *buf, size_t count)
  397. {
  398. size_t len = count;
  399. ssize_t ret;
  400. if (len == 0)
  401. return len;
  402. if (buf[len - 1] == '\n')
  403. len--;
  404. if ((len != OCFS2_STACK_LABEL_LEN) ||
  405. (strnlen(buf, len) != len))
  406. return -EINVAL;
  407. spin_lock(&ocfs2_stack_lock);
  408. if (active_stack) {
  409. if (!strncmp(buf, cluster_stack_name, len))
  410. ret = count;
  411. else
  412. ret = -EBUSY;
  413. } else {
  414. memcpy(cluster_stack_name, buf, len);
  415. ret = count;
  416. }
  417. spin_unlock(&ocfs2_stack_lock);
  418. return ret;
  419. }
  420. static struct kobj_attribute ocfs2_attr_cluster_stack =
  421. __ATTR(cluster_stack, S_IFREG | S_IRUGO | S_IWUSR,
  422. ocfs2_cluster_stack_show,
  423. ocfs2_cluster_stack_store);
  424. static struct attribute *ocfs2_attrs[] = {
  425. &ocfs2_attr_max_locking_protocol.attr,
  426. &ocfs2_attr_loaded_cluster_plugins.attr,
  427. &ocfs2_attr_active_cluster_plugin.attr,
  428. &ocfs2_attr_cluster_stack.attr,
  429. NULL,
  430. };
  431. static struct attribute_group ocfs2_attr_group = {
  432. .attrs = ocfs2_attrs,
  433. };
  434. static struct kset *ocfs2_kset;
  435. static void ocfs2_sysfs_exit(void)
  436. {
  437. kset_unregister(ocfs2_kset);
  438. }
  439. static int ocfs2_sysfs_init(void)
  440. {
  441. int ret;
  442. ocfs2_kset = kset_create_and_add("ocfs2", NULL, fs_kobj);
  443. if (!ocfs2_kset)
  444. return -ENOMEM;
  445. ret = sysfs_create_group(&ocfs2_kset->kobj, &ocfs2_attr_group);
  446. if (ret)
  447. goto error;
  448. return 0;
  449. error:
  450. kset_unregister(ocfs2_kset);
  451. return ret;
  452. }
  453. /*
  454. * Sysctl bits
  455. *
  456. * The sysctl lives at /proc/sys/fs/ocfs2/nm/hb_ctl_path. The 'nm' doesn't
  457. * make as much sense in a multiple cluster stack world, but it's safer
  458. * and easier to preserve the name.
  459. */
  460. #define FS_OCFS2_NM 1
  461. #define OCFS2_MAX_HB_CTL_PATH 256
  462. static char ocfs2_hb_ctl_path[OCFS2_MAX_HB_CTL_PATH] = "/sbin/ocfs2_hb_ctl";
  463. static ctl_table ocfs2_nm_table[] = {
  464. {
  465. .ctl_name = 1,
  466. .procname = "hb_ctl_path",
  467. .data = ocfs2_hb_ctl_path,
  468. .maxlen = OCFS2_MAX_HB_CTL_PATH,
  469. .mode = 0644,
  470. .proc_handler = &proc_dostring,
  471. .strategy = &sysctl_string,
  472. },
  473. { .ctl_name = 0 }
  474. };
  475. static ctl_table ocfs2_mod_table[] = {
  476. {
  477. .ctl_name = FS_OCFS2_NM,
  478. .procname = "nm",
  479. .data = NULL,
  480. .maxlen = 0,
  481. .mode = 0555,
  482. .child = ocfs2_nm_table
  483. },
  484. { .ctl_name = 0}
  485. };
  486. static ctl_table ocfs2_kern_table[] = {
  487. {
  488. .ctl_name = FS_OCFS2,
  489. .procname = "ocfs2",
  490. .data = NULL,
  491. .maxlen = 0,
  492. .mode = 0555,
  493. .child = ocfs2_mod_table
  494. },
  495. { .ctl_name = 0}
  496. };
  497. static ctl_table ocfs2_root_table[] = {
  498. {
  499. .ctl_name = CTL_FS,
  500. .procname = "fs",
  501. .data = NULL,
  502. .maxlen = 0,
  503. .mode = 0555,
  504. .child = ocfs2_kern_table
  505. },
  506. { .ctl_name = 0 }
  507. };
  508. static struct ctl_table_header *ocfs2_table_header = NULL;
  509. const char *ocfs2_get_hb_ctl_path(void)
  510. {
  511. return ocfs2_hb_ctl_path;
  512. }
  513. EXPORT_SYMBOL_GPL(ocfs2_get_hb_ctl_path);
  514. /*
  515. * Initialization
  516. */
  517. static int __init ocfs2_stack_glue_init(void)
  518. {
  519. strcpy(cluster_stack_name, OCFS2_STACK_PLUGIN_O2CB);
  520. ocfs2_table_header = register_sysctl_table(ocfs2_root_table);
  521. if (!ocfs2_table_header) {
  522. printk(KERN_ERR
  523. "ocfs2 stack glue: unable to register sysctl\n");
  524. return -ENOMEM; /* or something. */
  525. }
  526. return ocfs2_sysfs_init();
  527. }
  528. static void __exit ocfs2_stack_glue_exit(void)
  529. {
  530. lproto = NULL;
  531. ocfs2_sysfs_exit();
  532. if (ocfs2_table_header)
  533. unregister_sysctl_table(ocfs2_table_header);
  534. }
  535. MODULE_AUTHOR("Oracle");
  536. MODULE_DESCRIPTION("ocfs2 cluter stack glue layer");
  537. MODULE_LICENSE("GPL");
  538. module_init(ocfs2_stack_glue_init);
  539. module_exit(ocfs2_stack_glue_exit);