stack_user.c 28 KB

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