stack_user.c 29 KB

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