stack_user.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  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_minor = 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. misc_deregister(&ocfs2_control_device);
  549. }
  550. static void fsdlm_lock_ast_wrapper(void *astarg)
  551. {
  552. struct ocfs2_dlm_lksb *lksb = astarg;
  553. int status = lksb->lksb_fsdlm.sb_status;
  554. /*
  555. * For now we're punting on the issue of other non-standard errors
  556. * where we can't tell if the unlock_ast or lock_ast should be called.
  557. * The main "other error" that's possible is EINVAL which means the
  558. * function was called with invalid args, which shouldn't be possible
  559. * since the caller here is under our control. Other non-standard
  560. * errors probably fall into the same category, or otherwise are fatal
  561. * which means we can't carry on anyway.
  562. */
  563. if (status == -DLM_EUNLOCK || status == -DLM_ECANCEL)
  564. lksb->lksb_conn->cc_proto->lp_unlock_ast(lksb, 0);
  565. else
  566. lksb->lksb_conn->cc_proto->lp_lock_ast(lksb);
  567. }
  568. static void fsdlm_blocking_ast_wrapper(void *astarg, int level)
  569. {
  570. struct ocfs2_dlm_lksb *lksb = astarg;
  571. lksb->lksb_conn->cc_proto->lp_blocking_ast(lksb, level);
  572. }
  573. static int user_dlm_lock(struct ocfs2_cluster_connection *conn,
  574. int mode,
  575. struct ocfs2_dlm_lksb *lksb,
  576. u32 flags,
  577. void *name,
  578. unsigned int namelen)
  579. {
  580. int ret;
  581. if (!lksb->lksb_fsdlm.sb_lvbptr)
  582. lksb->lksb_fsdlm.sb_lvbptr = (char *)lksb +
  583. sizeof(struct dlm_lksb);
  584. ret = dlm_lock(conn->cc_lockspace, mode, &lksb->lksb_fsdlm,
  585. flags|DLM_LKF_NODLCKWT, name, namelen, 0,
  586. fsdlm_lock_ast_wrapper, lksb,
  587. fsdlm_blocking_ast_wrapper);
  588. return ret;
  589. }
  590. static int user_dlm_unlock(struct ocfs2_cluster_connection *conn,
  591. struct ocfs2_dlm_lksb *lksb,
  592. u32 flags)
  593. {
  594. int ret;
  595. ret = dlm_unlock(conn->cc_lockspace, lksb->lksb_fsdlm.sb_lkid,
  596. flags, &lksb->lksb_fsdlm, lksb);
  597. return ret;
  598. }
  599. static int user_dlm_lock_status(struct ocfs2_dlm_lksb *lksb)
  600. {
  601. return lksb->lksb_fsdlm.sb_status;
  602. }
  603. static int user_dlm_lvb_valid(struct ocfs2_dlm_lksb *lksb)
  604. {
  605. int invalid = lksb->lksb_fsdlm.sb_flags & DLM_SBF_VALNOTVALID;
  606. return !invalid;
  607. }
  608. static void *user_dlm_lvb(struct ocfs2_dlm_lksb *lksb)
  609. {
  610. if (!lksb->lksb_fsdlm.sb_lvbptr)
  611. lksb->lksb_fsdlm.sb_lvbptr = (char *)lksb +
  612. sizeof(struct dlm_lksb);
  613. return (void *)(lksb->lksb_fsdlm.sb_lvbptr);
  614. }
  615. static void user_dlm_dump_lksb(struct ocfs2_dlm_lksb *lksb)
  616. {
  617. }
  618. static int user_plock(struct ocfs2_cluster_connection *conn,
  619. u64 ino,
  620. struct file *file,
  621. int cmd,
  622. struct file_lock *fl)
  623. {
  624. /*
  625. * This more or less just demuxes the plock request into any
  626. * one of three dlm calls.
  627. *
  628. * Internally, fs/dlm will pass these to a misc device, which
  629. * a userspace daemon will read and write to.
  630. *
  631. * For now, cancel requests (which happen internally only),
  632. * are turned into unlocks. Most of this function taken from
  633. * gfs2_lock.
  634. */
  635. if (cmd == F_CANCELLK) {
  636. cmd = F_SETLK;
  637. fl->fl_type = F_UNLCK;
  638. }
  639. if (IS_GETLK(cmd))
  640. return dlm_posix_get(conn->cc_lockspace, ino, file, fl);
  641. else if (fl->fl_type == F_UNLCK)
  642. return dlm_posix_unlock(conn->cc_lockspace, ino, file, fl);
  643. else
  644. return dlm_posix_lock(conn->cc_lockspace, ino, file, cmd, fl);
  645. }
  646. /*
  647. * Compare a requested locking protocol version against the current one.
  648. *
  649. * If the major numbers are different, they are incompatible.
  650. * If the current minor is greater than the request, they are incompatible.
  651. * If the current minor is less than or equal to the request, they are
  652. * compatible, and the requester should run at the current minor version.
  653. */
  654. static int fs_protocol_compare(struct ocfs2_protocol_version *existing,
  655. struct ocfs2_protocol_version *request)
  656. {
  657. if (existing->pv_major != request->pv_major)
  658. return 1;
  659. if (existing->pv_minor > request->pv_minor)
  660. return 1;
  661. if (existing->pv_minor < request->pv_minor)
  662. request->pv_minor = existing->pv_minor;
  663. return 0;
  664. }
  665. static void lvb_to_version(char *lvb, struct ocfs2_protocol_version *ver)
  666. {
  667. struct ocfs2_protocol_version *pv =
  668. (struct ocfs2_protocol_version *)lvb;
  669. /*
  670. * ocfs2_protocol_version has two u8 variables, so we don't
  671. * need any endian conversion.
  672. */
  673. ver->pv_major = pv->pv_major;
  674. ver->pv_minor = pv->pv_minor;
  675. }
  676. static void version_to_lvb(struct ocfs2_protocol_version *ver, char *lvb)
  677. {
  678. struct ocfs2_protocol_version *pv =
  679. (struct ocfs2_protocol_version *)lvb;
  680. /*
  681. * ocfs2_protocol_version has two u8 variables, so we don't
  682. * need any endian conversion.
  683. */
  684. pv->pv_major = ver->pv_major;
  685. pv->pv_minor = ver->pv_minor;
  686. }
  687. static void sync_wait_cb(void *arg)
  688. {
  689. struct ocfs2_cluster_connection *conn = arg;
  690. struct ocfs2_live_connection *lc = conn->cc_private;
  691. complete(&lc->oc_sync_wait);
  692. }
  693. static int sync_unlock(struct ocfs2_cluster_connection *conn,
  694. struct dlm_lksb *lksb, char *name)
  695. {
  696. int error;
  697. struct ocfs2_live_connection *lc = conn->cc_private;
  698. error = dlm_unlock(conn->cc_lockspace, lksb->sb_lkid, 0, lksb, conn);
  699. if (error) {
  700. printk(KERN_ERR "%s lkid %x error %d\n",
  701. name, lksb->sb_lkid, error);
  702. return error;
  703. }
  704. wait_for_completion(&lc->oc_sync_wait);
  705. if (lksb->sb_status != -DLM_EUNLOCK) {
  706. printk(KERN_ERR "%s lkid %x status %d\n",
  707. name, lksb->sb_lkid, lksb->sb_status);
  708. return -1;
  709. }
  710. return 0;
  711. }
  712. static int sync_lock(struct ocfs2_cluster_connection *conn,
  713. int mode, uint32_t flags,
  714. struct dlm_lksb *lksb, char *name)
  715. {
  716. int error, status;
  717. struct ocfs2_live_connection *lc = conn->cc_private;
  718. error = dlm_lock(conn->cc_lockspace, mode, lksb, flags,
  719. name, strlen(name),
  720. 0, sync_wait_cb, conn, NULL);
  721. if (error) {
  722. printk(KERN_ERR "%s lkid %x flags %x mode %d error %d\n",
  723. name, lksb->sb_lkid, flags, mode, error);
  724. return error;
  725. }
  726. wait_for_completion(&lc->oc_sync_wait);
  727. status = lksb->sb_status;
  728. if (status && status != -EAGAIN) {
  729. printk(KERN_ERR "%s lkid %x flags %x mode %d status %d\n",
  730. name, lksb->sb_lkid, flags, mode, status);
  731. }
  732. return status;
  733. }
  734. static int version_lock(struct ocfs2_cluster_connection *conn, int mode,
  735. int flags)
  736. {
  737. struct ocfs2_live_connection *lc = conn->cc_private;
  738. return sync_lock(conn, mode, flags,
  739. &lc->oc_version_lksb, VERSION_LOCK);
  740. }
  741. static int version_unlock(struct ocfs2_cluster_connection *conn)
  742. {
  743. struct ocfs2_live_connection *lc = conn->cc_private;
  744. return sync_unlock(conn, &lc->oc_version_lksb, VERSION_LOCK);
  745. }
  746. /* get_protocol_version()
  747. *
  748. * To exchange ocfs2 versioning, we use the LVB of the version dlm lock.
  749. * The algorithm is:
  750. * 1. Attempt to take the lock in EX mode (non-blocking).
  751. * 2. If successful (which means it is the first mount), write the
  752. * version number and downconvert to PR lock.
  753. * 3. If unsuccessful (returns -EAGAIN), read the version from the LVB after
  754. * taking the PR lock.
  755. */
  756. static int get_protocol_version(struct ocfs2_cluster_connection *conn)
  757. {
  758. int ret;
  759. struct ocfs2_live_connection *lc = conn->cc_private;
  760. struct ocfs2_protocol_version pv;
  761. running_proto.pv_major =
  762. ocfs2_user_plugin.sp_max_proto.pv_major;
  763. running_proto.pv_minor =
  764. ocfs2_user_plugin.sp_max_proto.pv_minor;
  765. lc->oc_version_lksb.sb_lvbptr = lc->oc_lvb;
  766. ret = version_lock(conn, DLM_LOCK_EX,
  767. DLM_LKF_VALBLK|DLM_LKF_NOQUEUE);
  768. if (!ret) {
  769. conn->cc_version.pv_major = running_proto.pv_major;
  770. conn->cc_version.pv_minor = running_proto.pv_minor;
  771. version_to_lvb(&running_proto, lc->oc_lvb);
  772. version_lock(conn, DLM_LOCK_PR, DLM_LKF_CONVERT|DLM_LKF_VALBLK);
  773. } else if (ret == -EAGAIN) {
  774. ret = version_lock(conn, DLM_LOCK_PR, DLM_LKF_VALBLK);
  775. if (ret)
  776. goto out;
  777. lvb_to_version(lc->oc_lvb, &pv);
  778. if ((pv.pv_major != running_proto.pv_major) ||
  779. (pv.pv_minor > running_proto.pv_minor)) {
  780. ret = -EINVAL;
  781. goto out;
  782. }
  783. conn->cc_version.pv_major = pv.pv_major;
  784. conn->cc_version.pv_minor = pv.pv_minor;
  785. }
  786. out:
  787. return ret;
  788. }
  789. static void user_recover_prep(void *arg)
  790. {
  791. }
  792. static void user_recover_slot(void *arg, struct dlm_slot *slot)
  793. {
  794. struct ocfs2_cluster_connection *conn = arg;
  795. printk(KERN_INFO "ocfs2: Node %d/%d down. Initiating recovery.\n",
  796. slot->nodeid, slot->slot);
  797. conn->cc_recovery_handler(slot->nodeid, conn->cc_recovery_data);
  798. }
  799. static void user_recover_done(void *arg, struct dlm_slot *slots,
  800. int num_slots, int our_slot,
  801. uint32_t generation)
  802. {
  803. struct ocfs2_cluster_connection *conn = arg;
  804. struct ocfs2_live_connection *lc = conn->cc_private;
  805. int i;
  806. for (i = 0; i < num_slots; i++)
  807. if (slots[i].slot == our_slot) {
  808. atomic_set(&lc->oc_this_node, slots[i].nodeid);
  809. break;
  810. }
  811. lc->oc_our_slot = our_slot;
  812. wake_up(&lc->oc_wait);
  813. }
  814. static const struct dlm_lockspace_ops ocfs2_ls_ops = {
  815. .recover_prep = user_recover_prep,
  816. .recover_slot = user_recover_slot,
  817. .recover_done = user_recover_done,
  818. };
  819. static int user_cluster_disconnect(struct ocfs2_cluster_connection *conn)
  820. {
  821. version_unlock(conn);
  822. dlm_release_lockspace(conn->cc_lockspace, 2);
  823. conn->cc_lockspace = NULL;
  824. ocfs2_live_connection_drop(conn->cc_private);
  825. conn->cc_private = NULL;
  826. return 0;
  827. }
  828. static int user_cluster_connect(struct ocfs2_cluster_connection *conn)
  829. {
  830. dlm_lockspace_t *fsdlm;
  831. struct ocfs2_live_connection *lc;
  832. int rc, ops_rv;
  833. BUG_ON(conn == NULL);
  834. lc = kzalloc(sizeof(struct ocfs2_live_connection), GFP_KERNEL);
  835. if (!lc)
  836. return -ENOMEM;
  837. init_waitqueue_head(&lc->oc_wait);
  838. init_completion(&lc->oc_sync_wait);
  839. atomic_set(&lc->oc_this_node, 0);
  840. conn->cc_private = lc;
  841. lc->oc_type = NO_CONTROLD;
  842. rc = dlm_new_lockspace(conn->cc_name, conn->cc_cluster_name,
  843. DLM_LSFL_FS, DLM_LVB_LEN,
  844. &ocfs2_ls_ops, conn, &ops_rv, &fsdlm);
  845. if (rc)
  846. goto out;
  847. if (ops_rv == -EOPNOTSUPP) {
  848. lc->oc_type = WITH_CONTROLD;
  849. printk(KERN_NOTICE "ocfs2: You seem to be using an older "
  850. "version of dlm_controld and/or ocfs2-tools."
  851. " Please consider upgrading.\n");
  852. } else if (ops_rv) {
  853. rc = ops_rv;
  854. goto out;
  855. }
  856. conn->cc_lockspace = fsdlm;
  857. rc = ocfs2_live_connection_attach(conn, lc);
  858. if (rc)
  859. goto out;
  860. if (lc->oc_type == NO_CONTROLD) {
  861. rc = get_protocol_version(conn);
  862. if (rc) {
  863. printk(KERN_ERR "ocfs2: Could not determine"
  864. " locking version\n");
  865. user_cluster_disconnect(conn);
  866. goto out;
  867. }
  868. wait_event(lc->oc_wait, (atomic_read(&lc->oc_this_node) > 0));
  869. }
  870. /*
  871. * running_proto must have been set before we allowed any mounts
  872. * to proceed.
  873. */
  874. if (fs_protocol_compare(&running_proto, &conn->cc_version)) {
  875. printk(KERN_ERR
  876. "Unable to mount with fs locking protocol version "
  877. "%u.%u because negotiated protocol is %u.%u\n",
  878. conn->cc_version.pv_major, conn->cc_version.pv_minor,
  879. running_proto.pv_major, running_proto.pv_minor);
  880. rc = -EPROTO;
  881. ocfs2_live_connection_drop(lc);
  882. lc = NULL;
  883. }
  884. out:
  885. if (rc)
  886. kfree(lc);
  887. return rc;
  888. }
  889. static int user_cluster_this_node(struct ocfs2_cluster_connection *conn,
  890. unsigned int *this_node)
  891. {
  892. int rc;
  893. struct ocfs2_live_connection *lc = conn->cc_private;
  894. if (lc->oc_type == WITH_CONTROLD)
  895. rc = ocfs2_control_get_this_node();
  896. else if (lc->oc_type == NO_CONTROLD)
  897. rc = atomic_read(&lc->oc_this_node);
  898. else
  899. rc = -EINVAL;
  900. if (rc < 0)
  901. return rc;
  902. *this_node = rc;
  903. return 0;
  904. }
  905. static struct ocfs2_stack_operations ocfs2_user_plugin_ops = {
  906. .connect = user_cluster_connect,
  907. .disconnect = user_cluster_disconnect,
  908. .this_node = user_cluster_this_node,
  909. .dlm_lock = user_dlm_lock,
  910. .dlm_unlock = user_dlm_unlock,
  911. .lock_status = user_dlm_lock_status,
  912. .lvb_valid = user_dlm_lvb_valid,
  913. .lock_lvb = user_dlm_lvb,
  914. .plock = user_plock,
  915. .dump_lksb = user_dlm_dump_lksb,
  916. };
  917. static struct ocfs2_stack_plugin ocfs2_user_plugin = {
  918. .sp_name = "user",
  919. .sp_ops = &ocfs2_user_plugin_ops,
  920. .sp_owner = THIS_MODULE,
  921. };
  922. static int __init ocfs2_user_plugin_init(void)
  923. {
  924. int rc;
  925. rc = ocfs2_control_init();
  926. if (!rc) {
  927. rc = ocfs2_stack_glue_register(&ocfs2_user_plugin);
  928. if (rc)
  929. ocfs2_control_exit();
  930. }
  931. return rc;
  932. }
  933. static void __exit ocfs2_user_plugin_exit(void)
  934. {
  935. ocfs2_stack_glue_unregister(&ocfs2_user_plugin);
  936. ocfs2_control_exit();
  937. }
  938. MODULE_AUTHOR("Oracle");
  939. MODULE_DESCRIPTION("ocfs2 driver for userspace cluster stacks");
  940. MODULE_LICENSE("GPL");
  941. module_init(ocfs2_user_plugin_init);
  942. module_exit(ocfs2_user_plugin_exit);