rcom.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /******************************************************************************
  2. *******************************************************************************
  3. **
  4. ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  5. ** Copyright (C) 2005-2008 Red Hat, Inc. All rights reserved.
  6. **
  7. ** This copyrighted material is made available to anyone wishing to use,
  8. ** modify, copy, or redistribute it subject to the terms and conditions
  9. ** of the GNU General Public License v.2.
  10. **
  11. *******************************************************************************
  12. ******************************************************************************/
  13. #include "dlm_internal.h"
  14. #include "lockspace.h"
  15. #include "member.h"
  16. #include "lowcomms.h"
  17. #include "midcomms.h"
  18. #include "rcom.h"
  19. #include "recover.h"
  20. #include "dir.h"
  21. #include "config.h"
  22. #include "memory.h"
  23. #include "lock.h"
  24. #include "util.h"
  25. static int rcom_response(struct dlm_ls *ls)
  26. {
  27. return test_bit(LSFL_RCOM_READY, &ls->ls_flags);
  28. }
  29. static int create_rcom(struct dlm_ls *ls, int to_nodeid, int type, int len,
  30. struct dlm_rcom **rc_ret, struct dlm_mhandle **mh_ret)
  31. {
  32. struct dlm_rcom *rc;
  33. struct dlm_mhandle *mh;
  34. char *mb;
  35. int mb_len = sizeof(struct dlm_rcom) + len;
  36. mh = dlm_lowcomms_get_buffer(to_nodeid, mb_len, GFP_NOFS, &mb);
  37. if (!mh) {
  38. log_print("create_rcom to %d type %d len %d ENOBUFS",
  39. to_nodeid, type, len);
  40. return -ENOBUFS;
  41. }
  42. memset(mb, 0, mb_len);
  43. rc = (struct dlm_rcom *) mb;
  44. rc->rc_header.h_version = (DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
  45. rc->rc_header.h_lockspace = ls->ls_global_id;
  46. rc->rc_header.h_nodeid = dlm_our_nodeid();
  47. rc->rc_header.h_length = mb_len;
  48. rc->rc_header.h_cmd = DLM_RCOM;
  49. rc->rc_type = type;
  50. spin_lock(&ls->ls_recover_lock);
  51. rc->rc_seq = ls->ls_recover_seq;
  52. spin_unlock(&ls->ls_recover_lock);
  53. *mh_ret = mh;
  54. *rc_ret = rc;
  55. return 0;
  56. }
  57. static void send_rcom(struct dlm_ls *ls, struct dlm_mhandle *mh,
  58. struct dlm_rcom *rc)
  59. {
  60. dlm_rcom_out(rc);
  61. dlm_lowcomms_commit_buffer(mh);
  62. }
  63. /* When replying to a status request, a node also sends back its
  64. configuration values. The requesting node then checks that the remote
  65. node is configured the same way as itself. */
  66. static void make_config(struct dlm_ls *ls, struct rcom_config *rf)
  67. {
  68. rf->rf_lvblen = cpu_to_le32(ls->ls_lvblen);
  69. rf->rf_lsflags = cpu_to_le32(ls->ls_exflags);
  70. }
  71. static int check_config(struct dlm_ls *ls, struct dlm_rcom *rc, int nodeid)
  72. {
  73. struct rcom_config *rf = (struct rcom_config *) rc->rc_buf;
  74. size_t conf_size = sizeof(struct dlm_rcom) + sizeof(struct rcom_config);
  75. if ((rc->rc_header.h_version & 0xFFFF0000) != DLM_HEADER_MAJOR) {
  76. log_error(ls, "version mismatch: %x nodeid %d: %x",
  77. DLM_HEADER_MAJOR | DLM_HEADER_MINOR, nodeid,
  78. rc->rc_header.h_version);
  79. return -EPROTO;
  80. }
  81. if (rc->rc_header.h_length < conf_size) {
  82. log_error(ls, "config too short: %d nodeid %d",
  83. rc->rc_header.h_length, nodeid);
  84. return -EPROTO;
  85. }
  86. if (le32_to_cpu(rf->rf_lvblen) != ls->ls_lvblen ||
  87. le32_to_cpu(rf->rf_lsflags) != ls->ls_exflags) {
  88. log_error(ls, "config mismatch: %d,%x nodeid %d: %d,%x",
  89. ls->ls_lvblen, ls->ls_exflags, nodeid,
  90. le32_to_cpu(rf->rf_lvblen),
  91. le32_to_cpu(rf->rf_lsflags));
  92. return -EPROTO;
  93. }
  94. return 0;
  95. }
  96. static void allow_sync_reply(struct dlm_ls *ls, uint64_t *new_seq)
  97. {
  98. spin_lock(&ls->ls_rcom_spin);
  99. *new_seq = ++ls->ls_rcom_seq;
  100. set_bit(LSFL_RCOM_WAIT, &ls->ls_flags);
  101. spin_unlock(&ls->ls_rcom_spin);
  102. }
  103. static void disallow_sync_reply(struct dlm_ls *ls)
  104. {
  105. spin_lock(&ls->ls_rcom_spin);
  106. clear_bit(LSFL_RCOM_WAIT, &ls->ls_flags);
  107. clear_bit(LSFL_RCOM_READY, &ls->ls_flags);
  108. spin_unlock(&ls->ls_rcom_spin);
  109. }
  110. int dlm_rcom_status(struct dlm_ls *ls, int nodeid)
  111. {
  112. struct dlm_rcom *rc;
  113. struct dlm_mhandle *mh;
  114. int error = 0;
  115. ls->ls_recover_nodeid = nodeid;
  116. if (nodeid == dlm_our_nodeid()) {
  117. rc = ls->ls_recover_buf;
  118. rc->rc_result = dlm_recover_status(ls);
  119. goto out;
  120. }
  121. error = create_rcom(ls, nodeid, DLM_RCOM_STATUS, 0, &rc, &mh);
  122. if (error)
  123. goto out;
  124. allow_sync_reply(ls, &rc->rc_id);
  125. memset(ls->ls_recover_buf, 0, dlm_config.ci_buffer_size);
  126. send_rcom(ls, mh, rc);
  127. error = dlm_wait_function(ls, &rcom_response);
  128. disallow_sync_reply(ls);
  129. if (error)
  130. goto out;
  131. rc = ls->ls_recover_buf;
  132. if (rc->rc_result == -ESRCH) {
  133. /* we pretend the remote lockspace exists with 0 status */
  134. log_debug(ls, "remote node %d not ready", nodeid);
  135. rc->rc_result = 0;
  136. } else
  137. error = check_config(ls, rc, nodeid);
  138. /* the caller looks at rc_result for the remote recovery status */
  139. out:
  140. return error;
  141. }
  142. static void receive_rcom_status(struct dlm_ls *ls, struct dlm_rcom *rc_in)
  143. {
  144. struct dlm_rcom *rc;
  145. struct dlm_mhandle *mh;
  146. int error, nodeid = rc_in->rc_header.h_nodeid;
  147. error = create_rcom(ls, nodeid, DLM_RCOM_STATUS_REPLY,
  148. sizeof(struct rcom_config), &rc, &mh);
  149. if (error)
  150. return;
  151. rc->rc_id = rc_in->rc_id;
  152. rc->rc_seq_reply = rc_in->rc_seq;
  153. rc->rc_result = dlm_recover_status(ls);
  154. make_config(ls, (struct rcom_config *) rc->rc_buf);
  155. send_rcom(ls, mh, rc);
  156. }
  157. static void receive_sync_reply(struct dlm_ls *ls, struct dlm_rcom *rc_in)
  158. {
  159. spin_lock(&ls->ls_rcom_spin);
  160. if (!test_bit(LSFL_RCOM_WAIT, &ls->ls_flags) ||
  161. rc_in->rc_id != ls->ls_rcom_seq) {
  162. log_debug(ls, "reject reply %d from %d seq %llx expect %llx",
  163. rc_in->rc_type, rc_in->rc_header.h_nodeid,
  164. (unsigned long long)rc_in->rc_id,
  165. (unsigned long long)ls->ls_rcom_seq);
  166. goto out;
  167. }
  168. memcpy(ls->ls_recover_buf, rc_in, rc_in->rc_header.h_length);
  169. set_bit(LSFL_RCOM_READY, &ls->ls_flags);
  170. clear_bit(LSFL_RCOM_WAIT, &ls->ls_flags);
  171. wake_up(&ls->ls_wait_general);
  172. out:
  173. spin_unlock(&ls->ls_rcom_spin);
  174. }
  175. int dlm_rcom_names(struct dlm_ls *ls, int nodeid, char *last_name, int last_len)
  176. {
  177. struct dlm_rcom *rc;
  178. struct dlm_mhandle *mh;
  179. int error = 0;
  180. int max_size = dlm_config.ci_buffer_size - sizeof(struct dlm_rcom);
  181. ls->ls_recover_nodeid = nodeid;
  182. if (nodeid == dlm_our_nodeid()) {
  183. ls->ls_recover_buf->rc_header.h_length =
  184. dlm_config.ci_buffer_size;
  185. dlm_copy_master_names(ls, last_name, last_len,
  186. ls->ls_recover_buf->rc_buf,
  187. max_size, nodeid);
  188. goto out;
  189. }
  190. error = create_rcom(ls, nodeid, DLM_RCOM_NAMES, last_len, &rc, &mh);
  191. if (error)
  192. goto out;
  193. memcpy(rc->rc_buf, last_name, last_len);
  194. allow_sync_reply(ls, &rc->rc_id);
  195. memset(ls->ls_recover_buf, 0, dlm_config.ci_buffer_size);
  196. send_rcom(ls, mh, rc);
  197. error = dlm_wait_function(ls, &rcom_response);
  198. disallow_sync_reply(ls);
  199. out:
  200. return error;
  201. }
  202. static void receive_rcom_names(struct dlm_ls *ls, struct dlm_rcom *rc_in)
  203. {
  204. struct dlm_rcom *rc;
  205. struct dlm_mhandle *mh;
  206. int error, inlen, outlen, nodeid;
  207. nodeid = rc_in->rc_header.h_nodeid;
  208. inlen = rc_in->rc_header.h_length - sizeof(struct dlm_rcom);
  209. outlen = dlm_config.ci_buffer_size - sizeof(struct dlm_rcom);
  210. error = create_rcom(ls, nodeid, DLM_RCOM_NAMES_REPLY, outlen, &rc, &mh);
  211. if (error)
  212. return;
  213. rc->rc_id = rc_in->rc_id;
  214. rc->rc_seq_reply = rc_in->rc_seq;
  215. dlm_copy_master_names(ls, rc_in->rc_buf, inlen, rc->rc_buf, outlen,
  216. nodeid);
  217. send_rcom(ls, mh, rc);
  218. }
  219. int dlm_send_rcom_lookup(struct dlm_rsb *r, int dir_nodeid)
  220. {
  221. struct dlm_rcom *rc;
  222. struct dlm_mhandle *mh;
  223. struct dlm_ls *ls = r->res_ls;
  224. int error;
  225. error = create_rcom(ls, dir_nodeid, DLM_RCOM_LOOKUP, r->res_length,
  226. &rc, &mh);
  227. if (error)
  228. goto out;
  229. memcpy(rc->rc_buf, r->res_name, r->res_length);
  230. rc->rc_id = (unsigned long) r;
  231. send_rcom(ls, mh, rc);
  232. out:
  233. return error;
  234. }
  235. static void receive_rcom_lookup(struct dlm_ls *ls, struct dlm_rcom *rc_in)
  236. {
  237. struct dlm_rcom *rc;
  238. struct dlm_mhandle *mh;
  239. int error, ret_nodeid, nodeid = rc_in->rc_header.h_nodeid;
  240. int len = rc_in->rc_header.h_length - sizeof(struct dlm_rcom);
  241. error = create_rcom(ls, nodeid, DLM_RCOM_LOOKUP_REPLY, 0, &rc, &mh);
  242. if (error)
  243. return;
  244. error = dlm_dir_lookup(ls, nodeid, rc_in->rc_buf, len, &ret_nodeid);
  245. if (error)
  246. ret_nodeid = error;
  247. rc->rc_result = ret_nodeid;
  248. rc->rc_id = rc_in->rc_id;
  249. rc->rc_seq_reply = rc_in->rc_seq;
  250. send_rcom(ls, mh, rc);
  251. }
  252. static void receive_rcom_lookup_reply(struct dlm_ls *ls, struct dlm_rcom *rc_in)
  253. {
  254. dlm_recover_master_reply(ls, rc_in);
  255. }
  256. static void pack_rcom_lock(struct dlm_rsb *r, struct dlm_lkb *lkb,
  257. struct rcom_lock *rl)
  258. {
  259. memset(rl, 0, sizeof(*rl));
  260. rl->rl_ownpid = cpu_to_le32(lkb->lkb_ownpid);
  261. rl->rl_lkid = cpu_to_le32(lkb->lkb_id);
  262. rl->rl_exflags = cpu_to_le32(lkb->lkb_exflags);
  263. rl->rl_flags = cpu_to_le32(lkb->lkb_flags);
  264. rl->rl_lvbseq = cpu_to_le32(lkb->lkb_lvbseq);
  265. rl->rl_rqmode = lkb->lkb_rqmode;
  266. rl->rl_grmode = lkb->lkb_grmode;
  267. rl->rl_status = lkb->lkb_status;
  268. rl->rl_wait_type = cpu_to_le16(lkb->lkb_wait_type);
  269. if (lkb->lkb_bastfn)
  270. rl->rl_asts |= DLM_CB_BAST;
  271. if (lkb->lkb_astfn)
  272. rl->rl_asts |= DLM_CB_CAST;
  273. rl->rl_namelen = cpu_to_le16(r->res_length);
  274. memcpy(rl->rl_name, r->res_name, r->res_length);
  275. /* FIXME: might we have an lvb without DLM_LKF_VALBLK set ?
  276. If so, receive_rcom_lock_args() won't take this copy. */
  277. if (lkb->lkb_lvbptr)
  278. memcpy(rl->rl_lvb, lkb->lkb_lvbptr, r->res_ls->ls_lvblen);
  279. }
  280. int dlm_send_rcom_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
  281. {
  282. struct dlm_ls *ls = r->res_ls;
  283. struct dlm_rcom *rc;
  284. struct dlm_mhandle *mh;
  285. struct rcom_lock *rl;
  286. int error, len = sizeof(struct rcom_lock);
  287. if (lkb->lkb_lvbptr)
  288. len += ls->ls_lvblen;
  289. error = create_rcom(ls, r->res_nodeid, DLM_RCOM_LOCK, len, &rc, &mh);
  290. if (error)
  291. goto out;
  292. rl = (struct rcom_lock *) rc->rc_buf;
  293. pack_rcom_lock(r, lkb, rl);
  294. rc->rc_id = (unsigned long) r;
  295. send_rcom(ls, mh, rc);
  296. out:
  297. return error;
  298. }
  299. /* needs at least dlm_rcom + rcom_lock */
  300. static void receive_rcom_lock(struct dlm_ls *ls, struct dlm_rcom *rc_in)
  301. {
  302. struct dlm_rcom *rc;
  303. struct dlm_mhandle *mh;
  304. int error, nodeid = rc_in->rc_header.h_nodeid;
  305. dlm_recover_master_copy(ls, rc_in);
  306. error = create_rcom(ls, nodeid, DLM_RCOM_LOCK_REPLY,
  307. sizeof(struct rcom_lock), &rc, &mh);
  308. if (error)
  309. return;
  310. /* We send back the same rcom_lock struct we received, but
  311. dlm_recover_master_copy() has filled in rl_remid and rl_result */
  312. memcpy(rc->rc_buf, rc_in->rc_buf, sizeof(struct rcom_lock));
  313. rc->rc_id = rc_in->rc_id;
  314. rc->rc_seq_reply = rc_in->rc_seq;
  315. send_rcom(ls, mh, rc);
  316. }
  317. /* If the lockspace doesn't exist then still send a status message
  318. back; it's possible that it just doesn't have its global_id yet. */
  319. int dlm_send_ls_not_ready(int nodeid, struct dlm_rcom *rc_in)
  320. {
  321. struct dlm_rcom *rc;
  322. struct rcom_config *rf;
  323. struct dlm_mhandle *mh;
  324. char *mb;
  325. int mb_len = sizeof(struct dlm_rcom) + sizeof(struct rcom_config);
  326. mh = dlm_lowcomms_get_buffer(nodeid, mb_len, GFP_NOFS, &mb);
  327. if (!mh)
  328. return -ENOBUFS;
  329. memset(mb, 0, mb_len);
  330. rc = (struct dlm_rcom *) mb;
  331. rc->rc_header.h_version = (DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
  332. rc->rc_header.h_lockspace = rc_in->rc_header.h_lockspace;
  333. rc->rc_header.h_nodeid = dlm_our_nodeid();
  334. rc->rc_header.h_length = mb_len;
  335. rc->rc_header.h_cmd = DLM_RCOM;
  336. rc->rc_type = DLM_RCOM_STATUS_REPLY;
  337. rc->rc_id = rc_in->rc_id;
  338. rc->rc_seq_reply = rc_in->rc_seq;
  339. rc->rc_result = -ESRCH;
  340. rf = (struct rcom_config *) rc->rc_buf;
  341. rf->rf_lvblen = cpu_to_le32(~0U);
  342. dlm_rcom_out(rc);
  343. dlm_lowcomms_commit_buffer(mh);
  344. return 0;
  345. }
  346. static int is_old_reply(struct dlm_ls *ls, struct dlm_rcom *rc)
  347. {
  348. uint64_t seq;
  349. int rv = 0;
  350. switch (rc->rc_type) {
  351. case DLM_RCOM_STATUS_REPLY:
  352. case DLM_RCOM_NAMES_REPLY:
  353. case DLM_RCOM_LOOKUP_REPLY:
  354. case DLM_RCOM_LOCK_REPLY:
  355. spin_lock(&ls->ls_recover_lock);
  356. seq = ls->ls_recover_seq;
  357. spin_unlock(&ls->ls_recover_lock);
  358. if (rc->rc_seq_reply != seq) {
  359. log_debug(ls, "ignoring old reply %x from %d "
  360. "seq_reply %llx expect %llx",
  361. rc->rc_type, rc->rc_header.h_nodeid,
  362. (unsigned long long)rc->rc_seq_reply,
  363. (unsigned long long)seq);
  364. rv = 1;
  365. }
  366. }
  367. return rv;
  368. }
  369. /* Called by dlm_recv; corresponds to dlm_receive_message() but special
  370. recovery-only comms are sent through here. */
  371. void dlm_receive_rcom(struct dlm_ls *ls, struct dlm_rcom *rc, int nodeid)
  372. {
  373. int lock_size = sizeof(struct dlm_rcom) + sizeof(struct rcom_lock);
  374. if (dlm_recovery_stopped(ls) && (rc->rc_type != DLM_RCOM_STATUS)) {
  375. log_debug(ls, "ignoring recovery message %x from %d",
  376. rc->rc_type, nodeid);
  377. goto out;
  378. }
  379. if (is_old_reply(ls, rc))
  380. goto out;
  381. switch (rc->rc_type) {
  382. case DLM_RCOM_STATUS:
  383. receive_rcom_status(ls, rc);
  384. break;
  385. case DLM_RCOM_NAMES:
  386. receive_rcom_names(ls, rc);
  387. break;
  388. case DLM_RCOM_LOOKUP:
  389. receive_rcom_lookup(ls, rc);
  390. break;
  391. case DLM_RCOM_LOCK:
  392. if (rc->rc_header.h_length < lock_size)
  393. goto Eshort;
  394. receive_rcom_lock(ls, rc);
  395. break;
  396. case DLM_RCOM_STATUS_REPLY:
  397. receive_sync_reply(ls, rc);
  398. break;
  399. case DLM_RCOM_NAMES_REPLY:
  400. receive_sync_reply(ls, rc);
  401. break;
  402. case DLM_RCOM_LOOKUP_REPLY:
  403. receive_rcom_lookup_reply(ls, rc);
  404. break;
  405. case DLM_RCOM_LOCK_REPLY:
  406. if (rc->rc_header.h_length < lock_size)
  407. goto Eshort;
  408. dlm_recover_process_copy(ls, rc);
  409. break;
  410. default:
  411. log_error(ls, "receive_rcom bad type %d", rc->rc_type);
  412. }
  413. out:
  414. return;
  415. Eshort:
  416. log_error(ls, "recovery message %x from %d is too short",
  417. rc->rc_type, nodeid);
  418. }