stack_user.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  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 <linux/sched.h>
  26. #include <asm/uaccess.h>
  27. #include "stackglue.h"
  28. #include <linux/dlm_plock.h>
  29. /*
  30. * The control protocol starts with a handshake. Until the handshake
  31. * is complete, the control device will fail all write(2)s.
  32. *
  33. * The handshake is simple. First, the client reads until EOF. Each line
  34. * of output is a supported protocol tag. All protocol tags are a single
  35. * character followed by a two hex digit version number. Currently the
  36. * only things supported is T01, for "Text-base version 0x01". Next, the
  37. * client writes the version they would like to use, including the newline.
  38. * Thus, the protocol tag is 'T01\n'. If the version tag written is
  39. * unknown, -EINVAL is returned. Once the negotiation is complete, the
  40. * client can start sending messages.
  41. *
  42. * The T01 protocol has three messages. First is the "SETN" message.
  43. * It has the following syntax:
  44. *
  45. * SETN<space><8-char-hex-nodenum><newline>
  46. *
  47. * This is 14 characters.
  48. *
  49. * The "SETN" message must be the first message following the protocol.
  50. * It tells ocfs2_control the local node number.
  51. *
  52. * Next comes the "SETV" message. It has the following syntax:
  53. *
  54. * SETV<space><2-char-hex-major><space><2-char-hex-minor><newline>
  55. *
  56. * This is 11 characters.
  57. *
  58. * The "SETV" message sets the filesystem locking protocol version as
  59. * negotiated by the client. The client negotiates based on the maximum
  60. * version advertised in /sys/fs/ocfs2/max_locking_protocol. The major
  61. * number from the "SETV" message must match
  62. * ocfs2_user_plugin.sp_max_proto.pv_major, and the minor number
  63. * must be less than or equal to ...sp_max_version.pv_minor.
  64. *
  65. * Once this information has been set, mounts will be allowed. From this
  66. * point on, the "DOWN" message can be sent for node down notification.
  67. * It has the following syntax:
  68. *
  69. * DOWN<space><32-char-cap-hex-uuid><space><8-char-hex-nodenum><newline>
  70. *
  71. * eg:
  72. *
  73. * DOWN 632A924FDD844190BDA93C0DF6B94899 00000001\n
  74. *
  75. * This is 47 characters.
  76. */
  77. /*
  78. * Whether or not the client has done the handshake.
  79. * For now, we have just one protocol version.
  80. */
  81. #define OCFS2_CONTROL_PROTO "T01\n"
  82. #define OCFS2_CONTROL_PROTO_LEN 4
  83. /* Handshake states */
  84. #define OCFS2_CONTROL_HANDSHAKE_INVALID (0)
  85. #define OCFS2_CONTROL_HANDSHAKE_READ (1)
  86. #define OCFS2_CONTROL_HANDSHAKE_PROTOCOL (2)
  87. #define OCFS2_CONTROL_HANDSHAKE_VALID (3)
  88. /* Messages */
  89. #define OCFS2_CONTROL_MESSAGE_OP_LEN 4
  90. #define OCFS2_CONTROL_MESSAGE_SETNODE_OP "SETN"
  91. #define OCFS2_CONTROL_MESSAGE_SETNODE_TOTAL_LEN 14
  92. #define OCFS2_CONTROL_MESSAGE_SETVERSION_OP "SETV"
  93. #define OCFS2_CONTROL_MESSAGE_SETVERSION_TOTAL_LEN 11
  94. #define OCFS2_CONTROL_MESSAGE_DOWN_OP "DOWN"
  95. #define OCFS2_CONTROL_MESSAGE_DOWN_TOTAL_LEN 47
  96. #define OCFS2_TEXT_UUID_LEN 32
  97. #define OCFS2_CONTROL_MESSAGE_VERNUM_LEN 2
  98. #define OCFS2_CONTROL_MESSAGE_NODENUM_LEN 8
  99. #define VERSION_LOCK "version_lock"
  100. enum ocfs2_connection_type {
  101. WITH_CONTROLD,
  102. NO_CONTROLD
  103. };
  104. /*
  105. * ocfs2_live_connection is refcounted because the filesystem and
  106. * miscdevice sides can detach in different order. Let's just be safe.
  107. */
  108. struct ocfs2_live_connection {
  109. struct list_head oc_list;
  110. struct ocfs2_cluster_connection *oc_conn;
  111. enum ocfs2_connection_type oc_type;
  112. atomic_t oc_this_node;
  113. int oc_our_slot;
  114. struct dlm_lksb oc_version_lksb;
  115. char oc_lvb[DLM_LVB_LEN];
  116. struct completion oc_sync_wait;
  117. wait_queue_head_t oc_wait;
  118. };
  119. struct ocfs2_control_private {
  120. struct list_head op_list;
  121. int op_state;
  122. int op_this_node;
  123. struct ocfs2_protocol_version op_proto;
  124. };
  125. /* SETN<space><8-char-hex-nodenum><newline> */
  126. struct ocfs2_control_message_setn {
  127. char tag[OCFS2_CONTROL_MESSAGE_OP_LEN];
  128. char space;
  129. char nodestr[OCFS2_CONTROL_MESSAGE_NODENUM_LEN];
  130. char newline;
  131. };
  132. /* SETV<space><2-char-hex-major><space><2-char-hex-minor><newline> */
  133. struct ocfs2_control_message_setv {
  134. char tag[OCFS2_CONTROL_MESSAGE_OP_LEN];
  135. char space1;
  136. char major[OCFS2_CONTROL_MESSAGE_VERNUM_LEN];
  137. char space2;
  138. char minor[OCFS2_CONTROL_MESSAGE_VERNUM_LEN];
  139. char newline;
  140. };
  141. /* DOWN<space><32-char-cap-hex-uuid><space><8-char-hex-nodenum><newline> */
  142. struct ocfs2_control_message_down {
  143. char tag[OCFS2_CONTROL_MESSAGE_OP_LEN];
  144. char space1;
  145. char uuid[OCFS2_TEXT_UUID_LEN];
  146. char space2;
  147. char nodestr[OCFS2_CONTROL_MESSAGE_NODENUM_LEN];
  148. char newline;
  149. };
  150. union ocfs2_control_message {
  151. char tag[OCFS2_CONTROL_MESSAGE_OP_LEN];
  152. struct ocfs2_control_message_setn u_setn;
  153. struct ocfs2_control_message_setv u_setv;
  154. struct ocfs2_control_message_down u_down;
  155. };
  156. static struct ocfs2_stack_plugin ocfs2_user_plugin;
  157. static atomic_t ocfs2_control_opened;
  158. static int ocfs2_control_this_node = -1;
  159. static struct ocfs2_protocol_version running_proto;
  160. static LIST_HEAD(ocfs2_live_connection_list);
  161. static LIST_HEAD(ocfs2_control_private_list);
  162. static DEFINE_MUTEX(ocfs2_control_lock);
  163. static inline void ocfs2_control_set_handshake_state(struct file *file,
  164. int state)
  165. {
  166. struct ocfs2_control_private *p = file->private_data;
  167. p->op_state = state;
  168. }
  169. static inline int ocfs2_control_get_handshake_state(struct file *file)
  170. {
  171. struct ocfs2_control_private *p = file->private_data;
  172. return p->op_state;
  173. }
  174. static struct ocfs2_live_connection *ocfs2_connection_find(const char *name)
  175. {
  176. size_t len = strlen(name);
  177. struct ocfs2_live_connection *c;
  178. BUG_ON(!mutex_is_locked(&ocfs2_control_lock));
  179. list_for_each_entry(c, &ocfs2_live_connection_list, oc_list) {
  180. if ((c->oc_conn->cc_namelen == len) &&
  181. !strncmp(c->oc_conn->cc_name, name, len))
  182. return c;
  183. }
  184. return NULL;
  185. }
  186. /*
  187. * ocfs2_live_connection structures are created underneath the ocfs2
  188. * mount path. Since the VFS prevents multiple calls to
  189. * fill_super(), we can't get dupes here.
  190. */
  191. static int ocfs2_live_connection_attach(struct ocfs2_cluster_connection *conn,
  192. struct ocfs2_live_connection *c)
  193. {
  194. int rc = 0;
  195. mutex_lock(&ocfs2_control_lock);
  196. c->oc_conn = conn;
  197. if ((c->oc_type == NO_CONTROLD) || atomic_read(&ocfs2_control_opened))
  198. list_add(&c->oc_list, &ocfs2_live_connection_list);
  199. else {
  200. printk(KERN_ERR
  201. "ocfs2: Userspace control daemon is not present\n");
  202. rc = -ESRCH;
  203. }
  204. mutex_unlock(&ocfs2_control_lock);
  205. return rc;
  206. }
  207. /*
  208. * This function disconnects the cluster connection from ocfs2_control.
  209. * Afterwards, userspace can't affect the cluster connection.
  210. */
  211. static void ocfs2_live_connection_drop(struct ocfs2_live_connection *c)
  212. {
  213. mutex_lock(&ocfs2_control_lock);
  214. list_del_init(&c->oc_list);
  215. c->oc_conn = NULL;
  216. mutex_unlock(&ocfs2_control_lock);
  217. kfree(c);
  218. }
  219. static int ocfs2_control_cfu(void *target, size_t target_len,
  220. const char __user *buf, size_t count)
  221. {
  222. /* The T01 expects write(2) calls to have exactly one command */
  223. if ((count != target_len) ||
  224. (count > sizeof(union ocfs2_control_message)))
  225. return -EINVAL;
  226. if (copy_from_user(target, buf, target_len))
  227. return -EFAULT;
  228. return 0;
  229. }
  230. static ssize_t ocfs2_control_validate_protocol(struct file *file,
  231. const char __user *buf,
  232. size_t count)
  233. {
  234. ssize_t ret;
  235. char kbuf[OCFS2_CONTROL_PROTO_LEN];
  236. ret = ocfs2_control_cfu(kbuf, OCFS2_CONTROL_PROTO_LEN,
  237. buf, count);
  238. if (ret)
  239. return ret;
  240. if (strncmp(kbuf, OCFS2_CONTROL_PROTO, OCFS2_CONTROL_PROTO_LEN))
  241. return -EINVAL;
  242. ocfs2_control_set_handshake_state(file,
  243. OCFS2_CONTROL_HANDSHAKE_PROTOCOL);
  244. return count;
  245. }
  246. static void ocfs2_control_send_down(const char *uuid,
  247. int nodenum)
  248. {
  249. struct ocfs2_live_connection *c;
  250. mutex_lock(&ocfs2_control_lock);
  251. c = ocfs2_connection_find(uuid);
  252. if (c) {
  253. BUG_ON(c->oc_conn == NULL);
  254. c->oc_conn->cc_recovery_handler(nodenum,
  255. c->oc_conn->cc_recovery_data);
  256. }
  257. mutex_unlock(&ocfs2_control_lock);
  258. }
  259. /*
  260. * Called whenever configuration elements are sent to /dev/ocfs2_control.
  261. * If all configuration elements are present, try to set the global
  262. * values. If there is a problem, return an error. Skip any missing
  263. * elements, and only bump ocfs2_control_opened when we have all elements
  264. * and are successful.
  265. */
  266. static int ocfs2_control_install_private(struct file *file)
  267. {
  268. int rc = 0;
  269. int set_p = 1;
  270. struct ocfs2_control_private *p = file->private_data;
  271. BUG_ON(p->op_state != OCFS2_CONTROL_HANDSHAKE_PROTOCOL);
  272. mutex_lock(&ocfs2_control_lock);
  273. if (p->op_this_node < 0) {
  274. set_p = 0;
  275. } else if ((ocfs2_control_this_node >= 0) &&
  276. (ocfs2_control_this_node != p->op_this_node)) {
  277. rc = -EINVAL;
  278. goto out_unlock;
  279. }
  280. if (!p->op_proto.pv_major) {
  281. set_p = 0;
  282. } else if (!list_empty(&ocfs2_live_connection_list) &&
  283. ((running_proto.pv_major != p->op_proto.pv_major) ||
  284. (running_proto.pv_minor != p->op_proto.pv_minor))) {
  285. rc = -EINVAL;
  286. goto out_unlock;
  287. }
  288. if (set_p) {
  289. ocfs2_control_this_node = p->op_this_node;
  290. running_proto.pv_major = p->op_proto.pv_major;
  291. running_proto.pv_minor = p->op_proto.pv_minor;
  292. }
  293. out_unlock:
  294. mutex_unlock(&ocfs2_control_lock);
  295. if (!rc && set_p) {
  296. /* We set the global values successfully */
  297. atomic_inc(&ocfs2_control_opened);
  298. ocfs2_control_set_handshake_state(file,
  299. OCFS2_CONTROL_HANDSHAKE_VALID);
  300. }
  301. return rc;
  302. }
  303. static int ocfs2_control_get_this_node(void)
  304. {
  305. int rc;
  306. mutex_lock(&ocfs2_control_lock);
  307. if (ocfs2_control_this_node < 0)
  308. rc = -EINVAL;
  309. else
  310. rc = ocfs2_control_this_node;
  311. mutex_unlock(&ocfs2_control_lock);
  312. return rc;
  313. }
  314. static int ocfs2_control_do_setnode_msg(struct file *file,
  315. struct ocfs2_control_message_setn *msg)
  316. {
  317. long nodenum;
  318. char *ptr = NULL;
  319. struct ocfs2_control_private *p = file->private_data;
  320. if (ocfs2_control_get_handshake_state(file) !=
  321. OCFS2_CONTROL_HANDSHAKE_PROTOCOL)
  322. return -EINVAL;
  323. if (strncmp(msg->tag, OCFS2_CONTROL_MESSAGE_SETNODE_OP,
  324. OCFS2_CONTROL_MESSAGE_OP_LEN))
  325. return -EINVAL;
  326. if ((msg->space != ' ') || (msg->newline != '\n'))
  327. return -EINVAL;
  328. msg->space = msg->newline = '\0';
  329. nodenum = simple_strtol(msg->nodestr, &ptr, 16);
  330. if (!ptr || *ptr)
  331. return -EINVAL;
  332. if ((nodenum == LONG_MIN) || (nodenum == LONG_MAX) ||
  333. (nodenum > INT_MAX) || (nodenum < 0))
  334. return -ERANGE;
  335. p->op_this_node = nodenum;
  336. return ocfs2_control_install_private(file);
  337. }
  338. static int ocfs2_control_do_setversion_msg(struct file *file,
  339. struct ocfs2_control_message_setv *msg)
  340. {
  341. long major, minor;
  342. char *ptr = NULL;
  343. struct ocfs2_control_private *p = file->private_data;
  344. struct ocfs2_protocol_version *max =
  345. &ocfs2_user_plugin.sp_max_proto;
  346. if (ocfs2_control_get_handshake_state(file) !=
  347. OCFS2_CONTROL_HANDSHAKE_PROTOCOL)
  348. return -EINVAL;
  349. if (strncmp(msg->tag, OCFS2_CONTROL_MESSAGE_SETVERSION_OP,
  350. OCFS2_CONTROL_MESSAGE_OP_LEN))
  351. return -EINVAL;
  352. if ((msg->space1 != ' ') || (msg->space2 != ' ') ||
  353. (msg->newline != '\n'))
  354. return -EINVAL;
  355. msg->space1 = msg->space2 = msg->newline = '\0';
  356. major = simple_strtol(msg->major, &ptr, 16);
  357. if (!ptr || *ptr)
  358. return -EINVAL;
  359. minor = simple_strtol(msg->minor, &ptr, 16);
  360. if (!ptr || *ptr)
  361. return -EINVAL;
  362. /*
  363. * The major must be between 1 and 255, inclusive. The minor
  364. * must be between 0 and 255, inclusive. The version passed in
  365. * must be within the maximum version supported by the filesystem.
  366. */
  367. if ((major == LONG_MIN) || (major == LONG_MAX) ||
  368. (major > (u8)-1) || (major < 1))
  369. return -ERANGE;
  370. if ((minor == LONG_MIN) || (minor == LONG_MAX) ||
  371. (minor > (u8)-1) || (minor < 0))
  372. return -ERANGE;
  373. if ((major != max->pv_major) ||
  374. (minor > max->pv_minor))
  375. return -EINVAL;
  376. p->op_proto.pv_major = major;
  377. p->op_proto.pv_minor = minor;
  378. return ocfs2_control_install_private(file);
  379. }
  380. static int ocfs2_control_do_down_msg(struct file *file,
  381. struct ocfs2_control_message_down *msg)
  382. {
  383. long nodenum;
  384. char *p = NULL;
  385. if (ocfs2_control_get_handshake_state(file) !=
  386. OCFS2_CONTROL_HANDSHAKE_VALID)
  387. return -EINVAL;
  388. if (strncmp(msg->tag, OCFS2_CONTROL_MESSAGE_DOWN_OP,
  389. OCFS2_CONTROL_MESSAGE_OP_LEN))
  390. return -EINVAL;
  391. if ((msg->space1 != ' ') || (msg->space2 != ' ') ||
  392. (msg->newline != '\n'))
  393. return -EINVAL;
  394. msg->space1 = msg->space2 = msg->newline = '\0';
  395. nodenum = simple_strtol(msg->nodestr, &p, 16);
  396. if (!p || *p)
  397. return -EINVAL;
  398. if ((nodenum == LONG_MIN) || (nodenum == LONG_MAX) ||
  399. (nodenum > INT_MAX) || (nodenum < 0))
  400. return -ERANGE;
  401. ocfs2_control_send_down(msg->uuid, nodenum);
  402. return 0;
  403. }
  404. static ssize_t ocfs2_control_message(struct file *file,
  405. const char __user *buf,
  406. size_t count)
  407. {
  408. ssize_t ret;
  409. union ocfs2_control_message msg;
  410. /* Try to catch padding issues */
  411. WARN_ON(offsetof(struct ocfs2_control_message_down, uuid) !=
  412. (sizeof(msg.u_down.tag) + sizeof(msg.u_down.space1)));
  413. memset(&msg, 0, sizeof(union ocfs2_control_message));
  414. ret = ocfs2_control_cfu(&msg, count, buf, count);
  415. if (ret)
  416. goto out;
  417. if ((count == OCFS2_CONTROL_MESSAGE_SETNODE_TOTAL_LEN) &&
  418. !strncmp(msg.tag, OCFS2_CONTROL_MESSAGE_SETNODE_OP,
  419. OCFS2_CONTROL_MESSAGE_OP_LEN))
  420. ret = ocfs2_control_do_setnode_msg(file, &msg.u_setn);
  421. else if ((count == OCFS2_CONTROL_MESSAGE_SETVERSION_TOTAL_LEN) &&
  422. !strncmp(msg.tag, OCFS2_CONTROL_MESSAGE_SETVERSION_OP,
  423. OCFS2_CONTROL_MESSAGE_OP_LEN))
  424. ret = ocfs2_control_do_setversion_msg(file, &msg.u_setv);
  425. else if ((count == OCFS2_CONTROL_MESSAGE_DOWN_TOTAL_LEN) &&
  426. !strncmp(msg.tag, OCFS2_CONTROL_MESSAGE_DOWN_OP,
  427. OCFS2_CONTROL_MESSAGE_OP_LEN))
  428. ret = ocfs2_control_do_down_msg(file, &msg.u_down);
  429. else
  430. ret = -EINVAL;
  431. out:
  432. return ret ? ret : count;
  433. }
  434. static ssize_t ocfs2_control_write(struct file *file,
  435. const char __user *buf,
  436. size_t count,
  437. loff_t *ppos)
  438. {
  439. ssize_t ret;
  440. switch (ocfs2_control_get_handshake_state(file)) {
  441. case OCFS2_CONTROL_HANDSHAKE_INVALID:
  442. ret = -EINVAL;
  443. break;
  444. case OCFS2_CONTROL_HANDSHAKE_READ:
  445. ret = ocfs2_control_validate_protocol(file, buf,
  446. count);
  447. break;
  448. case OCFS2_CONTROL_HANDSHAKE_PROTOCOL:
  449. case OCFS2_CONTROL_HANDSHAKE_VALID:
  450. ret = ocfs2_control_message(file, buf, count);
  451. break;
  452. default:
  453. BUG();
  454. ret = -EIO;
  455. break;
  456. }
  457. return ret;
  458. }
  459. /*
  460. * This is a naive version. If we ever have a new protocol, we'll expand
  461. * it. Probably using seq_file.
  462. */
  463. static ssize_t ocfs2_control_read(struct file *file,
  464. char __user *buf,
  465. size_t count,
  466. loff_t *ppos)
  467. {
  468. ssize_t ret;
  469. ret = simple_read_from_buffer(buf, count, ppos,
  470. OCFS2_CONTROL_PROTO, OCFS2_CONTROL_PROTO_LEN);
  471. /* Have we read the whole protocol list? */
  472. if (ret > 0 && *ppos >= OCFS2_CONTROL_PROTO_LEN)
  473. ocfs2_control_set_handshake_state(file,
  474. OCFS2_CONTROL_HANDSHAKE_READ);
  475. return ret;
  476. }
  477. static int ocfs2_control_release(struct inode *inode, struct file *file)
  478. {
  479. struct ocfs2_control_private *p = file->private_data;
  480. mutex_lock(&ocfs2_control_lock);
  481. if (ocfs2_control_get_handshake_state(file) !=
  482. OCFS2_CONTROL_HANDSHAKE_VALID)
  483. goto out;
  484. if (atomic_dec_and_test(&ocfs2_control_opened)) {
  485. if (!list_empty(&ocfs2_live_connection_list)) {
  486. /* XXX: Do bad things! */
  487. printk(KERN_ERR
  488. "ocfs2: Unexpected release of ocfs2_control!\n"
  489. " Loss of cluster connection requires "
  490. "an emergency restart!\n");
  491. emergency_restart();
  492. }
  493. /*
  494. * Last valid close clears the node number and resets
  495. * the locking protocol version
  496. */
  497. ocfs2_control_this_node = -1;
  498. running_proto.pv_major = 0;
  499. running_proto.pv_major = 0;
  500. }
  501. out:
  502. list_del_init(&p->op_list);
  503. file->private_data = NULL;
  504. mutex_unlock(&ocfs2_control_lock);
  505. kfree(p);
  506. return 0;
  507. }
  508. static int ocfs2_control_open(struct inode *inode, struct file *file)
  509. {
  510. struct ocfs2_control_private *p;
  511. p = kzalloc(sizeof(struct ocfs2_control_private), GFP_KERNEL);
  512. if (!p)
  513. return -ENOMEM;
  514. p->op_this_node = -1;
  515. mutex_lock(&ocfs2_control_lock);
  516. file->private_data = p;
  517. list_add(&p->op_list, &ocfs2_control_private_list);
  518. mutex_unlock(&ocfs2_control_lock);
  519. return 0;
  520. }
  521. static const struct file_operations ocfs2_control_fops = {
  522. .open = ocfs2_control_open,
  523. .release = ocfs2_control_release,
  524. .read = ocfs2_control_read,
  525. .write = ocfs2_control_write,
  526. .owner = THIS_MODULE,
  527. .llseek = default_llseek,
  528. };
  529. static 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 void fsdlm_lock_ast_wrapper(void *astarg)
  557. {
  558. struct ocfs2_dlm_lksb *lksb = astarg;
  559. int status = lksb->lksb_fsdlm.sb_status;
  560. /*
  561. * For now we're punting on the issue of other non-standard errors
  562. * where we can't tell if the unlock_ast or lock_ast should be called.
  563. * The main "other error" that's possible is EINVAL which means the
  564. * function was called with invalid args, which shouldn't be possible
  565. * since the caller here is under our control. Other non-standard
  566. * errors probably fall into the same category, or otherwise are fatal
  567. * which means we can't carry on anyway.
  568. */
  569. if (status == -DLM_EUNLOCK || status == -DLM_ECANCEL)
  570. lksb->lksb_conn->cc_proto->lp_unlock_ast(lksb, 0);
  571. else
  572. lksb->lksb_conn->cc_proto->lp_lock_ast(lksb);
  573. }
  574. static void fsdlm_blocking_ast_wrapper(void *astarg, int level)
  575. {
  576. struct ocfs2_dlm_lksb *lksb = astarg;
  577. lksb->lksb_conn->cc_proto->lp_blocking_ast(lksb, level);
  578. }
  579. static int user_dlm_lock(struct ocfs2_cluster_connection *conn,
  580. int mode,
  581. struct ocfs2_dlm_lksb *lksb,
  582. u32 flags,
  583. void *name,
  584. unsigned int namelen)
  585. {
  586. int ret;
  587. if (!lksb->lksb_fsdlm.sb_lvbptr)
  588. lksb->lksb_fsdlm.sb_lvbptr = (char *)lksb +
  589. sizeof(struct dlm_lksb);
  590. ret = dlm_lock(conn->cc_lockspace, mode, &lksb->lksb_fsdlm,
  591. flags|DLM_LKF_NODLCKWT, name, namelen, 0,
  592. fsdlm_lock_ast_wrapper, lksb,
  593. fsdlm_blocking_ast_wrapper);
  594. return ret;
  595. }
  596. static int user_dlm_unlock(struct ocfs2_cluster_connection *conn,
  597. struct ocfs2_dlm_lksb *lksb,
  598. u32 flags)
  599. {
  600. int ret;
  601. ret = dlm_unlock(conn->cc_lockspace, lksb->lksb_fsdlm.sb_lkid,
  602. flags, &lksb->lksb_fsdlm, lksb);
  603. return ret;
  604. }
  605. static int user_dlm_lock_status(struct ocfs2_dlm_lksb *lksb)
  606. {
  607. return lksb->lksb_fsdlm.sb_status;
  608. }
  609. static int user_dlm_lvb_valid(struct ocfs2_dlm_lksb *lksb)
  610. {
  611. int invalid = lksb->lksb_fsdlm.sb_flags & DLM_SBF_VALNOTVALID;
  612. return !invalid;
  613. }
  614. static void *user_dlm_lvb(struct ocfs2_dlm_lksb *lksb)
  615. {
  616. if (!lksb->lksb_fsdlm.sb_lvbptr)
  617. lksb->lksb_fsdlm.sb_lvbptr = (char *)lksb +
  618. sizeof(struct dlm_lksb);
  619. return (void *)(lksb->lksb_fsdlm.sb_lvbptr);
  620. }
  621. static void user_dlm_dump_lksb(struct ocfs2_dlm_lksb *lksb)
  622. {
  623. }
  624. static int user_plock(struct ocfs2_cluster_connection *conn,
  625. u64 ino,
  626. struct file *file,
  627. int cmd,
  628. struct file_lock *fl)
  629. {
  630. /*
  631. * This more or less just demuxes the plock request into any
  632. * one of three dlm calls.
  633. *
  634. * Internally, fs/dlm will pass these to a misc device, which
  635. * a userspace daemon will read and write to.
  636. *
  637. * For now, cancel requests (which happen internally only),
  638. * are turned into unlocks. Most of this function taken from
  639. * gfs2_lock.
  640. */
  641. if (cmd == F_CANCELLK) {
  642. cmd = F_SETLK;
  643. fl->fl_type = F_UNLCK;
  644. }
  645. if (IS_GETLK(cmd))
  646. return dlm_posix_get(conn->cc_lockspace, ino, file, fl);
  647. else if (fl->fl_type == F_UNLCK)
  648. return dlm_posix_unlock(conn->cc_lockspace, ino, file, fl);
  649. else
  650. return dlm_posix_lock(conn->cc_lockspace, ino, file, cmd, fl);
  651. }
  652. /*
  653. * Compare a requested locking protocol version against the current one.
  654. *
  655. * If the major numbers are different, they are incompatible.
  656. * If the current minor is greater than the request, they are incompatible.
  657. * If the current minor is less than or equal to the request, they are
  658. * compatible, and the requester should run at the current minor version.
  659. */
  660. static int fs_protocol_compare(struct ocfs2_protocol_version *existing,
  661. struct ocfs2_protocol_version *request)
  662. {
  663. if (existing->pv_major != request->pv_major)
  664. return 1;
  665. if (existing->pv_minor > request->pv_minor)
  666. return 1;
  667. if (existing->pv_minor < request->pv_minor)
  668. request->pv_minor = existing->pv_minor;
  669. return 0;
  670. }
  671. static void lvb_to_version(char *lvb, struct ocfs2_protocol_version *ver)
  672. {
  673. struct ocfs2_protocol_version *pv =
  674. (struct ocfs2_protocol_version *)lvb;
  675. /*
  676. * ocfs2_protocol_version has two u8 variables, so we don't
  677. * need any endian conversion.
  678. */
  679. ver->pv_major = pv->pv_major;
  680. ver->pv_minor = pv->pv_minor;
  681. }
  682. static void version_to_lvb(struct ocfs2_protocol_version *ver, char *lvb)
  683. {
  684. struct ocfs2_protocol_version *pv =
  685. (struct ocfs2_protocol_version *)lvb;
  686. /*
  687. * ocfs2_protocol_version has two u8 variables, so we don't
  688. * need any endian conversion.
  689. */
  690. pv->pv_major = ver->pv_major;
  691. pv->pv_minor = ver->pv_minor;
  692. }
  693. static void sync_wait_cb(void *arg)
  694. {
  695. struct ocfs2_cluster_connection *conn = arg;
  696. struct ocfs2_live_connection *lc = conn->cc_private;
  697. complete(&lc->oc_sync_wait);
  698. }
  699. static int sync_unlock(struct ocfs2_cluster_connection *conn,
  700. struct dlm_lksb *lksb, char *name)
  701. {
  702. int error;
  703. struct ocfs2_live_connection *lc = conn->cc_private;
  704. error = dlm_unlock(conn->cc_lockspace, lksb->sb_lkid, 0, lksb, conn);
  705. if (error) {
  706. printk(KERN_ERR "%s lkid %x error %d\n",
  707. name, lksb->sb_lkid, error);
  708. return error;
  709. }
  710. wait_for_completion(&lc->oc_sync_wait);
  711. if (lksb->sb_status != -DLM_EUNLOCK) {
  712. printk(KERN_ERR "%s lkid %x status %d\n",
  713. name, lksb->sb_lkid, lksb->sb_status);
  714. return -1;
  715. }
  716. return 0;
  717. }
  718. static int sync_lock(struct ocfs2_cluster_connection *conn,
  719. int mode, uint32_t flags,
  720. struct dlm_lksb *lksb, char *name)
  721. {
  722. int error, status;
  723. struct ocfs2_live_connection *lc = conn->cc_private;
  724. error = dlm_lock(conn->cc_lockspace, mode, lksb, flags,
  725. name, strlen(name),
  726. 0, sync_wait_cb, conn, NULL);
  727. if (error) {
  728. printk(KERN_ERR "%s lkid %x flags %x mode %d error %d\n",
  729. name, lksb->sb_lkid, flags, mode, error);
  730. return error;
  731. }
  732. wait_for_completion(&lc->oc_sync_wait);
  733. status = lksb->sb_status;
  734. if (status && status != -EAGAIN) {
  735. printk(KERN_ERR "%s lkid %x flags %x mode %d status %d\n",
  736. name, lksb->sb_lkid, flags, mode, status);
  737. }
  738. return status;
  739. }
  740. static int version_lock(struct ocfs2_cluster_connection *conn, int mode,
  741. int flags)
  742. {
  743. struct ocfs2_live_connection *lc = conn->cc_private;
  744. return sync_lock(conn, mode, flags,
  745. &lc->oc_version_lksb, VERSION_LOCK);
  746. }
  747. static int version_unlock(struct ocfs2_cluster_connection *conn)
  748. {
  749. struct ocfs2_live_connection *lc = conn->cc_private;
  750. return sync_unlock(conn, &lc->oc_version_lksb, VERSION_LOCK);
  751. }
  752. /* get_protocol_version()
  753. *
  754. * To exchange ocfs2 versioning, we use the LVB of the version dlm lock.
  755. * The algorithm is:
  756. * 1. Attempt to take the lock in EX mode (non-blocking).
  757. * 2. If successful (which means it is the first mount), write the
  758. * version number and downconvert to PR lock.
  759. * 3. If unsuccessful (returns -EAGAIN), read the version from the LVB after
  760. * taking the PR lock.
  761. */
  762. static int get_protocol_version(struct ocfs2_cluster_connection *conn)
  763. {
  764. int ret;
  765. struct ocfs2_live_connection *lc = conn->cc_private;
  766. struct ocfs2_protocol_version pv;
  767. running_proto.pv_major =
  768. ocfs2_user_plugin.sp_max_proto.pv_major;
  769. running_proto.pv_minor =
  770. ocfs2_user_plugin.sp_max_proto.pv_minor;
  771. lc->oc_version_lksb.sb_lvbptr = lc->oc_lvb;
  772. ret = version_lock(conn, DLM_LOCK_EX,
  773. DLM_LKF_VALBLK|DLM_LKF_NOQUEUE);
  774. if (!ret) {
  775. conn->cc_version.pv_major = running_proto.pv_major;
  776. conn->cc_version.pv_minor = running_proto.pv_minor;
  777. version_to_lvb(&running_proto, lc->oc_lvb);
  778. version_lock(conn, DLM_LOCK_PR, DLM_LKF_CONVERT|DLM_LKF_VALBLK);
  779. } else if (ret == -EAGAIN) {
  780. ret = version_lock(conn, DLM_LOCK_PR, DLM_LKF_VALBLK);
  781. if (ret)
  782. goto out;
  783. lvb_to_version(lc->oc_lvb, &pv);
  784. if ((pv.pv_major != running_proto.pv_major) ||
  785. (pv.pv_minor > running_proto.pv_minor)) {
  786. ret = -EINVAL;
  787. goto out;
  788. }
  789. conn->cc_version.pv_major = pv.pv_major;
  790. conn->cc_version.pv_minor = pv.pv_minor;
  791. }
  792. out:
  793. return ret;
  794. }
  795. static void user_recover_prep(void *arg)
  796. {
  797. }
  798. static void user_recover_slot(void *arg, struct dlm_slot *slot)
  799. {
  800. struct ocfs2_cluster_connection *conn = arg;
  801. printk(KERN_INFO "ocfs2: Node %d/%d down. Initiating recovery.\n",
  802. slot->nodeid, slot->slot);
  803. conn->cc_recovery_handler(slot->nodeid, conn->cc_recovery_data);
  804. }
  805. static void user_recover_done(void *arg, struct dlm_slot *slots,
  806. int num_slots, int our_slot,
  807. uint32_t generation)
  808. {
  809. struct ocfs2_cluster_connection *conn = arg;
  810. struct ocfs2_live_connection *lc = conn->cc_private;
  811. int i;
  812. for (i = 0; i < num_slots; i++)
  813. if (slots[i].slot == our_slot) {
  814. atomic_set(&lc->oc_this_node, slots[i].nodeid);
  815. break;
  816. }
  817. lc->oc_our_slot = our_slot;
  818. wake_up(&lc->oc_wait);
  819. }
  820. static const struct dlm_lockspace_ops ocfs2_ls_ops = {
  821. .recover_prep = user_recover_prep,
  822. .recover_slot = user_recover_slot,
  823. .recover_done = user_recover_done,
  824. };
  825. static int user_cluster_disconnect(struct ocfs2_cluster_connection *conn)
  826. {
  827. version_unlock(conn);
  828. dlm_release_lockspace(conn->cc_lockspace, 2);
  829. conn->cc_lockspace = NULL;
  830. ocfs2_live_connection_drop(conn->cc_private);
  831. conn->cc_private = NULL;
  832. return 0;
  833. }
  834. static int user_cluster_connect(struct ocfs2_cluster_connection *conn)
  835. {
  836. dlm_lockspace_t *fsdlm;
  837. struct ocfs2_live_connection *lc;
  838. int rc, ops_rv;
  839. BUG_ON(conn == NULL);
  840. lc = kzalloc(sizeof(struct ocfs2_live_connection), GFP_KERNEL);
  841. if (!lc) {
  842. rc = -ENOMEM;
  843. goto out;
  844. }
  845. init_waitqueue_head(&lc->oc_wait);
  846. init_completion(&lc->oc_sync_wait);
  847. atomic_set(&lc->oc_this_node, 0);
  848. conn->cc_private = lc;
  849. lc->oc_type = NO_CONTROLD;
  850. rc = dlm_new_lockspace(conn->cc_name, conn->cc_cluster_name,
  851. DLM_LSFL_FS, DLM_LVB_LEN,
  852. &ocfs2_ls_ops, conn, &ops_rv, &fsdlm);
  853. if (rc)
  854. goto out;
  855. if (ops_rv == -EOPNOTSUPP) {
  856. lc->oc_type = WITH_CONTROLD;
  857. printk(KERN_NOTICE "ocfs2: You seem to be using an older "
  858. "version of dlm_controld and/or ocfs2-tools."
  859. " Please consider upgrading.\n");
  860. } else if (ops_rv) {
  861. rc = ops_rv;
  862. goto out;
  863. }
  864. conn->cc_lockspace = fsdlm;
  865. rc = ocfs2_live_connection_attach(conn, lc);
  866. if (rc)
  867. goto out;
  868. if (lc->oc_type == NO_CONTROLD) {
  869. rc = get_protocol_version(conn);
  870. if (rc) {
  871. printk(KERN_ERR "ocfs2: Could not determine"
  872. " locking version\n");
  873. user_cluster_disconnect(conn);
  874. goto out;
  875. }
  876. wait_event(lc->oc_wait, (atomic_read(&lc->oc_this_node) > 0));
  877. }
  878. /*
  879. * running_proto must have been set before we allowed any mounts
  880. * to proceed.
  881. */
  882. if (fs_protocol_compare(&running_proto, &conn->cc_version)) {
  883. printk(KERN_ERR
  884. "Unable to mount with fs locking protocol version "
  885. "%u.%u because negotiated protocol is %u.%u\n",
  886. conn->cc_version.pv_major, conn->cc_version.pv_minor,
  887. running_proto.pv_major, running_proto.pv_minor);
  888. rc = -EPROTO;
  889. ocfs2_live_connection_drop(lc);
  890. lc = NULL;
  891. }
  892. out:
  893. if (rc && lc)
  894. kfree(lc);
  895. return rc;
  896. }
  897. static int user_cluster_this_node(struct ocfs2_cluster_connection *conn,
  898. unsigned int *this_node)
  899. {
  900. int rc;
  901. struct ocfs2_live_connection *lc = conn->cc_private;
  902. if (lc->oc_type == WITH_CONTROLD)
  903. rc = ocfs2_control_get_this_node();
  904. else if (lc->oc_type == NO_CONTROLD)
  905. rc = atomic_read(&lc->oc_this_node);
  906. else
  907. rc = -EINVAL;
  908. if (rc < 0)
  909. return rc;
  910. *this_node = rc;
  911. return 0;
  912. }
  913. static struct ocfs2_stack_operations ocfs2_user_plugin_ops = {
  914. .connect = user_cluster_connect,
  915. .disconnect = user_cluster_disconnect,
  916. .this_node = user_cluster_this_node,
  917. .dlm_lock = user_dlm_lock,
  918. .dlm_unlock = user_dlm_unlock,
  919. .lock_status = user_dlm_lock_status,
  920. .lvb_valid = user_dlm_lvb_valid,
  921. .lock_lvb = user_dlm_lvb,
  922. .plock = user_plock,
  923. .dump_lksb = user_dlm_dump_lksb,
  924. };
  925. static struct ocfs2_stack_plugin ocfs2_user_plugin = {
  926. .sp_name = "user",
  927. .sp_ops = &ocfs2_user_plugin_ops,
  928. .sp_owner = THIS_MODULE,
  929. };
  930. static int __init ocfs2_user_plugin_init(void)
  931. {
  932. int rc;
  933. rc = ocfs2_control_init();
  934. if (!rc) {
  935. rc = ocfs2_stack_glue_register(&ocfs2_user_plugin);
  936. if (rc)
  937. ocfs2_control_exit();
  938. }
  939. return rc;
  940. }
  941. static void __exit ocfs2_user_plugin_exit(void)
  942. {
  943. ocfs2_stack_glue_unregister(&ocfs2_user_plugin);
  944. ocfs2_control_exit();
  945. }
  946. MODULE_AUTHOR("Oracle");
  947. MODULE_DESCRIPTION("ocfs2 driver for userspace cluster stacks");
  948. MODULE_LICENSE("GPL");
  949. module_init(ocfs2_user_plugin_init);
  950. module_exit(ocfs2_user_plugin_exit);