stack_user.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * stack_user.c
  5. *
  6. * Code which interfaces ocfs2 with fs/dlm and a userspace stack.
  7. *
  8. * Copyright (C) 2007 Oracle. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public
  12. * License as published by the Free Software Foundation, version 2.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/fs.h>
  21. #include <linux/miscdevice.h>
  22. #include <linux/mutex.h>
  23. #include <linux/reboot.h>
  24. #include <asm/uaccess.h>
  25. #include "stackglue.h"
  26. /*
  27. * The control protocol starts with a handshake. Until the handshake
  28. * is complete, the control device will fail all write(2)s.
  29. *
  30. * The handshake is simple. First, the client reads until EOF. Each line
  31. * of output is a supported protocol tag. All protocol tags are a single
  32. * character followed by a two hex digit version number. Currently the
  33. * only things supported is T01, for "Text-base version 0x01". Next, the
  34. * client writes the version they would like to use, including the newline.
  35. * Thus, the protocol tag is 'T01\n'. If the version tag written is
  36. * unknown, -EINVAL is returned. Once the negotiation is complete, the
  37. * client can start sending messages.
  38. *
  39. * The T01 protocol only has two messages. First is the "SETN" message.
  40. * It has the following syntax:
  41. *
  42. * SETN<space><8-char-hex-nodenum><newline>
  43. *
  44. * This is 14 characters.
  45. *
  46. * The "SETN" message must be the first message following the protocol.
  47. * It tells ocfs2_control the local node number.
  48. *
  49. * Once the local node number has been set, the "DOWN" message can be
  50. * sent for node down notification. It has the following syntax:
  51. *
  52. * DOWN<space><32-char-cap-hex-uuid><space><8-char-hex-nodenum><newline>
  53. *
  54. * eg:
  55. *
  56. * DOWN 632A924FDD844190BDA93C0DF6B94899 00000001\n
  57. *
  58. * This is 47 characters.
  59. */
  60. /*
  61. * Whether or not the client has done the handshake.
  62. * For now, we have just one protocol version.
  63. */
  64. #define OCFS2_CONTROL_PROTO "T01\n"
  65. #define OCFS2_CONTROL_PROTO_LEN 4
  66. /* Handshake states */
  67. #define OCFS2_CONTROL_HANDSHAKE_INVALID (0)
  68. #define OCFS2_CONTROL_HANDSHAKE_READ (1)
  69. #define OCFS2_CONTROL_HANDSHAKE_PROTOCOL (2)
  70. #define OCFS2_CONTROL_HANDSHAKE_VALID (3)
  71. /* Messages */
  72. #define OCFS2_CONTROL_MESSAGE_OP_LEN 4
  73. #define OCFS2_CONTROL_MESSAGE_SETNODE_OP "SETN"
  74. #define OCFS2_CONTROL_MESSAGE_SETNODE_TOTAL_LEN 14
  75. #define OCFS2_CONTROL_MESSAGE_DOWN_OP "DOWN"
  76. #define OCFS2_CONTROL_MESSAGE_DOWN_TOTAL_LEN 47
  77. #define OCFS2_TEXT_UUID_LEN 32
  78. #define OCFS2_CONTROL_MESSAGE_NODENUM_LEN 8
  79. /*
  80. * ocfs2_live_connection is refcounted because the filesystem and
  81. * miscdevice sides can detach in different order. Let's just be safe.
  82. */
  83. struct ocfs2_live_connection {
  84. struct list_head oc_list;
  85. struct ocfs2_cluster_connection *oc_conn;
  86. };
  87. struct ocfs2_control_private {
  88. struct list_head op_list;
  89. int op_state;
  90. int op_this_node;
  91. };
  92. /* SETN<space><8-char-hex-nodenum><newline> */
  93. struct ocfs2_control_message_setn {
  94. char tag[OCFS2_CONTROL_MESSAGE_OP_LEN];
  95. char space;
  96. char nodestr[OCFS2_CONTROL_MESSAGE_NODENUM_LEN];
  97. char newline;
  98. };
  99. /* DOWN<space><32-char-cap-hex-uuid><space><8-char-hex-nodenum><newline> */
  100. struct ocfs2_control_message_down {
  101. char tag[OCFS2_CONTROL_MESSAGE_OP_LEN];
  102. char space1;
  103. char uuid[OCFS2_TEXT_UUID_LEN];
  104. char space2;
  105. char nodestr[OCFS2_CONTROL_MESSAGE_NODENUM_LEN];
  106. char newline;
  107. };
  108. union ocfs2_control_message {
  109. char tag[OCFS2_CONTROL_MESSAGE_OP_LEN];
  110. struct ocfs2_control_message_setn u_setn;
  111. struct ocfs2_control_message_down u_down;
  112. };
  113. static atomic_t ocfs2_control_opened;
  114. static int ocfs2_control_this_node = -1;
  115. static LIST_HEAD(ocfs2_live_connection_list);
  116. static LIST_HEAD(ocfs2_control_private_list);
  117. static DEFINE_MUTEX(ocfs2_control_lock);
  118. static inline void ocfs2_control_set_handshake_state(struct file *file,
  119. int state)
  120. {
  121. struct ocfs2_control_private *p = file->private_data;
  122. p->op_state = state;
  123. }
  124. static inline int ocfs2_control_get_handshake_state(struct file *file)
  125. {
  126. struct ocfs2_control_private *p = file->private_data;
  127. return p->op_state;
  128. }
  129. static struct ocfs2_live_connection *ocfs2_connection_find(const char *name)
  130. {
  131. size_t len = strlen(name);
  132. struct ocfs2_live_connection *c;
  133. BUG_ON(!mutex_is_locked(&ocfs2_control_lock));
  134. list_for_each_entry(c, &ocfs2_live_connection_list, oc_list) {
  135. if ((c->oc_conn->cc_namelen == len) &&
  136. !strncmp(c->oc_conn->cc_name, name, len))
  137. return c;
  138. }
  139. return c;
  140. }
  141. /*
  142. * ocfs2_live_connection structures are created underneath the ocfs2
  143. * mount path. Since the VFS prevents multiple calls to
  144. * fill_super(), we can't get dupes here.
  145. */
  146. static int ocfs2_live_connection_new(struct ocfs2_cluster_connection *conn,
  147. struct ocfs2_live_connection **c_ret)
  148. {
  149. int rc = 0;
  150. struct ocfs2_live_connection *c;
  151. c = kzalloc(sizeof(struct ocfs2_live_connection), GFP_KERNEL);
  152. if (!c)
  153. return -ENOMEM;
  154. mutex_lock(&ocfs2_control_lock);
  155. c->oc_conn = conn;
  156. if (atomic_read(&ocfs2_control_opened))
  157. list_add(&c->oc_list, &ocfs2_live_connection_list);
  158. else {
  159. printk(KERN_ERR
  160. "ocfs2: Userspace control daemon is not present\n");
  161. rc = -ESRCH;
  162. }
  163. mutex_unlock(&ocfs2_control_lock);
  164. if (!rc)
  165. *c_ret = c;
  166. else
  167. kfree(c);
  168. return rc;
  169. }
  170. /*
  171. * This function disconnects the cluster connection from ocfs2_control.
  172. * Afterwards, userspace can't affect the cluster connection.
  173. */
  174. static void ocfs2_live_connection_drop(struct ocfs2_live_connection *c)
  175. {
  176. mutex_lock(&ocfs2_control_lock);
  177. list_del_init(&c->oc_list);
  178. c->oc_conn = NULL;
  179. mutex_unlock(&ocfs2_control_lock);
  180. kfree(c);
  181. }
  182. static int ocfs2_control_cfu(void *target, size_t target_len,
  183. const char __user *buf, size_t count)
  184. {
  185. /* The T01 expects write(2) calls to have exactly one command */
  186. if ((count != target_len) ||
  187. (count > sizeof(union ocfs2_control_message)))
  188. return -EINVAL;
  189. if (copy_from_user(target, buf, target_len))
  190. return -EFAULT;
  191. return 0;
  192. }
  193. static ssize_t ocfs2_control_validate_protocol(struct file *file,
  194. const char __user *buf,
  195. size_t count)
  196. {
  197. ssize_t ret;
  198. char kbuf[OCFS2_CONTROL_PROTO_LEN];
  199. ret = ocfs2_control_cfu(kbuf, OCFS2_CONTROL_PROTO_LEN,
  200. buf, count);
  201. if (ret)
  202. return ret;
  203. if (strncmp(kbuf, OCFS2_CONTROL_PROTO, OCFS2_CONTROL_PROTO_LEN))
  204. return -EINVAL;
  205. ocfs2_control_set_handshake_state(file,
  206. OCFS2_CONTROL_HANDSHAKE_PROTOCOL);
  207. return count;
  208. }
  209. static void ocfs2_control_send_down(const char *uuid,
  210. int nodenum)
  211. {
  212. struct ocfs2_live_connection *c;
  213. mutex_lock(&ocfs2_control_lock);
  214. c = ocfs2_connection_find(uuid);
  215. if (c) {
  216. BUG_ON(c->oc_conn == NULL);
  217. c->oc_conn->cc_recovery_handler(nodenum,
  218. c->oc_conn->cc_recovery_data);
  219. }
  220. mutex_unlock(&ocfs2_control_lock);
  221. }
  222. /*
  223. * Called whenever configuration elements are sent to /dev/ocfs2_control.
  224. * If all configuration elements are present, try to set the global
  225. * values. If not, return -EAGAIN. If there is a problem, return a
  226. * different error.
  227. */
  228. static int ocfs2_control_install_private(struct file *file)
  229. {
  230. int rc = 0;
  231. int set_p = 1;
  232. struct ocfs2_control_private *p = file->private_data;
  233. BUG_ON(p->op_state != OCFS2_CONTROL_HANDSHAKE_PROTOCOL);
  234. if (p->op_this_node < 0)
  235. set_p = 0;
  236. mutex_lock(&ocfs2_control_lock);
  237. if (ocfs2_control_this_node < 0) {
  238. if (set_p)
  239. ocfs2_control_this_node = p->op_this_node;
  240. } else if (ocfs2_control_this_node != p->op_this_node)
  241. rc = -EINVAL;
  242. mutex_unlock(&ocfs2_control_lock);
  243. if (!rc && set_p) {
  244. /* We set the global values successfully */
  245. atomic_inc(&ocfs2_control_opened);
  246. ocfs2_control_set_handshake_state(file,
  247. OCFS2_CONTROL_HANDSHAKE_VALID);
  248. }
  249. return rc;
  250. }
  251. static int ocfs2_control_do_setnode_msg(struct file *file,
  252. struct ocfs2_control_message_setn *msg)
  253. {
  254. long nodenum;
  255. char *ptr = NULL;
  256. struct ocfs2_control_private *p = file->private_data;
  257. if (ocfs2_control_get_handshake_state(file) !=
  258. OCFS2_CONTROL_HANDSHAKE_PROTOCOL)
  259. return -EINVAL;
  260. if (strncmp(msg->tag, OCFS2_CONTROL_MESSAGE_SETNODE_OP,
  261. OCFS2_CONTROL_MESSAGE_OP_LEN))
  262. return -EINVAL;
  263. if ((msg->space != ' ') || (msg->newline != '\n'))
  264. return -EINVAL;
  265. msg->space = msg->newline = '\0';
  266. nodenum = simple_strtol(msg->nodestr, &ptr, 16);
  267. if (!ptr || *ptr)
  268. return -EINVAL;
  269. if ((nodenum == LONG_MIN) || (nodenum == LONG_MAX) ||
  270. (nodenum > INT_MAX) || (nodenum < 0))
  271. return -ERANGE;
  272. p->op_this_node = nodenum;
  273. return ocfs2_control_install_private(file);
  274. }
  275. static int ocfs2_control_do_down_msg(struct file *file,
  276. struct ocfs2_control_message_down *msg)
  277. {
  278. long nodenum;
  279. char *p = NULL;
  280. if (ocfs2_control_get_handshake_state(file) !=
  281. OCFS2_CONTROL_HANDSHAKE_VALID)
  282. return -EINVAL;
  283. if (strncmp(msg->tag, OCFS2_CONTROL_MESSAGE_DOWN_OP,
  284. OCFS2_CONTROL_MESSAGE_OP_LEN))
  285. return -EINVAL;
  286. if ((msg->space1 != ' ') || (msg->space2 != ' ') ||
  287. (msg->newline != '\n'))
  288. return -EINVAL;
  289. msg->space1 = msg->space2 = msg->newline = '\0';
  290. nodenum = simple_strtol(msg->nodestr, &p, 16);
  291. if (!p || *p)
  292. return -EINVAL;
  293. if ((nodenum == LONG_MIN) || (nodenum == LONG_MAX) ||
  294. (nodenum > INT_MAX) || (nodenum < 0))
  295. return -ERANGE;
  296. ocfs2_control_send_down(msg->uuid, nodenum);
  297. return 0;
  298. }
  299. static ssize_t ocfs2_control_message(struct file *file,
  300. const char __user *buf,
  301. size_t count)
  302. {
  303. ssize_t ret;
  304. union ocfs2_control_message msg;
  305. /* Try to catch padding issues */
  306. WARN_ON(offsetof(struct ocfs2_control_message_down, uuid) !=
  307. (sizeof(msg.u_down.tag) + sizeof(msg.u_down.space1)));
  308. memset(&msg, 0, sizeof(union ocfs2_control_message));
  309. ret = ocfs2_control_cfu(&msg, count, buf, count);
  310. if (ret)
  311. goto out;
  312. if ((count == OCFS2_CONTROL_MESSAGE_SETNODE_TOTAL_LEN) &&
  313. !strncmp(msg.tag, OCFS2_CONTROL_MESSAGE_SETNODE_OP,
  314. OCFS2_CONTROL_MESSAGE_OP_LEN))
  315. ret = ocfs2_control_do_setnode_msg(file, &msg.u_setn);
  316. else if ((count == OCFS2_CONTROL_MESSAGE_DOWN_TOTAL_LEN) &&
  317. !strncmp(msg.tag, OCFS2_CONTROL_MESSAGE_DOWN_OP,
  318. OCFS2_CONTROL_MESSAGE_OP_LEN))
  319. ret = ocfs2_control_do_down_msg(file, &msg.u_down);
  320. else
  321. ret = -EINVAL;
  322. out:
  323. return ret ? ret : count;
  324. }
  325. static ssize_t ocfs2_control_write(struct file *file,
  326. const char __user *buf,
  327. size_t count,
  328. loff_t *ppos)
  329. {
  330. ssize_t ret;
  331. switch (ocfs2_control_get_handshake_state(file)) {
  332. case OCFS2_CONTROL_HANDSHAKE_INVALID:
  333. ret = -EINVAL;
  334. break;
  335. case OCFS2_CONTROL_HANDSHAKE_READ:
  336. ret = ocfs2_control_validate_protocol(file, buf,
  337. count);
  338. break;
  339. case OCFS2_CONTROL_HANDSHAKE_PROTOCOL:
  340. case OCFS2_CONTROL_HANDSHAKE_VALID:
  341. ret = ocfs2_control_message(file, buf, count);
  342. break;
  343. default:
  344. BUG();
  345. ret = -EIO;
  346. break;
  347. }
  348. return ret;
  349. }
  350. /*
  351. * This is a naive version. If we ever have a new protocol, we'll expand
  352. * it. Probably using seq_file.
  353. */
  354. static ssize_t ocfs2_control_read(struct file *file,
  355. char __user *buf,
  356. size_t count,
  357. loff_t *ppos)
  358. {
  359. char *proto_string = OCFS2_CONTROL_PROTO;
  360. size_t to_write = 0;
  361. if (*ppos >= OCFS2_CONTROL_PROTO_LEN)
  362. return 0;
  363. to_write = OCFS2_CONTROL_PROTO_LEN - *ppos;
  364. if (to_write > count)
  365. to_write = count;
  366. if (copy_to_user(buf, proto_string + *ppos, to_write))
  367. return -EFAULT;
  368. *ppos += to_write;
  369. /* Have we read the whole protocol list? */
  370. if (*ppos >= OCFS2_CONTROL_PROTO_LEN)
  371. ocfs2_control_set_handshake_state(file,
  372. OCFS2_CONTROL_HANDSHAKE_READ);
  373. return to_write;
  374. }
  375. static int ocfs2_control_release(struct inode *inode, struct file *file)
  376. {
  377. struct ocfs2_control_private *p = file->private_data;
  378. mutex_lock(&ocfs2_control_lock);
  379. if (ocfs2_control_get_handshake_state(file) !=
  380. OCFS2_CONTROL_HANDSHAKE_VALID)
  381. goto out;
  382. if (atomic_dec_and_test(&ocfs2_control_opened)) {
  383. if (!list_empty(&ocfs2_live_connection_list)) {
  384. /* XXX: Do bad things! */
  385. printk(KERN_ERR
  386. "ocfs2: Unexpected release of ocfs2_control!\n"
  387. " Loss of cluster connection requires "
  388. "an emergency restart!\n");
  389. emergency_restart();
  390. }
  391. /* Last valid close clears the node number */
  392. ocfs2_control_this_node = -1;
  393. }
  394. out:
  395. list_del_init(&p->op_list);
  396. file->private_data = NULL;
  397. mutex_unlock(&ocfs2_control_lock);
  398. kfree(p);
  399. return 0;
  400. }
  401. static int ocfs2_control_open(struct inode *inode, struct file *file)
  402. {
  403. struct ocfs2_control_private *p;
  404. p = kzalloc(sizeof(struct ocfs2_control_private), GFP_KERNEL);
  405. if (!p)
  406. return -ENOMEM;
  407. p->op_this_node = -1;
  408. mutex_lock(&ocfs2_control_lock);
  409. file->private_data = p;
  410. list_add(&p->op_list, &ocfs2_control_private_list);
  411. mutex_unlock(&ocfs2_control_lock);
  412. return 0;
  413. }
  414. static const struct file_operations ocfs2_control_fops = {
  415. .open = ocfs2_control_open,
  416. .release = ocfs2_control_release,
  417. .read = ocfs2_control_read,
  418. .write = ocfs2_control_write,
  419. .owner = THIS_MODULE,
  420. };
  421. struct miscdevice ocfs2_control_device = {
  422. .minor = MISC_DYNAMIC_MINOR,
  423. .name = "ocfs2_control",
  424. .fops = &ocfs2_control_fops,
  425. };
  426. static int ocfs2_control_init(void)
  427. {
  428. int rc;
  429. atomic_set(&ocfs2_control_opened, 0);
  430. rc = misc_register(&ocfs2_control_device);
  431. if (rc)
  432. printk(KERN_ERR
  433. "ocfs2: Unable to register ocfs2_control device "
  434. "(errno %d)\n",
  435. -rc);
  436. return rc;
  437. }
  438. static void ocfs2_control_exit(void)
  439. {
  440. int rc;
  441. rc = misc_deregister(&ocfs2_control_device);
  442. if (rc)
  443. printk(KERN_ERR
  444. "ocfs2: Unable to deregister ocfs2_control device "
  445. "(errno %d)\n",
  446. -rc);
  447. }
  448. static int __init user_stack_init(void)
  449. {
  450. return ocfs2_control_init();
  451. }
  452. static void __exit user_stack_exit(void)
  453. {
  454. ocfs2_control_exit();
  455. }
  456. MODULE_AUTHOR("Oracle");
  457. MODULE_DESCRIPTION("ocfs2 driver for userspace cluster stacks");
  458. MODULE_LICENSE("GPL");
  459. module_init(user_stack_init);
  460. module_exit(user_stack_exit);