stack_user.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  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);