stack_user.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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/smp_lock.h>
  24. #include <linux/reboot.h>
  25. #include <asm/uaccess.h>
  26. #include "ocfs2.h" /* For struct ocfs2_lock_res */
  27. #include "stackglue.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_proto->lp_max_version.pv_major, and the minor number
  62. * must be less than or equal to ...->lp_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 c;
  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_proto->lp_max_version;
  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. char *proto_string = OCFS2_CONTROL_PROTO;
  464. size_t to_write = 0;
  465. if (*ppos >= OCFS2_CONTROL_PROTO_LEN)
  466. return 0;
  467. to_write = OCFS2_CONTROL_PROTO_LEN - *ppos;
  468. if (to_write > count)
  469. to_write = count;
  470. if (copy_to_user(buf, proto_string + *ppos, to_write))
  471. return -EFAULT;
  472. *ppos += to_write;
  473. /* Have we read the whole protocol list? */
  474. if (*ppos >= OCFS2_CONTROL_PROTO_LEN)
  475. ocfs2_control_set_handshake_state(file,
  476. OCFS2_CONTROL_HANDSHAKE_READ);
  477. return to_write;
  478. }
  479. static int ocfs2_control_release(struct inode *inode, struct file *file)
  480. {
  481. struct ocfs2_control_private *p = file->private_data;
  482. mutex_lock(&ocfs2_control_lock);
  483. if (ocfs2_control_get_handshake_state(file) !=
  484. OCFS2_CONTROL_HANDSHAKE_VALID)
  485. goto out;
  486. if (atomic_dec_and_test(&ocfs2_control_opened)) {
  487. if (!list_empty(&ocfs2_live_connection_list)) {
  488. /* XXX: Do bad things! */
  489. printk(KERN_ERR
  490. "ocfs2: Unexpected release of ocfs2_control!\n"
  491. " Loss of cluster connection requires "
  492. "an emergency restart!\n");
  493. emergency_restart();
  494. }
  495. /*
  496. * Last valid close clears the node number and resets
  497. * the locking protocol version
  498. */
  499. ocfs2_control_this_node = -1;
  500. running_proto.pv_major = 0;
  501. running_proto.pv_major = 0;
  502. }
  503. out:
  504. list_del_init(&p->op_list);
  505. file->private_data = NULL;
  506. mutex_unlock(&ocfs2_control_lock);
  507. kfree(p);
  508. return 0;
  509. }
  510. static int ocfs2_control_open(struct inode *inode, struct file *file)
  511. {
  512. struct ocfs2_control_private *p;
  513. p = kzalloc(sizeof(struct ocfs2_control_private), GFP_KERNEL);
  514. if (!p)
  515. return -ENOMEM;
  516. p->op_this_node = -1;
  517. lock_kernel();
  518. mutex_lock(&ocfs2_control_lock);
  519. file->private_data = p;
  520. list_add(&p->op_list, &ocfs2_control_private_list);
  521. mutex_unlock(&ocfs2_control_lock);
  522. unlock_kernel();
  523. return 0;
  524. }
  525. static const struct file_operations ocfs2_control_fops = {
  526. .open = ocfs2_control_open,
  527. .release = ocfs2_control_release,
  528. .read = ocfs2_control_read,
  529. .write = ocfs2_control_write,
  530. .owner = THIS_MODULE,
  531. };
  532. static struct miscdevice ocfs2_control_device = {
  533. .minor = MISC_DYNAMIC_MINOR,
  534. .name = "ocfs2_control",
  535. .fops = &ocfs2_control_fops,
  536. };
  537. static int ocfs2_control_init(void)
  538. {
  539. int rc;
  540. atomic_set(&ocfs2_control_opened, 0);
  541. rc = misc_register(&ocfs2_control_device);
  542. if (rc)
  543. printk(KERN_ERR
  544. "ocfs2: Unable to register ocfs2_control device "
  545. "(errno %d)\n",
  546. -rc);
  547. return rc;
  548. }
  549. static void ocfs2_control_exit(void)
  550. {
  551. int rc;
  552. rc = misc_deregister(&ocfs2_control_device);
  553. if (rc)
  554. printk(KERN_ERR
  555. "ocfs2: Unable to deregister ocfs2_control device "
  556. "(errno %d)\n",
  557. -rc);
  558. }
  559. static struct dlm_lksb *fsdlm_astarg_to_lksb(void *astarg)
  560. {
  561. struct ocfs2_lock_res *res = astarg;
  562. return &res->l_lksb.lksb_fsdlm;
  563. }
  564. static void fsdlm_lock_ast_wrapper(void *astarg)
  565. {
  566. struct dlm_lksb *lksb = fsdlm_astarg_to_lksb(astarg);
  567. int status = lksb->sb_status;
  568. BUG_ON(ocfs2_user_plugin.sp_proto == NULL);
  569. /*
  570. * For now we're punting on the issue of other non-standard errors
  571. * where we can't tell if the unlock_ast or lock_ast should be called.
  572. * The main "other error" that's possible is EINVAL which means the
  573. * function was called with invalid args, which shouldn't be possible
  574. * since the caller here is under our control. Other non-standard
  575. * errors probably fall into the same category, or otherwise are fatal
  576. * which means we can't carry on anyway.
  577. */
  578. if (status == -DLM_EUNLOCK || status == -DLM_ECANCEL)
  579. ocfs2_user_plugin.sp_proto->lp_unlock_ast(astarg, 0);
  580. else
  581. ocfs2_user_plugin.sp_proto->lp_lock_ast(astarg);
  582. }
  583. static void fsdlm_blocking_ast_wrapper(void *astarg, int level)
  584. {
  585. BUG_ON(ocfs2_user_plugin.sp_proto == NULL);
  586. ocfs2_user_plugin.sp_proto->lp_blocking_ast(astarg, level);
  587. }
  588. static int user_dlm_lock(struct ocfs2_cluster_connection *conn,
  589. int mode,
  590. union ocfs2_dlm_lksb *lksb,
  591. u32 flags,
  592. void *name,
  593. unsigned int namelen,
  594. void *astarg)
  595. {
  596. int ret;
  597. if (!lksb->lksb_fsdlm.sb_lvbptr)
  598. lksb->lksb_fsdlm.sb_lvbptr = (char *)lksb +
  599. sizeof(struct dlm_lksb);
  600. ret = dlm_lock(conn->cc_lockspace, mode, &lksb->lksb_fsdlm,
  601. flags|DLM_LKF_NODLCKWT, name, namelen, 0,
  602. fsdlm_lock_ast_wrapper, astarg,
  603. fsdlm_blocking_ast_wrapper);
  604. return ret;
  605. }
  606. static int user_dlm_unlock(struct ocfs2_cluster_connection *conn,
  607. union ocfs2_dlm_lksb *lksb,
  608. u32 flags,
  609. void *astarg)
  610. {
  611. int ret;
  612. ret = dlm_unlock(conn->cc_lockspace, lksb->lksb_fsdlm.sb_lkid,
  613. flags, &lksb->lksb_fsdlm, astarg);
  614. return ret;
  615. }
  616. static int user_dlm_lock_status(union ocfs2_dlm_lksb *lksb)
  617. {
  618. return lksb->lksb_fsdlm.sb_status;
  619. }
  620. static void *user_dlm_lvb(union ocfs2_dlm_lksb *lksb)
  621. {
  622. return (void *)(lksb->lksb_fsdlm.sb_lvbptr);
  623. }
  624. static void user_dlm_dump_lksb(union ocfs2_dlm_lksb *lksb)
  625. {
  626. }
  627. /*
  628. * Compare a requested locking protocol version against the current one.
  629. *
  630. * If the major numbers are different, they are incompatible.
  631. * If the current minor is greater than the request, they are incompatible.
  632. * If the current minor is less than or equal to the request, they are
  633. * compatible, and the requester should run at the current minor version.
  634. */
  635. static int fs_protocol_compare(struct ocfs2_protocol_version *existing,
  636. struct ocfs2_protocol_version *request)
  637. {
  638. if (existing->pv_major != request->pv_major)
  639. return 1;
  640. if (existing->pv_minor > request->pv_minor)
  641. return 1;
  642. if (existing->pv_minor < request->pv_minor)
  643. request->pv_minor = existing->pv_minor;
  644. return 0;
  645. }
  646. static int user_cluster_connect(struct ocfs2_cluster_connection *conn)
  647. {
  648. dlm_lockspace_t *fsdlm;
  649. struct ocfs2_live_connection *control;
  650. int rc = 0;
  651. BUG_ON(conn == NULL);
  652. rc = ocfs2_live_connection_new(conn, &control);
  653. if (rc)
  654. goto out;
  655. /*
  656. * running_proto must have been set before we allowed any mounts
  657. * to proceed.
  658. */
  659. if (fs_protocol_compare(&running_proto, &conn->cc_version)) {
  660. printk(KERN_ERR
  661. "Unable to mount with fs locking protocol version "
  662. "%u.%u because the userspace control daemon has "
  663. "negotiated %u.%u\n",
  664. conn->cc_version.pv_major, conn->cc_version.pv_minor,
  665. running_proto.pv_major, running_proto.pv_minor);
  666. rc = -EPROTO;
  667. ocfs2_live_connection_drop(control);
  668. goto out;
  669. }
  670. rc = dlm_new_lockspace(conn->cc_name, strlen(conn->cc_name),
  671. &fsdlm, DLM_LSFL_FS, DLM_LVB_LEN);
  672. if (rc) {
  673. ocfs2_live_connection_drop(control);
  674. goto out;
  675. }
  676. conn->cc_private = control;
  677. conn->cc_lockspace = fsdlm;
  678. out:
  679. return rc;
  680. }
  681. static int user_cluster_disconnect(struct ocfs2_cluster_connection *conn)
  682. {
  683. dlm_release_lockspace(conn->cc_lockspace, 2);
  684. conn->cc_lockspace = NULL;
  685. ocfs2_live_connection_drop(conn->cc_private);
  686. conn->cc_private = NULL;
  687. return 0;
  688. }
  689. static int user_cluster_this_node(unsigned int *this_node)
  690. {
  691. int rc;
  692. rc = ocfs2_control_get_this_node();
  693. if (rc < 0)
  694. return rc;
  695. *this_node = rc;
  696. return 0;
  697. }
  698. static struct ocfs2_stack_operations ocfs2_user_plugin_ops = {
  699. .connect = user_cluster_connect,
  700. .disconnect = user_cluster_disconnect,
  701. .this_node = user_cluster_this_node,
  702. .dlm_lock = user_dlm_lock,
  703. .dlm_unlock = user_dlm_unlock,
  704. .lock_status = user_dlm_lock_status,
  705. .lock_lvb = user_dlm_lvb,
  706. .dump_lksb = user_dlm_dump_lksb,
  707. };
  708. static struct ocfs2_stack_plugin ocfs2_user_plugin = {
  709. .sp_name = "user",
  710. .sp_ops = &ocfs2_user_plugin_ops,
  711. .sp_owner = THIS_MODULE,
  712. };
  713. static int __init ocfs2_user_plugin_init(void)
  714. {
  715. int rc;
  716. rc = ocfs2_control_init();
  717. if (!rc) {
  718. rc = ocfs2_stack_glue_register(&ocfs2_user_plugin);
  719. if (rc)
  720. ocfs2_control_exit();
  721. }
  722. return rc;
  723. }
  724. static void __exit ocfs2_user_plugin_exit(void)
  725. {
  726. ocfs2_stack_glue_unregister(&ocfs2_user_plugin);
  727. ocfs2_control_exit();
  728. }
  729. MODULE_AUTHOR("Oracle");
  730. MODULE_DESCRIPTION("ocfs2 driver for userspace cluster stacks");
  731. MODULE_LICENSE("GPL");
  732. module_init(ocfs2_user_plugin_init);
  733. module_exit(ocfs2_user_plugin_exit);