rcom.c 11 KB

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