rcom.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /******************************************************************************
  2. *******************************************************************************
  3. **
  4. ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  5. ** Copyright (C) 2005 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_KERNEL, &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. *mh_ret = mh;
  51. *rc_ret = rc;
  52. return 0;
  53. }
  54. static void send_rcom(struct dlm_ls *ls, struct dlm_mhandle *mh,
  55. struct dlm_rcom *rc)
  56. {
  57. dlm_rcom_out(rc);
  58. dlm_lowcomms_commit_buffer(mh);
  59. }
  60. /* When replying to a status request, a node also sends back its
  61. configuration values. The requesting node then checks that the remote
  62. node is configured the same way as itself. */
  63. static void make_config(struct dlm_ls *ls, struct rcom_config *rf)
  64. {
  65. rf->rf_lvblen = ls->ls_lvblen;
  66. rf->rf_lsflags = ls->ls_exflags;
  67. }
  68. static int check_config(struct dlm_ls *ls, struct rcom_config *rf, int nodeid)
  69. {
  70. if (rf->rf_lvblen != ls->ls_lvblen ||
  71. rf->rf_lsflags != ls->ls_exflags) {
  72. log_error(ls, "config mismatch: %d,%x nodeid %d: %d,%x",
  73. ls->ls_lvblen, ls->ls_exflags,
  74. nodeid, rf->rf_lvblen, rf->rf_lsflags);
  75. return -EINVAL;
  76. }
  77. return 0;
  78. }
  79. int dlm_rcom_status(struct dlm_ls *ls, int nodeid)
  80. {
  81. struct dlm_rcom *rc;
  82. struct dlm_mhandle *mh;
  83. int error = 0;
  84. memset(ls->ls_recover_buf, 0, dlm_config.buffer_size);
  85. ls->ls_recover_nodeid = nodeid;
  86. if (nodeid == dlm_our_nodeid()) {
  87. rc = (struct dlm_rcom *) ls->ls_recover_buf;
  88. rc->rc_result = dlm_recover_status(ls);
  89. goto out;
  90. }
  91. error = create_rcom(ls, nodeid, DLM_RCOM_STATUS, 0, &rc, &mh);
  92. if (error)
  93. goto out;
  94. rc->rc_id = ++ls->ls_rcom_seq;
  95. send_rcom(ls, mh, rc);
  96. error = dlm_wait_function(ls, &rcom_response);
  97. clear_bit(LSFL_RCOM_READY, &ls->ls_flags);
  98. if (error)
  99. goto out;
  100. rc = (struct dlm_rcom *) ls->ls_recover_buf;
  101. if (rc->rc_result == -ESRCH) {
  102. /* we pretend the remote lockspace exists with 0 status */
  103. log_debug(ls, "remote node %d not ready", nodeid);
  104. rc->rc_result = 0;
  105. } else
  106. error = check_config(ls, (struct rcom_config *) rc->rc_buf,
  107. nodeid);
  108. /* the caller looks at rc_result for the remote recovery status */
  109. out:
  110. return error;
  111. }
  112. static void receive_rcom_status(struct dlm_ls *ls, struct dlm_rcom *rc_in)
  113. {
  114. struct dlm_rcom *rc;
  115. struct dlm_mhandle *mh;
  116. int error, nodeid = rc_in->rc_header.h_nodeid;
  117. error = create_rcom(ls, nodeid, DLM_RCOM_STATUS_REPLY,
  118. sizeof(struct rcom_config), &rc, &mh);
  119. if (error)
  120. return;
  121. rc->rc_id = rc_in->rc_id;
  122. rc->rc_result = dlm_recover_status(ls);
  123. make_config(ls, (struct rcom_config *) rc->rc_buf);
  124. send_rcom(ls, mh, rc);
  125. }
  126. static void receive_sync_reply(struct dlm_ls *ls, struct dlm_rcom *rc_in)
  127. {
  128. if (rc_in->rc_id != ls->ls_rcom_seq) {
  129. log_debug(ls, "reject old reply %d got %llx wanted %llx",
  130. rc_in->rc_type, rc_in->rc_id, ls->ls_rcom_seq);
  131. return;
  132. }
  133. memcpy(ls->ls_recover_buf, rc_in, rc_in->rc_header.h_length);
  134. set_bit(LSFL_RCOM_READY, &ls->ls_flags);
  135. wake_up(&ls->ls_wait_general);
  136. }
  137. static void receive_rcom_status_reply(struct dlm_ls *ls, struct dlm_rcom *rc_in)
  138. {
  139. receive_sync_reply(ls, rc_in);
  140. }
  141. int dlm_rcom_names(struct dlm_ls *ls, int nodeid, char *last_name, int last_len)
  142. {
  143. struct dlm_rcom *rc;
  144. struct dlm_mhandle *mh;
  145. int error = 0, len = sizeof(struct dlm_rcom);
  146. memset(ls->ls_recover_buf, 0, dlm_config.buffer_size);
  147. ls->ls_recover_nodeid = nodeid;
  148. if (nodeid == dlm_our_nodeid()) {
  149. dlm_copy_master_names(ls, last_name, last_len,
  150. ls->ls_recover_buf + len,
  151. dlm_config.buffer_size - len, nodeid);
  152. goto out;
  153. }
  154. error = create_rcom(ls, nodeid, DLM_RCOM_NAMES, last_len, &rc, &mh);
  155. if (error)
  156. goto out;
  157. memcpy(rc->rc_buf, last_name, last_len);
  158. rc->rc_id = ++ls->ls_rcom_seq;
  159. send_rcom(ls, mh, rc);
  160. error = dlm_wait_function(ls, &rcom_response);
  161. clear_bit(LSFL_RCOM_READY, &ls->ls_flags);
  162. out:
  163. return error;
  164. }
  165. static void receive_rcom_names(struct dlm_ls *ls, struct dlm_rcom *rc_in)
  166. {
  167. struct dlm_rcom *rc;
  168. struct dlm_mhandle *mh;
  169. int error, inlen, outlen;
  170. int nodeid = rc_in->rc_header.h_nodeid;
  171. uint32_t status = dlm_recover_status(ls);
  172. /*
  173. * We can't run dlm_dir_rebuild_send (which uses ls_nodes) while
  174. * dlm_recoverd is running ls_nodes_reconfig (which changes ls_nodes).
  175. * It could only happen in rare cases where we get a late NAMES
  176. * message from a previous instance of recovery.
  177. */
  178. if (!(status & DLM_RS_NODES)) {
  179. log_debug(ls, "ignoring RCOM_NAMES from %u", nodeid);
  180. return;
  181. }
  182. nodeid = rc_in->rc_header.h_nodeid;
  183. inlen = rc_in->rc_header.h_length - sizeof(struct dlm_rcom);
  184. outlen = dlm_config.buffer_size - sizeof(struct dlm_rcom);
  185. error = create_rcom(ls, nodeid, DLM_RCOM_NAMES_REPLY, outlen, &rc, &mh);
  186. if (error)
  187. return;
  188. rc->rc_id = rc_in->rc_id;
  189. dlm_copy_master_names(ls, rc_in->rc_buf, inlen, rc->rc_buf, outlen,
  190. nodeid);
  191. send_rcom(ls, mh, rc);
  192. }
  193. static void receive_rcom_names_reply(struct dlm_ls *ls, struct dlm_rcom *rc_in)
  194. {
  195. receive_sync_reply(ls, rc_in);
  196. }
  197. int dlm_send_rcom_lookup(struct dlm_rsb *r, int dir_nodeid)
  198. {
  199. struct dlm_rcom *rc;
  200. struct dlm_mhandle *mh;
  201. struct dlm_ls *ls = r->res_ls;
  202. int error;
  203. error = create_rcom(ls, dir_nodeid, DLM_RCOM_LOOKUP, r->res_length,
  204. &rc, &mh);
  205. if (error)
  206. goto out;
  207. memcpy(rc->rc_buf, r->res_name, r->res_length);
  208. rc->rc_id = (unsigned long) r;
  209. send_rcom(ls, mh, rc);
  210. out:
  211. return error;
  212. }
  213. static void receive_rcom_lookup(struct dlm_ls *ls, struct dlm_rcom *rc_in)
  214. {
  215. struct dlm_rcom *rc;
  216. struct dlm_mhandle *mh;
  217. int error, ret_nodeid, nodeid = rc_in->rc_header.h_nodeid;
  218. int len = rc_in->rc_header.h_length - sizeof(struct dlm_rcom);
  219. error = create_rcom(ls, nodeid, DLM_RCOM_LOOKUP_REPLY, 0, &rc, &mh);
  220. if (error)
  221. return;
  222. error = dlm_dir_lookup(ls, nodeid, rc_in->rc_buf, len, &ret_nodeid);
  223. if (error)
  224. ret_nodeid = error;
  225. rc->rc_result = ret_nodeid;
  226. rc->rc_id = rc_in->rc_id;
  227. send_rcom(ls, mh, rc);
  228. }
  229. static void receive_rcom_lookup_reply(struct dlm_ls *ls, struct dlm_rcom *rc_in)
  230. {
  231. dlm_recover_master_reply(ls, rc_in);
  232. }
  233. static void pack_rcom_lock(struct dlm_rsb *r, struct dlm_lkb *lkb,
  234. struct rcom_lock *rl)
  235. {
  236. memset(rl, 0, sizeof(*rl));
  237. rl->rl_ownpid = lkb->lkb_ownpid;
  238. rl->rl_lkid = lkb->lkb_id;
  239. rl->rl_exflags = lkb->lkb_exflags;
  240. rl->rl_flags = lkb->lkb_flags;
  241. rl->rl_lvbseq = lkb->lkb_lvbseq;
  242. rl->rl_rqmode = lkb->lkb_rqmode;
  243. rl->rl_grmode = lkb->lkb_grmode;
  244. rl->rl_status = lkb->lkb_status;
  245. rl->rl_wait_type = lkb->lkb_wait_type;
  246. if (lkb->lkb_bastaddr)
  247. rl->rl_asts |= AST_BAST;
  248. if (lkb->lkb_astaddr)
  249. rl->rl_asts |= AST_COMP;
  250. rl->rl_namelen = r->res_length;
  251. memcpy(rl->rl_name, r->res_name, r->res_length);
  252. /* FIXME: might we have an lvb without DLM_LKF_VALBLK set ?
  253. If so, receive_rcom_lock_args() won't take this copy. */
  254. if (lkb->lkb_lvbptr)
  255. memcpy(rl->rl_lvb, lkb->lkb_lvbptr, r->res_ls->ls_lvblen);
  256. }
  257. int dlm_send_rcom_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
  258. {
  259. struct dlm_ls *ls = r->res_ls;
  260. struct dlm_rcom *rc;
  261. struct dlm_mhandle *mh;
  262. struct rcom_lock *rl;
  263. int error, len = sizeof(struct rcom_lock);
  264. if (lkb->lkb_lvbptr)
  265. len += ls->ls_lvblen;
  266. error = create_rcom(ls, r->res_nodeid, DLM_RCOM_LOCK, len, &rc, &mh);
  267. if (error)
  268. goto out;
  269. rl = (struct rcom_lock *) rc->rc_buf;
  270. pack_rcom_lock(r, lkb, rl);
  271. rc->rc_id = (unsigned long) r;
  272. send_rcom(ls, mh, rc);
  273. out:
  274. return error;
  275. }
  276. static void receive_rcom_lock(struct dlm_ls *ls, struct dlm_rcom *rc_in)
  277. {
  278. struct dlm_rcom *rc;
  279. struct dlm_mhandle *mh;
  280. int error, nodeid = rc_in->rc_header.h_nodeid;
  281. dlm_recover_master_copy(ls, rc_in);
  282. error = create_rcom(ls, nodeid, DLM_RCOM_LOCK_REPLY,
  283. sizeof(struct rcom_lock), &rc, &mh);
  284. if (error)
  285. return;
  286. /* We send back the same rcom_lock struct we received, but
  287. dlm_recover_master_copy() has filled in rl_remid and rl_result */
  288. memcpy(rc->rc_buf, rc_in->rc_buf, sizeof(struct rcom_lock));
  289. rc->rc_id = rc_in->rc_id;
  290. send_rcom(ls, mh, rc);
  291. }
  292. static void receive_rcom_lock_reply(struct dlm_ls *ls, struct dlm_rcom *rc_in)
  293. {
  294. uint32_t status = dlm_recover_status(ls);
  295. if (!(status & DLM_RS_DIR)) {
  296. log_debug(ls, "ignoring RCOM_LOCK_REPLY from %u",
  297. rc_in->rc_header.h_nodeid);
  298. return;
  299. }
  300. dlm_recover_process_copy(ls, rc_in);
  301. }
  302. static int send_ls_not_ready(int nodeid, struct dlm_rcom *rc_in)
  303. {
  304. struct dlm_rcom *rc;
  305. struct dlm_mhandle *mh;
  306. char *mb;
  307. int mb_len = sizeof(struct dlm_rcom);
  308. mh = dlm_lowcomms_get_buffer(nodeid, mb_len, GFP_KERNEL, &mb);
  309. if (!mh)
  310. return -ENOBUFS;
  311. memset(mb, 0, mb_len);
  312. rc = (struct dlm_rcom *) mb;
  313. rc->rc_header.h_version = (DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
  314. rc->rc_header.h_lockspace = rc_in->rc_header.h_lockspace;
  315. rc->rc_header.h_nodeid = dlm_our_nodeid();
  316. rc->rc_header.h_length = mb_len;
  317. rc->rc_header.h_cmd = DLM_RCOM;
  318. rc->rc_type = DLM_RCOM_STATUS_REPLY;
  319. rc->rc_id = rc_in->rc_id;
  320. rc->rc_result = -ESRCH;
  321. dlm_rcom_out(rc);
  322. dlm_lowcomms_commit_buffer(mh);
  323. return 0;
  324. }
  325. /* Called by dlm_recvd; corresponds to dlm_receive_message() but special
  326. recovery-only comms are sent through here. */
  327. void dlm_receive_rcom(struct dlm_header *hd, int nodeid)
  328. {
  329. struct dlm_rcom *rc = (struct dlm_rcom *) hd;
  330. struct dlm_ls *ls;
  331. dlm_rcom_in(rc);
  332. /* If the lockspace doesn't exist then still send a status message
  333. back; it's possible that it just doesn't have its global_id yet. */
  334. ls = dlm_find_lockspace_global(hd->h_lockspace);
  335. if (!ls) {
  336. log_print("lockspace %x from %d not found",
  337. hd->h_lockspace, nodeid);
  338. send_ls_not_ready(nodeid, rc);
  339. return;
  340. }
  341. if (dlm_recovery_stopped(ls) && (rc->rc_type != DLM_RCOM_STATUS)) {
  342. log_error(ls, "ignoring recovery message %x from %d",
  343. rc->rc_type, nodeid);
  344. goto out;
  345. }
  346. if (nodeid != rc->rc_header.h_nodeid) {
  347. log_error(ls, "bad rcom nodeid %d from %d",
  348. rc->rc_header.h_nodeid, nodeid);
  349. goto out;
  350. }
  351. switch (rc->rc_type) {
  352. case DLM_RCOM_STATUS:
  353. receive_rcom_status(ls, rc);
  354. break;
  355. case DLM_RCOM_NAMES:
  356. receive_rcom_names(ls, rc);
  357. break;
  358. case DLM_RCOM_LOOKUP:
  359. receive_rcom_lookup(ls, rc);
  360. break;
  361. case DLM_RCOM_LOCK:
  362. receive_rcom_lock(ls, rc);
  363. break;
  364. case DLM_RCOM_STATUS_REPLY:
  365. receive_rcom_status_reply(ls, rc);
  366. break;
  367. case DLM_RCOM_NAMES_REPLY:
  368. receive_rcom_names_reply(ls, rc);
  369. break;
  370. case DLM_RCOM_LOOKUP_REPLY:
  371. receive_rcom_lookup_reply(ls, rc);
  372. break;
  373. case DLM_RCOM_LOCK_REPLY:
  374. receive_rcom_lock_reply(ls, rc);
  375. break;
  376. default:
  377. DLM_ASSERT(0, printk("rc_type=%x\n", rc->rc_type););
  378. }
  379. out:
  380. dlm_put_lockspace(ls);
  381. }