stack_user.c 22 KB

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