stack_user.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  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. char *ptr = NULL;
  310. struct ocfs2_control_private *p = file->private_data;
  311. if (ocfs2_control_get_handshake_state(file) !=
  312. OCFS2_CONTROL_HANDSHAKE_PROTOCOL)
  313. return -EINVAL;
  314. if (strncmp(msg->tag, OCFS2_CONTROL_MESSAGE_SETNODE_OP,
  315. OCFS2_CONTROL_MESSAGE_OP_LEN))
  316. return -EINVAL;
  317. if ((msg->space != ' ') || (msg->newline != '\n'))
  318. return -EINVAL;
  319. msg->space = msg->newline = '\0';
  320. nodenum = simple_strtol(msg->nodestr, &ptr, 16);
  321. if (!ptr || *ptr)
  322. return -EINVAL;
  323. if ((nodenum == LONG_MIN) || (nodenum == LONG_MAX) ||
  324. (nodenum > INT_MAX) || (nodenum < 0))
  325. return -ERANGE;
  326. p->op_this_node = nodenum;
  327. return ocfs2_control_install_private(file);
  328. }
  329. static int ocfs2_control_do_setversion_msg(struct file *file,
  330. struct ocfs2_control_message_setv *msg)
  331. {
  332. long major, minor;
  333. char *ptr = NULL;
  334. struct ocfs2_control_private *p = file->private_data;
  335. struct ocfs2_protocol_version *max =
  336. &ocfs2_user_plugin.sp_max_proto;
  337. if (ocfs2_control_get_handshake_state(file) !=
  338. OCFS2_CONTROL_HANDSHAKE_PROTOCOL)
  339. return -EINVAL;
  340. if (strncmp(msg->tag, OCFS2_CONTROL_MESSAGE_SETVERSION_OP,
  341. OCFS2_CONTROL_MESSAGE_OP_LEN))
  342. return -EINVAL;
  343. if ((msg->space1 != ' ') || (msg->space2 != ' ') ||
  344. (msg->newline != '\n'))
  345. return -EINVAL;
  346. msg->space1 = msg->space2 = msg->newline = '\0';
  347. major = simple_strtol(msg->major, &ptr, 16);
  348. if (!ptr || *ptr)
  349. return -EINVAL;
  350. minor = simple_strtol(msg->minor, &ptr, 16);
  351. if (!ptr || *ptr)
  352. return -EINVAL;
  353. /*
  354. * The major must be between 1 and 255, inclusive. The minor
  355. * must be between 0 and 255, inclusive. The version passed in
  356. * must be within the maximum version supported by the filesystem.
  357. */
  358. if ((major == LONG_MIN) || (major == LONG_MAX) ||
  359. (major > (u8)-1) || (major < 1))
  360. return -ERANGE;
  361. if ((minor == LONG_MIN) || (minor == LONG_MAX) ||
  362. (minor > (u8)-1) || (minor < 0))
  363. return -ERANGE;
  364. if ((major != max->pv_major) ||
  365. (minor > max->pv_minor))
  366. return -EINVAL;
  367. p->op_proto.pv_major = major;
  368. p->op_proto.pv_minor = minor;
  369. return ocfs2_control_install_private(file);
  370. }
  371. static int ocfs2_control_do_down_msg(struct file *file,
  372. struct ocfs2_control_message_down *msg)
  373. {
  374. long nodenum;
  375. char *p = NULL;
  376. if (ocfs2_control_get_handshake_state(file) !=
  377. OCFS2_CONTROL_HANDSHAKE_VALID)
  378. return -EINVAL;
  379. if (strncmp(msg->tag, OCFS2_CONTROL_MESSAGE_DOWN_OP,
  380. OCFS2_CONTROL_MESSAGE_OP_LEN))
  381. return -EINVAL;
  382. if ((msg->space1 != ' ') || (msg->space2 != ' ') ||
  383. (msg->newline != '\n'))
  384. return -EINVAL;
  385. msg->space1 = msg->space2 = msg->newline = '\0';
  386. nodenum = simple_strtol(msg->nodestr, &p, 16);
  387. if (!p || *p)
  388. return -EINVAL;
  389. if ((nodenum == LONG_MIN) || (nodenum == LONG_MAX) ||
  390. (nodenum > INT_MAX) || (nodenum < 0))
  391. return -ERANGE;
  392. ocfs2_control_send_down(msg->uuid, nodenum);
  393. return 0;
  394. }
  395. static ssize_t ocfs2_control_message(struct file *file,
  396. const char __user *buf,
  397. size_t count)
  398. {
  399. ssize_t ret;
  400. union ocfs2_control_message msg;
  401. /* Try to catch padding issues */
  402. WARN_ON(offsetof(struct ocfs2_control_message_down, uuid) !=
  403. (sizeof(msg.u_down.tag) + sizeof(msg.u_down.space1)));
  404. memset(&msg, 0, sizeof(union ocfs2_control_message));
  405. ret = ocfs2_control_cfu(&msg, count, buf, count);
  406. if (ret)
  407. goto out;
  408. if ((count == OCFS2_CONTROL_MESSAGE_SETNODE_TOTAL_LEN) &&
  409. !strncmp(msg.tag, OCFS2_CONTROL_MESSAGE_SETNODE_OP,
  410. OCFS2_CONTROL_MESSAGE_OP_LEN))
  411. ret = ocfs2_control_do_setnode_msg(file, &msg.u_setn);
  412. else if ((count == OCFS2_CONTROL_MESSAGE_SETVERSION_TOTAL_LEN) &&
  413. !strncmp(msg.tag, OCFS2_CONTROL_MESSAGE_SETVERSION_OP,
  414. OCFS2_CONTROL_MESSAGE_OP_LEN))
  415. ret = ocfs2_control_do_setversion_msg(file, &msg.u_setv);
  416. else if ((count == OCFS2_CONTROL_MESSAGE_DOWN_TOTAL_LEN) &&
  417. !strncmp(msg.tag, OCFS2_CONTROL_MESSAGE_DOWN_OP,
  418. OCFS2_CONTROL_MESSAGE_OP_LEN))
  419. ret = ocfs2_control_do_down_msg(file, &msg.u_down);
  420. else
  421. ret = -EINVAL;
  422. out:
  423. return ret ? ret : count;
  424. }
  425. static ssize_t ocfs2_control_write(struct file *file,
  426. const char __user *buf,
  427. size_t count,
  428. loff_t *ppos)
  429. {
  430. ssize_t ret;
  431. switch (ocfs2_control_get_handshake_state(file)) {
  432. case OCFS2_CONTROL_HANDSHAKE_INVALID:
  433. ret = -EINVAL;
  434. break;
  435. case OCFS2_CONTROL_HANDSHAKE_READ:
  436. ret = ocfs2_control_validate_protocol(file, buf,
  437. count);
  438. break;
  439. case OCFS2_CONTROL_HANDSHAKE_PROTOCOL:
  440. case OCFS2_CONTROL_HANDSHAKE_VALID:
  441. ret = ocfs2_control_message(file, buf, count);
  442. break;
  443. default:
  444. BUG();
  445. ret = -EIO;
  446. break;
  447. }
  448. return ret;
  449. }
  450. /*
  451. * This is a naive version. If we ever have a new protocol, we'll expand
  452. * it. Probably using seq_file.
  453. */
  454. static ssize_t ocfs2_control_read(struct file *file,
  455. char __user *buf,
  456. size_t count,
  457. loff_t *ppos)
  458. {
  459. ssize_t ret;
  460. ret = simple_read_from_buffer(buf, count, ppos,
  461. OCFS2_CONTROL_PROTO, OCFS2_CONTROL_PROTO_LEN);
  462. /* Have we read the whole protocol list? */
  463. if (ret > 0 && *ppos >= OCFS2_CONTROL_PROTO_LEN)
  464. ocfs2_control_set_handshake_state(file,
  465. OCFS2_CONTROL_HANDSHAKE_READ);
  466. return ret;
  467. }
  468. static int ocfs2_control_release(struct inode *inode, struct file *file)
  469. {
  470. struct ocfs2_control_private *p = file->private_data;
  471. mutex_lock(&ocfs2_control_lock);
  472. if (ocfs2_control_get_handshake_state(file) !=
  473. OCFS2_CONTROL_HANDSHAKE_VALID)
  474. goto out;
  475. if (atomic_dec_and_test(&ocfs2_control_opened)) {
  476. if (!list_empty(&ocfs2_live_connection_list)) {
  477. /* XXX: Do bad things! */
  478. printk(KERN_ERR
  479. "ocfs2: Unexpected release of ocfs2_control!\n"
  480. " Loss of cluster connection requires "
  481. "an emergency restart!\n");
  482. emergency_restart();
  483. }
  484. /*
  485. * Last valid close clears the node number and resets
  486. * the locking protocol version
  487. */
  488. ocfs2_control_this_node = -1;
  489. running_proto.pv_major = 0;
  490. running_proto.pv_minor = 0;
  491. }
  492. out:
  493. list_del_init(&p->op_list);
  494. file->private_data = NULL;
  495. mutex_unlock(&ocfs2_control_lock);
  496. kfree(p);
  497. return 0;
  498. }
  499. static int ocfs2_control_open(struct inode *inode, struct file *file)
  500. {
  501. struct ocfs2_control_private *p;
  502. p = kzalloc(sizeof(struct ocfs2_control_private), GFP_KERNEL);
  503. if (!p)
  504. return -ENOMEM;
  505. p->op_this_node = -1;
  506. mutex_lock(&ocfs2_control_lock);
  507. file->private_data = p;
  508. list_add(&p->op_list, &ocfs2_control_private_list);
  509. mutex_unlock(&ocfs2_control_lock);
  510. return 0;
  511. }
  512. static const struct file_operations ocfs2_control_fops = {
  513. .open = ocfs2_control_open,
  514. .release = ocfs2_control_release,
  515. .read = ocfs2_control_read,
  516. .write = ocfs2_control_write,
  517. .owner = THIS_MODULE,
  518. .llseek = default_llseek,
  519. };
  520. static struct miscdevice ocfs2_control_device = {
  521. .minor = MISC_DYNAMIC_MINOR,
  522. .name = "ocfs2_control",
  523. .fops = &ocfs2_control_fops,
  524. };
  525. static int ocfs2_control_init(void)
  526. {
  527. int rc;
  528. atomic_set(&ocfs2_control_opened, 0);
  529. rc = misc_register(&ocfs2_control_device);
  530. if (rc)
  531. printk(KERN_ERR
  532. "ocfs2: Unable to register ocfs2_control device "
  533. "(errno %d)\n",
  534. -rc);
  535. return rc;
  536. }
  537. static void ocfs2_control_exit(void)
  538. {
  539. misc_deregister(&ocfs2_control_device);
  540. }
  541. static void fsdlm_lock_ast_wrapper(void *astarg)
  542. {
  543. struct ocfs2_dlm_lksb *lksb = astarg;
  544. int status = lksb->lksb_fsdlm.sb_status;
  545. /*
  546. * For now we're punting on the issue of other non-standard errors
  547. * where we can't tell if the unlock_ast or lock_ast should be called.
  548. * The main "other error" that's possible is EINVAL which means the
  549. * function was called with invalid args, which shouldn't be possible
  550. * since the caller here is under our control. Other non-standard
  551. * errors probably fall into the same category, or otherwise are fatal
  552. * which means we can't carry on anyway.
  553. */
  554. if (status == -DLM_EUNLOCK || status == -DLM_ECANCEL)
  555. lksb->lksb_conn->cc_proto->lp_unlock_ast(lksb, 0);
  556. else
  557. lksb->lksb_conn->cc_proto->lp_lock_ast(lksb);
  558. }
  559. static void fsdlm_blocking_ast_wrapper(void *astarg, int level)
  560. {
  561. struct ocfs2_dlm_lksb *lksb = astarg;
  562. lksb->lksb_conn->cc_proto->lp_blocking_ast(lksb, level);
  563. }
  564. static int user_dlm_lock(struct ocfs2_cluster_connection *conn,
  565. int mode,
  566. struct ocfs2_dlm_lksb *lksb,
  567. u32 flags,
  568. void *name,
  569. unsigned int namelen)
  570. {
  571. if (!lksb->lksb_fsdlm.sb_lvbptr)
  572. lksb->lksb_fsdlm.sb_lvbptr = (char *)lksb +
  573. sizeof(struct dlm_lksb);
  574. return dlm_lock(conn->cc_lockspace, mode, &lksb->lksb_fsdlm,
  575. flags|DLM_LKF_NODLCKWT, name, namelen, 0,
  576. fsdlm_lock_ast_wrapper, lksb,
  577. fsdlm_blocking_ast_wrapper);
  578. }
  579. static int user_dlm_unlock(struct ocfs2_cluster_connection *conn,
  580. struct ocfs2_dlm_lksb *lksb,
  581. u32 flags)
  582. {
  583. return dlm_unlock(conn->cc_lockspace, lksb->lksb_fsdlm.sb_lkid,
  584. flags, &lksb->lksb_fsdlm, lksb);
  585. }
  586. static int user_dlm_lock_status(struct ocfs2_dlm_lksb *lksb)
  587. {
  588. return lksb->lksb_fsdlm.sb_status;
  589. }
  590. static int user_dlm_lvb_valid(struct ocfs2_dlm_lksb *lksb)
  591. {
  592. int invalid = lksb->lksb_fsdlm.sb_flags & DLM_SBF_VALNOTVALID;
  593. return !invalid;
  594. }
  595. static void *user_dlm_lvb(struct ocfs2_dlm_lksb *lksb)
  596. {
  597. if (!lksb->lksb_fsdlm.sb_lvbptr)
  598. lksb->lksb_fsdlm.sb_lvbptr = (char *)lksb +
  599. sizeof(struct dlm_lksb);
  600. return (void *)(lksb->lksb_fsdlm.sb_lvbptr);
  601. }
  602. static void user_dlm_dump_lksb(struct ocfs2_dlm_lksb *lksb)
  603. {
  604. }
  605. static int user_plock(struct ocfs2_cluster_connection *conn,
  606. u64 ino,
  607. struct file *file,
  608. int cmd,
  609. struct file_lock *fl)
  610. {
  611. /*
  612. * This more or less just demuxes the plock request into any
  613. * one of three dlm calls.
  614. *
  615. * Internally, fs/dlm will pass these to a misc device, which
  616. * a userspace daemon will read and write to.
  617. */
  618. if (cmd == F_CANCELLK)
  619. return dlm_posix_cancel(conn->cc_lockspace, ino, file, fl);
  620. else if (IS_GETLK(cmd))
  621. return dlm_posix_get(conn->cc_lockspace, ino, file, fl);
  622. else if (lock_is_unlock(fl))
  623. return dlm_posix_unlock(conn->cc_lockspace, ino, file, fl);
  624. else
  625. return dlm_posix_lock(conn->cc_lockspace, ino, file, cmd, fl);
  626. }
  627. /*
  628. * Compare a requested locking protocol version against the current one.
  629. *
  630. * If the major numbers are different, they are incompatible.
  631. * If the current minor is greater than the request, they are incompatible.
  632. * If the current minor is less than or equal to the request, they are
  633. * compatible, and the requester should run at the current minor version.
  634. */
  635. static int fs_protocol_compare(struct ocfs2_protocol_version *existing,
  636. struct ocfs2_protocol_version *request)
  637. {
  638. if (existing->pv_major != request->pv_major)
  639. return 1;
  640. if (existing->pv_minor > request->pv_minor)
  641. return 1;
  642. if (existing->pv_minor < request->pv_minor)
  643. request->pv_minor = existing->pv_minor;
  644. return 0;
  645. }
  646. static void lvb_to_version(char *lvb, struct ocfs2_protocol_version *ver)
  647. {
  648. struct ocfs2_protocol_version *pv =
  649. (struct ocfs2_protocol_version *)lvb;
  650. /*
  651. * ocfs2_protocol_version has two u8 variables, so we don't
  652. * need any endian conversion.
  653. */
  654. ver->pv_major = pv->pv_major;
  655. ver->pv_minor = pv->pv_minor;
  656. }
  657. static void version_to_lvb(struct ocfs2_protocol_version *ver, char *lvb)
  658. {
  659. struct ocfs2_protocol_version *pv =
  660. (struct ocfs2_protocol_version *)lvb;
  661. /*
  662. * ocfs2_protocol_version has two u8 variables, so we don't
  663. * need any endian conversion.
  664. */
  665. pv->pv_major = ver->pv_major;
  666. pv->pv_minor = ver->pv_minor;
  667. }
  668. static void sync_wait_cb(void *arg)
  669. {
  670. struct ocfs2_cluster_connection *conn = arg;
  671. struct ocfs2_live_connection *lc = conn->cc_private;
  672. complete(&lc->oc_sync_wait);
  673. }
  674. static int sync_unlock(struct ocfs2_cluster_connection *conn,
  675. struct dlm_lksb *lksb, char *name)
  676. {
  677. int error;
  678. struct ocfs2_live_connection *lc = conn->cc_private;
  679. error = dlm_unlock(conn->cc_lockspace, lksb->sb_lkid, 0, lksb, conn);
  680. if (error) {
  681. printk(KERN_ERR "%s lkid %x error %d\n",
  682. name, lksb->sb_lkid, error);
  683. return error;
  684. }
  685. wait_for_completion(&lc->oc_sync_wait);
  686. if (lksb->sb_status != -DLM_EUNLOCK) {
  687. printk(KERN_ERR "%s lkid %x status %d\n",
  688. name, lksb->sb_lkid, lksb->sb_status);
  689. return -1;
  690. }
  691. return 0;
  692. }
  693. static int sync_lock(struct ocfs2_cluster_connection *conn,
  694. int mode, uint32_t flags,
  695. struct dlm_lksb *lksb, char *name)
  696. {
  697. int error, status;
  698. struct ocfs2_live_connection *lc = conn->cc_private;
  699. error = dlm_lock(conn->cc_lockspace, mode, lksb, flags,
  700. name, strlen(name),
  701. 0, sync_wait_cb, conn, NULL);
  702. if (error) {
  703. printk(KERN_ERR "%s lkid %x flags %x mode %d error %d\n",
  704. name, lksb->sb_lkid, flags, mode, error);
  705. return error;
  706. }
  707. wait_for_completion(&lc->oc_sync_wait);
  708. status = lksb->sb_status;
  709. if (status && status != -EAGAIN) {
  710. printk(KERN_ERR "%s lkid %x flags %x mode %d status %d\n",
  711. name, lksb->sb_lkid, flags, mode, status);
  712. }
  713. return status;
  714. }
  715. static int version_lock(struct ocfs2_cluster_connection *conn, int mode,
  716. int flags)
  717. {
  718. struct ocfs2_live_connection *lc = conn->cc_private;
  719. return sync_lock(conn, mode, flags,
  720. &lc->oc_version_lksb, VERSION_LOCK);
  721. }
  722. static int version_unlock(struct ocfs2_cluster_connection *conn)
  723. {
  724. struct ocfs2_live_connection *lc = conn->cc_private;
  725. return sync_unlock(conn, &lc->oc_version_lksb, VERSION_LOCK);
  726. }
  727. /* get_protocol_version()
  728. *
  729. * To exchange ocfs2 versioning, we use the LVB of the version dlm lock.
  730. * The algorithm is:
  731. * 1. Attempt to take the lock in EX mode (non-blocking).
  732. * 2. If successful (which means it is the first mount), write the
  733. * version number and downconvert to PR lock.
  734. * 3. If unsuccessful (returns -EAGAIN), read the version from the LVB after
  735. * taking the PR lock.
  736. */
  737. static int get_protocol_version(struct ocfs2_cluster_connection *conn)
  738. {
  739. int ret;
  740. struct ocfs2_live_connection *lc = conn->cc_private;
  741. struct ocfs2_protocol_version pv;
  742. running_proto.pv_major =
  743. ocfs2_user_plugin.sp_max_proto.pv_major;
  744. running_proto.pv_minor =
  745. ocfs2_user_plugin.sp_max_proto.pv_minor;
  746. lc->oc_version_lksb.sb_lvbptr = lc->oc_lvb;
  747. ret = version_lock(conn, DLM_LOCK_EX,
  748. DLM_LKF_VALBLK|DLM_LKF_NOQUEUE);
  749. if (!ret) {
  750. conn->cc_version.pv_major = running_proto.pv_major;
  751. conn->cc_version.pv_minor = running_proto.pv_minor;
  752. version_to_lvb(&running_proto, lc->oc_lvb);
  753. version_lock(conn, DLM_LOCK_PR, DLM_LKF_CONVERT|DLM_LKF_VALBLK);
  754. } else if (ret == -EAGAIN) {
  755. ret = version_lock(conn, DLM_LOCK_PR, DLM_LKF_VALBLK);
  756. if (ret)
  757. goto out;
  758. lvb_to_version(lc->oc_lvb, &pv);
  759. if ((pv.pv_major != running_proto.pv_major) ||
  760. (pv.pv_minor > running_proto.pv_minor)) {
  761. ret = -EINVAL;
  762. goto out;
  763. }
  764. conn->cc_version.pv_major = pv.pv_major;
  765. conn->cc_version.pv_minor = pv.pv_minor;
  766. }
  767. out:
  768. return ret;
  769. }
  770. static void user_recover_prep(void *arg)
  771. {
  772. }
  773. static void user_recover_slot(void *arg, struct dlm_slot *slot)
  774. {
  775. struct ocfs2_cluster_connection *conn = arg;
  776. printk(KERN_INFO "ocfs2: Node %d/%d down. Initiating recovery.\n",
  777. slot->nodeid, slot->slot);
  778. conn->cc_recovery_handler(slot->nodeid, conn->cc_recovery_data);
  779. }
  780. static void user_recover_done(void *arg, struct dlm_slot *slots,
  781. int num_slots, int our_slot,
  782. uint32_t generation)
  783. {
  784. struct ocfs2_cluster_connection *conn = arg;
  785. struct ocfs2_live_connection *lc = conn->cc_private;
  786. int i;
  787. for (i = 0; i < num_slots; i++)
  788. if (slots[i].slot == our_slot) {
  789. atomic_set(&lc->oc_this_node, slots[i].nodeid);
  790. break;
  791. }
  792. lc->oc_our_slot = our_slot;
  793. wake_up(&lc->oc_wait);
  794. }
  795. static const struct dlm_lockspace_ops ocfs2_ls_ops = {
  796. .recover_prep = user_recover_prep,
  797. .recover_slot = user_recover_slot,
  798. .recover_done = user_recover_done,
  799. };
  800. static int user_cluster_disconnect(struct ocfs2_cluster_connection *conn)
  801. {
  802. version_unlock(conn);
  803. dlm_release_lockspace(conn->cc_lockspace, 2);
  804. conn->cc_lockspace = NULL;
  805. ocfs2_live_connection_drop(conn->cc_private);
  806. conn->cc_private = NULL;
  807. return 0;
  808. }
  809. static int user_cluster_connect(struct ocfs2_cluster_connection *conn)
  810. {
  811. dlm_lockspace_t *fsdlm;
  812. struct ocfs2_live_connection *lc;
  813. int rc, ops_rv;
  814. BUG_ON(conn == NULL);
  815. lc = kzalloc(sizeof(struct ocfs2_live_connection), GFP_KERNEL);
  816. if (!lc)
  817. return -ENOMEM;
  818. init_waitqueue_head(&lc->oc_wait);
  819. init_completion(&lc->oc_sync_wait);
  820. atomic_set(&lc->oc_this_node, 0);
  821. conn->cc_private = lc;
  822. lc->oc_type = NO_CONTROLD;
  823. rc = dlm_new_lockspace(conn->cc_name, conn->cc_cluster_name,
  824. DLM_LSFL_NEWEXCL, DLM_LVB_LEN,
  825. &ocfs2_ls_ops, conn, &ops_rv, &fsdlm);
  826. if (rc) {
  827. if (rc == -EEXIST || rc == -EPROTO)
  828. printk(KERN_ERR "ocfs2: Unable to create the "
  829. "lockspace %s (%d), because a ocfs2-tools "
  830. "program is running on this file system "
  831. "with the same name lockspace\n",
  832. conn->cc_name, rc);
  833. goto out;
  834. }
  835. if (ops_rv == -EOPNOTSUPP) {
  836. lc->oc_type = WITH_CONTROLD;
  837. printk(KERN_NOTICE "ocfs2: You seem to be using an older "
  838. "version of dlm_controld and/or ocfs2-tools."
  839. " Please consider upgrading.\n");
  840. } else if (ops_rv) {
  841. rc = ops_rv;
  842. goto out;
  843. }
  844. conn->cc_lockspace = fsdlm;
  845. rc = ocfs2_live_connection_attach(conn, lc);
  846. if (rc)
  847. goto out;
  848. if (lc->oc_type == NO_CONTROLD) {
  849. rc = get_protocol_version(conn);
  850. if (rc) {
  851. printk(KERN_ERR "ocfs2: Could not determine"
  852. " locking version\n");
  853. user_cluster_disconnect(conn);
  854. lc = NULL;
  855. goto out;
  856. }
  857. wait_event(lc->oc_wait, (atomic_read(&lc->oc_this_node) > 0));
  858. }
  859. /*
  860. * running_proto must have been set before we allowed any mounts
  861. * to proceed.
  862. */
  863. if (fs_protocol_compare(&running_proto, &conn->cc_version)) {
  864. printk(KERN_ERR
  865. "Unable to mount with fs locking protocol version "
  866. "%u.%u because negotiated protocol is %u.%u\n",
  867. conn->cc_version.pv_major, conn->cc_version.pv_minor,
  868. running_proto.pv_major, running_proto.pv_minor);
  869. rc = -EPROTO;
  870. ocfs2_live_connection_drop(lc);
  871. lc = NULL;
  872. }
  873. out:
  874. if (rc)
  875. kfree(lc);
  876. return rc;
  877. }
  878. static int user_cluster_this_node(struct ocfs2_cluster_connection *conn,
  879. unsigned int *this_node)
  880. {
  881. int rc;
  882. struct ocfs2_live_connection *lc = conn->cc_private;
  883. if (lc->oc_type == WITH_CONTROLD)
  884. rc = ocfs2_control_get_this_node();
  885. else if (lc->oc_type == NO_CONTROLD)
  886. rc = atomic_read(&lc->oc_this_node);
  887. else
  888. rc = -EINVAL;
  889. if (rc < 0)
  890. return rc;
  891. *this_node = rc;
  892. return 0;
  893. }
  894. static const struct ocfs2_stack_operations ocfs2_user_plugin_ops = {
  895. .connect = user_cluster_connect,
  896. .disconnect = user_cluster_disconnect,
  897. .this_node = user_cluster_this_node,
  898. .dlm_lock = user_dlm_lock,
  899. .dlm_unlock = user_dlm_unlock,
  900. .lock_status = user_dlm_lock_status,
  901. .lvb_valid = user_dlm_lvb_valid,
  902. .lock_lvb = user_dlm_lvb,
  903. .plock = user_plock,
  904. .dump_lksb = user_dlm_dump_lksb,
  905. };
  906. static struct ocfs2_stack_plugin ocfs2_user_plugin = {
  907. .sp_name = "user",
  908. .sp_ops = &ocfs2_user_plugin_ops,
  909. .sp_owner = THIS_MODULE,
  910. };
  911. static int __init ocfs2_user_plugin_init(void)
  912. {
  913. int rc;
  914. rc = ocfs2_control_init();
  915. if (!rc) {
  916. rc = ocfs2_stack_glue_register(&ocfs2_user_plugin);
  917. if (rc)
  918. ocfs2_control_exit();
  919. }
  920. return rc;
  921. }
  922. static void __exit ocfs2_user_plugin_exit(void)
  923. {
  924. ocfs2_stack_glue_unregister(&ocfs2_user_plugin);
  925. ocfs2_control_exit();
  926. }
  927. MODULE_AUTHOR("Oracle");
  928. MODULE_DESCRIPTION("ocfs2 driver for userspace cluster stacks");
  929. MODULE_LICENSE("GPL");
  930. module_init(ocfs2_user_plugin_init);
  931. module_exit(ocfs2_user_plugin_exit);