stack_user.c 29 KB

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