stack_user.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  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/slab.h>
  24. #include <linux/reboot.h>
  25. #include <asm/uaccess.h>
  26. #include "stackglue.h"
  27. #include <linux/dlm_plock.h>
  28. /*
  29. * The control protocol starts with a handshake. Until the handshake
  30. * is complete, the control device will fail all write(2)s.
  31. *
  32. * The handshake is simple. First, the client reads until EOF. Each line
  33. * of output is a supported protocol tag. All protocol tags are a single
  34. * character followed by a two hex digit version number. Currently the
  35. * only things supported is T01, for "Text-base version 0x01". Next, the
  36. * client writes the version they would like to use, including the newline.
  37. * Thus, the protocol tag is 'T01\n'. If the version tag written is
  38. * unknown, -EINVAL is returned. Once the negotiation is complete, the
  39. * client can start sending messages.
  40. *
  41. * The T01 protocol has three messages. First is the "SETN" message.
  42. * It has the following syntax:
  43. *
  44. * SETN<space><8-char-hex-nodenum><newline>
  45. *
  46. * This is 14 characters.
  47. *
  48. * The "SETN" message must be the first message following the protocol.
  49. * It tells ocfs2_control the local node number.
  50. *
  51. * Next comes the "SETV" message. It has the following syntax:
  52. *
  53. * SETV<space><2-char-hex-major><space><2-char-hex-minor><newline>
  54. *
  55. * This is 11 characters.
  56. *
  57. * The "SETV" message sets the filesystem locking protocol version as
  58. * negotiated by the client. The client negotiates based on the maximum
  59. * version advertised in /sys/fs/ocfs2/max_locking_protocol. The major
  60. * number from the "SETV" message must match
  61. * ocfs2_user_plugin.sp_max_proto.pv_major, and the minor number
  62. * must be less than or equal to ...sp_max_version.pv_minor.
  63. *
  64. * Once this information has been set, mounts will be allowed. From this
  65. * point on, the "DOWN" message can be sent for node down notification.
  66. * It has the following syntax:
  67. *
  68. * DOWN<space><32-char-cap-hex-uuid><space><8-char-hex-nodenum><newline>
  69. *
  70. * eg:
  71. *
  72. * DOWN 632A924FDD844190BDA93C0DF6B94899 00000001\n
  73. *
  74. * This is 47 characters.
  75. */
  76. /*
  77. * Whether or not the client has done the handshake.
  78. * For now, we have just one protocol version.
  79. */
  80. #define OCFS2_CONTROL_PROTO "T01\n"
  81. #define OCFS2_CONTROL_PROTO_LEN 4
  82. /* Handshake states */
  83. #define OCFS2_CONTROL_HANDSHAKE_INVALID (0)
  84. #define OCFS2_CONTROL_HANDSHAKE_READ (1)
  85. #define OCFS2_CONTROL_HANDSHAKE_PROTOCOL (2)
  86. #define OCFS2_CONTROL_HANDSHAKE_VALID (3)
  87. /* Messages */
  88. #define OCFS2_CONTROL_MESSAGE_OP_LEN 4
  89. #define OCFS2_CONTROL_MESSAGE_SETNODE_OP "SETN"
  90. #define OCFS2_CONTROL_MESSAGE_SETNODE_TOTAL_LEN 14
  91. #define OCFS2_CONTROL_MESSAGE_SETVERSION_OP "SETV"
  92. #define OCFS2_CONTROL_MESSAGE_SETVERSION_TOTAL_LEN 11
  93. #define OCFS2_CONTROL_MESSAGE_DOWN_OP "DOWN"
  94. #define OCFS2_CONTROL_MESSAGE_DOWN_TOTAL_LEN 47
  95. #define OCFS2_TEXT_UUID_LEN 32
  96. #define OCFS2_CONTROL_MESSAGE_VERNUM_LEN 2
  97. #define OCFS2_CONTROL_MESSAGE_NODENUM_LEN 8
  98. /*
  99. * ocfs2_live_connection is refcounted because the filesystem and
  100. * miscdevice sides can detach in different order. Let's just be safe.
  101. */
  102. struct ocfs2_live_connection {
  103. struct list_head oc_list;
  104. struct ocfs2_cluster_connection *oc_conn;
  105. };
  106. struct ocfs2_control_private {
  107. struct list_head op_list;
  108. int op_state;
  109. int op_this_node;
  110. struct ocfs2_protocol_version op_proto;
  111. };
  112. /* SETN<space><8-char-hex-nodenum><newline> */
  113. struct ocfs2_control_message_setn {
  114. char tag[OCFS2_CONTROL_MESSAGE_OP_LEN];
  115. char space;
  116. char nodestr[OCFS2_CONTROL_MESSAGE_NODENUM_LEN];
  117. char newline;
  118. };
  119. /* SETV<space><2-char-hex-major><space><2-char-hex-minor><newline> */
  120. struct ocfs2_control_message_setv {
  121. char tag[OCFS2_CONTROL_MESSAGE_OP_LEN];
  122. char space1;
  123. char major[OCFS2_CONTROL_MESSAGE_VERNUM_LEN];
  124. char space2;
  125. char minor[OCFS2_CONTROL_MESSAGE_VERNUM_LEN];
  126. char newline;
  127. };
  128. /* DOWN<space><32-char-cap-hex-uuid><space><8-char-hex-nodenum><newline> */
  129. struct ocfs2_control_message_down {
  130. char tag[OCFS2_CONTROL_MESSAGE_OP_LEN];
  131. char space1;
  132. char uuid[OCFS2_TEXT_UUID_LEN];
  133. char space2;
  134. char nodestr[OCFS2_CONTROL_MESSAGE_NODENUM_LEN];
  135. char newline;
  136. };
  137. union ocfs2_control_message {
  138. char tag[OCFS2_CONTROL_MESSAGE_OP_LEN];
  139. struct ocfs2_control_message_setn u_setn;
  140. struct ocfs2_control_message_setv u_setv;
  141. struct ocfs2_control_message_down u_down;
  142. };
  143. static struct ocfs2_stack_plugin ocfs2_user_plugin;
  144. static atomic_t ocfs2_control_opened;
  145. static int ocfs2_control_this_node = -1;
  146. static struct ocfs2_protocol_version running_proto;
  147. static LIST_HEAD(ocfs2_live_connection_list);
  148. static LIST_HEAD(ocfs2_control_private_list);
  149. static DEFINE_MUTEX(ocfs2_control_lock);
  150. static inline void ocfs2_control_set_handshake_state(struct file *file,
  151. int state)
  152. {
  153. struct ocfs2_control_private *p = file->private_data;
  154. p->op_state = state;
  155. }
  156. static inline int ocfs2_control_get_handshake_state(struct file *file)
  157. {
  158. struct ocfs2_control_private *p = file->private_data;
  159. return p->op_state;
  160. }
  161. static struct ocfs2_live_connection *ocfs2_connection_find(const char *name)
  162. {
  163. size_t len = strlen(name);
  164. struct ocfs2_live_connection *c;
  165. BUG_ON(!mutex_is_locked(&ocfs2_control_lock));
  166. list_for_each_entry(c, &ocfs2_live_connection_list, oc_list) {
  167. if ((c->oc_conn->cc_namelen == len) &&
  168. !strncmp(c->oc_conn->cc_name, name, len))
  169. return c;
  170. }
  171. return NULL;
  172. }
  173. /*
  174. * ocfs2_live_connection structures are created underneath the ocfs2
  175. * mount path. Since the VFS prevents multiple calls to
  176. * fill_super(), we can't get dupes here.
  177. */
  178. static int ocfs2_live_connection_new(struct ocfs2_cluster_connection *conn,
  179. struct ocfs2_live_connection **c_ret)
  180. {
  181. int rc = 0;
  182. struct ocfs2_live_connection *c;
  183. c = kzalloc(sizeof(struct ocfs2_live_connection), GFP_KERNEL);
  184. if (!c)
  185. return -ENOMEM;
  186. mutex_lock(&ocfs2_control_lock);
  187. c->oc_conn = conn;
  188. if (atomic_read(&ocfs2_control_opened))
  189. list_add(&c->oc_list, &ocfs2_live_connection_list);
  190. else {
  191. printk(KERN_ERR
  192. "ocfs2: Userspace control daemon is not present\n");
  193. rc = -ESRCH;
  194. }
  195. mutex_unlock(&ocfs2_control_lock);
  196. if (!rc)
  197. *c_ret = c;
  198. else
  199. kfree(c);
  200. return rc;
  201. }
  202. /*
  203. * This function disconnects the cluster connection from ocfs2_control.
  204. * Afterwards, userspace can't affect the cluster connection.
  205. */
  206. static void ocfs2_live_connection_drop(struct ocfs2_live_connection *c)
  207. {
  208. mutex_lock(&ocfs2_control_lock);
  209. list_del_init(&c->oc_list);
  210. c->oc_conn = NULL;
  211. mutex_unlock(&ocfs2_control_lock);
  212. kfree(c);
  213. }
  214. static int ocfs2_control_cfu(void *target, size_t target_len,
  215. const char __user *buf, size_t count)
  216. {
  217. /* The T01 expects write(2) calls to have exactly one command */
  218. if ((count != target_len) ||
  219. (count > sizeof(union ocfs2_control_message)))
  220. return -EINVAL;
  221. if (copy_from_user(target, buf, target_len))
  222. return -EFAULT;
  223. return 0;
  224. }
  225. static ssize_t ocfs2_control_validate_protocol(struct file *file,
  226. const char __user *buf,
  227. size_t count)
  228. {
  229. ssize_t ret;
  230. char kbuf[OCFS2_CONTROL_PROTO_LEN];
  231. ret = ocfs2_control_cfu(kbuf, OCFS2_CONTROL_PROTO_LEN,
  232. buf, count);
  233. if (ret)
  234. return ret;
  235. if (strncmp(kbuf, OCFS2_CONTROL_PROTO, OCFS2_CONTROL_PROTO_LEN))
  236. return -EINVAL;
  237. ocfs2_control_set_handshake_state(file,
  238. OCFS2_CONTROL_HANDSHAKE_PROTOCOL);
  239. return count;
  240. }
  241. static void ocfs2_control_send_down(const char *uuid,
  242. int nodenum)
  243. {
  244. struct ocfs2_live_connection *c;
  245. mutex_lock(&ocfs2_control_lock);
  246. c = ocfs2_connection_find(uuid);
  247. if (c) {
  248. BUG_ON(c->oc_conn == NULL);
  249. c->oc_conn->cc_recovery_handler(nodenum,
  250. c->oc_conn->cc_recovery_data);
  251. }
  252. mutex_unlock(&ocfs2_control_lock);
  253. }
  254. /*
  255. * Called whenever configuration elements are sent to /dev/ocfs2_control.
  256. * If all configuration elements are present, try to set the global
  257. * values. If there is a problem, return an error. Skip any missing
  258. * elements, and only bump ocfs2_control_opened when we have all elements
  259. * and are successful.
  260. */
  261. static int ocfs2_control_install_private(struct file *file)
  262. {
  263. int rc = 0;
  264. int set_p = 1;
  265. struct ocfs2_control_private *p = file->private_data;
  266. BUG_ON(p->op_state != OCFS2_CONTROL_HANDSHAKE_PROTOCOL);
  267. mutex_lock(&ocfs2_control_lock);
  268. if (p->op_this_node < 0) {
  269. set_p = 0;
  270. } else if ((ocfs2_control_this_node >= 0) &&
  271. (ocfs2_control_this_node != p->op_this_node)) {
  272. rc = -EINVAL;
  273. goto out_unlock;
  274. }
  275. if (!p->op_proto.pv_major) {
  276. set_p = 0;
  277. } else if (!list_empty(&ocfs2_live_connection_list) &&
  278. ((running_proto.pv_major != p->op_proto.pv_major) ||
  279. (running_proto.pv_minor != p->op_proto.pv_minor))) {
  280. rc = -EINVAL;
  281. goto out_unlock;
  282. }
  283. if (set_p) {
  284. ocfs2_control_this_node = p->op_this_node;
  285. running_proto.pv_major = p->op_proto.pv_major;
  286. running_proto.pv_minor = p->op_proto.pv_minor;
  287. }
  288. out_unlock:
  289. mutex_unlock(&ocfs2_control_lock);
  290. if (!rc && set_p) {
  291. /* We set the global values successfully */
  292. atomic_inc(&ocfs2_control_opened);
  293. ocfs2_control_set_handshake_state(file,
  294. OCFS2_CONTROL_HANDSHAKE_VALID);
  295. }
  296. return rc;
  297. }
  298. static int ocfs2_control_get_this_node(void)
  299. {
  300. int rc;
  301. mutex_lock(&ocfs2_control_lock);
  302. if (ocfs2_control_this_node < 0)
  303. rc = -EINVAL;
  304. else
  305. rc = ocfs2_control_this_node;
  306. mutex_unlock(&ocfs2_control_lock);
  307. return rc;
  308. }
  309. static int ocfs2_control_do_setnode_msg(struct file *file,
  310. struct ocfs2_control_message_setn *msg)
  311. {
  312. long nodenum;
  313. char *ptr = NULL;
  314. struct ocfs2_control_private *p = file->private_data;
  315. if (ocfs2_control_get_handshake_state(file) !=
  316. OCFS2_CONTROL_HANDSHAKE_PROTOCOL)
  317. return -EINVAL;
  318. if (strncmp(msg->tag, OCFS2_CONTROL_MESSAGE_SETNODE_OP,
  319. OCFS2_CONTROL_MESSAGE_OP_LEN))
  320. return -EINVAL;
  321. if ((msg->space != ' ') || (msg->newline != '\n'))
  322. return -EINVAL;
  323. msg->space = msg->newline = '\0';
  324. nodenum = simple_strtol(msg->nodestr, &ptr, 16);
  325. if (!ptr || *ptr)
  326. return -EINVAL;
  327. if ((nodenum == LONG_MIN) || (nodenum == LONG_MAX) ||
  328. (nodenum > INT_MAX) || (nodenum < 0))
  329. return -ERANGE;
  330. p->op_this_node = nodenum;
  331. return ocfs2_control_install_private(file);
  332. }
  333. static int ocfs2_control_do_setversion_msg(struct file *file,
  334. struct ocfs2_control_message_setv *msg)
  335. {
  336. long major, minor;
  337. char *ptr = NULL;
  338. struct ocfs2_control_private *p = file->private_data;
  339. struct ocfs2_protocol_version *max =
  340. &ocfs2_user_plugin.sp_max_proto;
  341. if (ocfs2_control_get_handshake_state(file) !=
  342. OCFS2_CONTROL_HANDSHAKE_PROTOCOL)
  343. return -EINVAL;
  344. if (strncmp(msg->tag, OCFS2_CONTROL_MESSAGE_SETVERSION_OP,
  345. OCFS2_CONTROL_MESSAGE_OP_LEN))
  346. return -EINVAL;
  347. if ((msg->space1 != ' ') || (msg->space2 != ' ') ||
  348. (msg->newline != '\n'))
  349. return -EINVAL;
  350. msg->space1 = msg->space2 = msg->newline = '\0';
  351. major = simple_strtol(msg->major, &ptr, 16);
  352. if (!ptr || *ptr)
  353. return -EINVAL;
  354. minor = simple_strtol(msg->minor, &ptr, 16);
  355. if (!ptr || *ptr)
  356. return -EINVAL;
  357. /*
  358. * The major must be between 1 and 255, inclusive. The minor
  359. * must be between 0 and 255, inclusive. The version passed in
  360. * must be within the maximum version supported by the filesystem.
  361. */
  362. if ((major == LONG_MIN) || (major == LONG_MAX) ||
  363. (major > (u8)-1) || (major < 1))
  364. return -ERANGE;
  365. if ((minor == LONG_MIN) || (minor == LONG_MAX) ||
  366. (minor > (u8)-1) || (minor < 0))
  367. return -ERANGE;
  368. if ((major != max->pv_major) ||
  369. (minor > max->pv_minor))
  370. return -EINVAL;
  371. p->op_proto.pv_major = major;
  372. p->op_proto.pv_minor = minor;
  373. return ocfs2_control_install_private(file);
  374. }
  375. static int ocfs2_control_do_down_msg(struct file *file,
  376. struct ocfs2_control_message_down *msg)
  377. {
  378. long nodenum;
  379. char *p = NULL;
  380. if (ocfs2_control_get_handshake_state(file) !=
  381. OCFS2_CONTROL_HANDSHAKE_VALID)
  382. return -EINVAL;
  383. if (strncmp(msg->tag, OCFS2_CONTROL_MESSAGE_DOWN_OP,
  384. OCFS2_CONTROL_MESSAGE_OP_LEN))
  385. return -EINVAL;
  386. if ((msg->space1 != ' ') || (msg->space2 != ' ') ||
  387. (msg->newline != '\n'))
  388. return -EINVAL;
  389. msg->space1 = msg->space2 = msg->newline = '\0';
  390. nodenum = simple_strtol(msg->nodestr, &p, 16);
  391. if (!p || *p)
  392. return -EINVAL;
  393. if ((nodenum == LONG_MIN) || (nodenum == LONG_MAX) ||
  394. (nodenum > INT_MAX) || (nodenum < 0))
  395. return -ERANGE;
  396. ocfs2_control_send_down(msg->uuid, nodenum);
  397. return 0;
  398. }
  399. static ssize_t ocfs2_control_message(struct file *file,
  400. const char __user *buf,
  401. size_t count)
  402. {
  403. ssize_t ret;
  404. union ocfs2_control_message msg;
  405. /* Try to catch padding issues */
  406. WARN_ON(offsetof(struct ocfs2_control_message_down, uuid) !=
  407. (sizeof(msg.u_down.tag) + sizeof(msg.u_down.space1)));
  408. memset(&msg, 0, sizeof(union ocfs2_control_message));
  409. ret = ocfs2_control_cfu(&msg, count, buf, count);
  410. if (ret)
  411. goto out;
  412. if ((count == OCFS2_CONTROL_MESSAGE_SETNODE_TOTAL_LEN) &&
  413. !strncmp(msg.tag, OCFS2_CONTROL_MESSAGE_SETNODE_OP,
  414. OCFS2_CONTROL_MESSAGE_OP_LEN))
  415. ret = ocfs2_control_do_setnode_msg(file, &msg.u_setn);
  416. else if ((count == OCFS2_CONTROL_MESSAGE_SETVERSION_TOTAL_LEN) &&
  417. !strncmp(msg.tag, OCFS2_CONTROL_MESSAGE_SETVERSION_OP,
  418. OCFS2_CONTROL_MESSAGE_OP_LEN))
  419. ret = ocfs2_control_do_setversion_msg(file, &msg.u_setv);
  420. else if ((count == OCFS2_CONTROL_MESSAGE_DOWN_TOTAL_LEN) &&
  421. !strncmp(msg.tag, OCFS2_CONTROL_MESSAGE_DOWN_OP,
  422. OCFS2_CONTROL_MESSAGE_OP_LEN))
  423. ret = ocfs2_control_do_down_msg(file, &msg.u_down);
  424. else
  425. ret = -EINVAL;
  426. out:
  427. return ret ? ret : count;
  428. }
  429. static ssize_t ocfs2_control_write(struct file *file,
  430. const char __user *buf,
  431. size_t count,
  432. loff_t *ppos)
  433. {
  434. ssize_t ret;
  435. switch (ocfs2_control_get_handshake_state(file)) {
  436. case OCFS2_CONTROL_HANDSHAKE_INVALID:
  437. ret = -EINVAL;
  438. break;
  439. case OCFS2_CONTROL_HANDSHAKE_READ:
  440. ret = ocfs2_control_validate_protocol(file, buf,
  441. count);
  442. break;
  443. case OCFS2_CONTROL_HANDSHAKE_PROTOCOL:
  444. case OCFS2_CONTROL_HANDSHAKE_VALID:
  445. ret = ocfs2_control_message(file, buf, count);
  446. break;
  447. default:
  448. BUG();
  449. ret = -EIO;
  450. break;
  451. }
  452. return ret;
  453. }
  454. /*
  455. * This is a naive version. If we ever have a new protocol, we'll expand
  456. * it. Probably using seq_file.
  457. */
  458. static ssize_t ocfs2_control_read(struct file *file,
  459. char __user *buf,
  460. size_t count,
  461. loff_t *ppos)
  462. {
  463. ssize_t ret;
  464. ret = simple_read_from_buffer(buf, count, ppos,
  465. OCFS2_CONTROL_PROTO, OCFS2_CONTROL_PROTO_LEN);
  466. /* Have we read the whole protocol list? */
  467. if (ret > 0 && *ppos >= OCFS2_CONTROL_PROTO_LEN)
  468. ocfs2_control_set_handshake_state(file,
  469. OCFS2_CONTROL_HANDSHAKE_READ);
  470. return ret;
  471. }
  472. static int ocfs2_control_release(struct inode *inode, struct file *file)
  473. {
  474. struct ocfs2_control_private *p = file->private_data;
  475. mutex_lock(&ocfs2_control_lock);
  476. if (ocfs2_control_get_handshake_state(file) !=
  477. OCFS2_CONTROL_HANDSHAKE_VALID)
  478. goto out;
  479. if (atomic_dec_and_test(&ocfs2_control_opened)) {
  480. if (!list_empty(&ocfs2_live_connection_list)) {
  481. /* XXX: Do bad things! */
  482. printk(KERN_ERR
  483. "ocfs2: Unexpected release of ocfs2_control!\n"
  484. " Loss of cluster connection requires "
  485. "an emergency restart!\n");
  486. emergency_restart();
  487. }
  488. /*
  489. * Last valid close clears the node number and resets
  490. * the locking protocol version
  491. */
  492. ocfs2_control_this_node = -1;
  493. running_proto.pv_major = 0;
  494. running_proto.pv_major = 0;
  495. }
  496. out:
  497. list_del_init(&p->op_list);
  498. file->private_data = NULL;
  499. mutex_unlock(&ocfs2_control_lock);
  500. kfree(p);
  501. return 0;
  502. }
  503. static int ocfs2_control_open(struct inode *inode, struct file *file)
  504. {
  505. struct ocfs2_control_private *p;
  506. p = kzalloc(sizeof(struct ocfs2_control_private), GFP_KERNEL);
  507. if (!p)
  508. return -ENOMEM;
  509. p->op_this_node = -1;
  510. mutex_lock(&ocfs2_control_lock);
  511. file->private_data = p;
  512. list_add(&p->op_list, &ocfs2_control_private_list);
  513. mutex_unlock(&ocfs2_control_lock);
  514. return 0;
  515. }
  516. static const struct file_operations ocfs2_control_fops = {
  517. .open = ocfs2_control_open,
  518. .release = ocfs2_control_release,
  519. .read = ocfs2_control_read,
  520. .write = ocfs2_control_write,
  521. .owner = THIS_MODULE,
  522. .llseek = default_llseek,
  523. };
  524. static struct miscdevice ocfs2_control_device = {
  525. .minor = MISC_DYNAMIC_MINOR,
  526. .name = "ocfs2_control",
  527. .fops = &ocfs2_control_fops,
  528. };
  529. static int ocfs2_control_init(void)
  530. {
  531. int rc;
  532. atomic_set(&ocfs2_control_opened, 0);
  533. rc = misc_register(&ocfs2_control_device);
  534. if (rc)
  535. printk(KERN_ERR
  536. "ocfs2: Unable to register ocfs2_control device "
  537. "(errno %d)\n",
  538. -rc);
  539. return rc;
  540. }
  541. static void ocfs2_control_exit(void)
  542. {
  543. int rc;
  544. rc = misc_deregister(&ocfs2_control_device);
  545. if (rc)
  546. printk(KERN_ERR
  547. "ocfs2: Unable to deregister ocfs2_control device "
  548. "(errno %d)\n",
  549. -rc);
  550. }
  551. static void fsdlm_lock_ast_wrapper(void *astarg)
  552. {
  553. struct ocfs2_dlm_lksb *lksb = astarg;
  554. int status = lksb->lksb_fsdlm.sb_status;
  555. /*
  556. * For now we're punting on the issue of other non-standard errors
  557. * where we can't tell if the unlock_ast or lock_ast should be called.
  558. * The main "other error" that's possible is EINVAL which means the
  559. * function was called with invalid args, which shouldn't be possible
  560. * since the caller here is under our control. Other non-standard
  561. * errors probably fall into the same category, or otherwise are fatal
  562. * which means we can't carry on anyway.
  563. */
  564. if (status == -DLM_EUNLOCK || status == -DLM_ECANCEL)
  565. lksb->lksb_conn->cc_proto->lp_unlock_ast(lksb, 0);
  566. else
  567. lksb->lksb_conn->cc_proto->lp_lock_ast(lksb);
  568. }
  569. static void fsdlm_blocking_ast_wrapper(void *astarg, int level)
  570. {
  571. struct ocfs2_dlm_lksb *lksb = astarg;
  572. lksb->lksb_conn->cc_proto->lp_blocking_ast(lksb, level);
  573. }
  574. static int user_dlm_lock(struct ocfs2_cluster_connection *conn,
  575. int mode,
  576. struct ocfs2_dlm_lksb *lksb,
  577. u32 flags,
  578. void *name,
  579. unsigned int namelen)
  580. {
  581. int ret;
  582. if (!lksb->lksb_fsdlm.sb_lvbptr)
  583. lksb->lksb_fsdlm.sb_lvbptr = (char *)lksb +
  584. sizeof(struct dlm_lksb);
  585. ret = dlm_lock(conn->cc_lockspace, mode, &lksb->lksb_fsdlm,
  586. flags|DLM_LKF_NODLCKWT, name, namelen, 0,
  587. fsdlm_lock_ast_wrapper, lksb,
  588. fsdlm_blocking_ast_wrapper);
  589. return ret;
  590. }
  591. static int user_dlm_unlock(struct ocfs2_cluster_connection *conn,
  592. struct ocfs2_dlm_lksb *lksb,
  593. u32 flags)
  594. {
  595. int ret;
  596. ret = dlm_unlock(conn->cc_lockspace, lksb->lksb_fsdlm.sb_lkid,
  597. flags, &lksb->lksb_fsdlm, lksb);
  598. return ret;
  599. }
  600. static int user_dlm_lock_status(struct ocfs2_dlm_lksb *lksb)
  601. {
  602. return lksb->lksb_fsdlm.sb_status;
  603. }
  604. static int user_dlm_lvb_valid(struct ocfs2_dlm_lksb *lksb)
  605. {
  606. int invalid = lksb->lksb_fsdlm.sb_flags & DLM_SBF_VALNOTVALID;
  607. return !invalid;
  608. }
  609. static void *user_dlm_lvb(struct ocfs2_dlm_lksb *lksb)
  610. {
  611. if (!lksb->lksb_fsdlm.sb_lvbptr)
  612. lksb->lksb_fsdlm.sb_lvbptr = (char *)lksb +
  613. sizeof(struct dlm_lksb);
  614. return (void *)(lksb->lksb_fsdlm.sb_lvbptr);
  615. }
  616. static void user_dlm_dump_lksb(struct ocfs2_dlm_lksb *lksb)
  617. {
  618. }
  619. static int user_plock(struct ocfs2_cluster_connection *conn,
  620. u64 ino,
  621. struct file *file,
  622. int cmd,
  623. struct file_lock *fl)
  624. {
  625. /*
  626. * This more or less just demuxes the plock request into any
  627. * one of three dlm calls.
  628. *
  629. * Internally, fs/dlm will pass these to a misc device, which
  630. * a userspace daemon will read and write to.
  631. *
  632. * For now, cancel requests (which happen internally only),
  633. * are turned into unlocks. Most of this function taken from
  634. * gfs2_lock.
  635. */
  636. if (cmd == F_CANCELLK) {
  637. cmd = F_SETLK;
  638. fl->fl_type = F_UNLCK;
  639. }
  640. if (IS_GETLK(cmd))
  641. return dlm_posix_get(conn->cc_lockspace, ino, file, fl);
  642. else if (fl->fl_type == F_UNLCK)
  643. return dlm_posix_unlock(conn->cc_lockspace, ino, file, fl);
  644. else
  645. return dlm_posix_lock(conn->cc_lockspace, ino, file, cmd, fl);
  646. }
  647. /*
  648. * Compare a requested locking protocol version against the current one.
  649. *
  650. * If the major numbers are different, they are incompatible.
  651. * If the current minor is greater than the request, they are incompatible.
  652. * If the current minor is less than or equal to the request, they are
  653. * compatible, and the requester should run at the current minor version.
  654. */
  655. static int fs_protocol_compare(struct ocfs2_protocol_version *existing,
  656. struct ocfs2_protocol_version *request)
  657. {
  658. if (existing->pv_major != request->pv_major)
  659. return 1;
  660. if (existing->pv_minor > request->pv_minor)
  661. return 1;
  662. if (existing->pv_minor < request->pv_minor)
  663. request->pv_minor = existing->pv_minor;
  664. return 0;
  665. }
  666. static int user_cluster_connect(struct ocfs2_cluster_connection *conn)
  667. {
  668. dlm_lockspace_t *fsdlm;
  669. struct ocfs2_live_connection *uninitialized_var(control);
  670. int rc = 0;
  671. BUG_ON(conn == NULL);
  672. rc = ocfs2_live_connection_new(conn, &control);
  673. if (rc)
  674. goto out;
  675. /*
  676. * running_proto must have been set before we allowed any mounts
  677. * to proceed.
  678. */
  679. if (fs_protocol_compare(&running_proto, &conn->cc_version)) {
  680. printk(KERN_ERR
  681. "Unable to mount with fs locking protocol version "
  682. "%u.%u because the userspace control daemon has "
  683. "negotiated %u.%u\n",
  684. conn->cc_version.pv_major, conn->cc_version.pv_minor,
  685. running_proto.pv_major, running_proto.pv_minor);
  686. rc = -EPROTO;
  687. ocfs2_live_connection_drop(control);
  688. goto out;
  689. }
  690. rc = dlm_new_lockspace(conn->cc_name, NULL, DLM_LSFL_FS, DLM_LVB_LEN,
  691. NULL, NULL, NULL, &fsdlm);
  692. if (rc) {
  693. ocfs2_live_connection_drop(control);
  694. goto out;
  695. }
  696. conn->cc_private = control;
  697. conn->cc_lockspace = fsdlm;
  698. out:
  699. return rc;
  700. }
  701. static int user_cluster_disconnect(struct ocfs2_cluster_connection *conn)
  702. {
  703. dlm_release_lockspace(conn->cc_lockspace, 2);
  704. conn->cc_lockspace = NULL;
  705. ocfs2_live_connection_drop(conn->cc_private);
  706. conn->cc_private = NULL;
  707. return 0;
  708. }
  709. static int user_cluster_this_node(unsigned int *this_node)
  710. {
  711. int rc;
  712. rc = ocfs2_control_get_this_node();
  713. if (rc < 0)
  714. return rc;
  715. *this_node = rc;
  716. return 0;
  717. }
  718. static struct ocfs2_stack_operations ocfs2_user_plugin_ops = {
  719. .connect = user_cluster_connect,
  720. .disconnect = user_cluster_disconnect,
  721. .this_node = user_cluster_this_node,
  722. .dlm_lock = user_dlm_lock,
  723. .dlm_unlock = user_dlm_unlock,
  724. .lock_status = user_dlm_lock_status,
  725. .lvb_valid = user_dlm_lvb_valid,
  726. .lock_lvb = user_dlm_lvb,
  727. .plock = user_plock,
  728. .dump_lksb = user_dlm_dump_lksb,
  729. };
  730. static struct ocfs2_stack_plugin ocfs2_user_plugin = {
  731. .sp_name = "user",
  732. .sp_ops = &ocfs2_user_plugin_ops,
  733. .sp_owner = THIS_MODULE,
  734. };
  735. static int __init ocfs2_user_plugin_init(void)
  736. {
  737. int rc;
  738. rc = ocfs2_control_init();
  739. if (!rc) {
  740. rc = ocfs2_stack_glue_register(&ocfs2_user_plugin);
  741. if (rc)
  742. ocfs2_control_exit();
  743. }
  744. return rc;
  745. }
  746. static void __exit ocfs2_user_plugin_exit(void)
  747. {
  748. ocfs2_stack_glue_unregister(&ocfs2_user_plugin);
  749. ocfs2_control_exit();
  750. }
  751. MODULE_AUTHOR("Oracle");
  752. MODULE_DESCRIPTION("ocfs2 driver for userspace cluster stacks");
  753. MODULE_LICENSE("GPL");
  754. module_init(ocfs2_user_plugin_init);
  755. module_exit(ocfs2_user_plugin_exit);