cpg.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369
  1. /*
  2. * vi: set autoindent tabstop=4 shiftwidth=4 :
  3. *
  4. * Copyright (c) 2006-2015 Red Hat, Inc.
  5. *
  6. * All rights reserved.
  7. *
  8. * Author: Christine Caulfield (ccaulfi@redhat.com)
  9. * Author: Jan Friesse (jfriesse@redhat.com)
  10. *
  11. * This software licensed under BSD license, the text of which follows:
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. * - Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  22. * contributors may be used to endorse or promote products derived from this
  23. * software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  29. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  35. * THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. /*
  38. * Provides a closed process group API using the coroipcc executive
  39. */
  40. #include <config.h>
  41. #include <stdlib.h>
  42. #include <stdio.h>
  43. #include <string.h>
  44. #include <unistd.h>
  45. #include <sys/types.h>
  46. #include <sys/socket.h>
  47. #include <sys/mman.h>
  48. #include <sys/uio.h>
  49. #include <sys/stat.h>
  50. #include <errno.h>
  51. #include <limits.h>
  52. #include <qb/qbdefs.h>
  53. #include <qb/qbipcc.h>
  54. #include <qb/qblog.h>
  55. #include <corosync/hdb.h>
  56. #include <corosync/list.h>
  57. #include <corosync/corotypes.h>
  58. #include <corosync/corodefs.h>
  59. #include <corosync/cpg.h>
  60. #include <corosync/ipc_cpg.h>
  61. #include "util.h"
  62. #ifndef MAP_ANONYMOUS
  63. #define MAP_ANONYMOUS MAP_ANON
  64. #endif
  65. /*
  66. * Maximum number of times to retry a send when transmitting
  67. * a large message fragment
  68. */
  69. #define MAX_RETRIES 100
  70. /*
  71. * ZCB files have following umask (umask is same as used in libqb)
  72. */
  73. #define CPG_MEMORY_MAP_UMASK 077
  74. struct cpg_inst {
  75. qb_ipcc_connection_t *c;
  76. int finalize;
  77. void *context;
  78. union {
  79. cpg_model_data_t model_data;
  80. cpg_model_v1_data_t model_v1_data;
  81. };
  82. struct list_head iteration_list_head;
  83. uint32_t max_msg_size;
  84. char *assembly_buf;
  85. uint32_t assembly_buf_ptr;
  86. int assembling; /* Flag that says we have started assembling a message.
  87. * It's here to catch the situation where a node joins
  88. * the cluster/group in the middle of a CPG message send
  89. * so we don't pass on a partial message to the client.
  90. */
  91. };
  92. static void cpg_inst_free (void *inst);
  93. DECLARE_HDB_DATABASE(cpg_handle_t_db, cpg_inst_free);
  94. struct cpg_iteration_instance_t {
  95. cpg_iteration_handle_t cpg_iteration_handle;
  96. qb_ipcc_connection_t *conn;
  97. hdb_handle_t executive_iteration_handle;
  98. struct list_head list;
  99. };
  100. DECLARE_HDB_DATABASE(cpg_iteration_handle_t_db,NULL);
  101. /*
  102. * Internal (not visible by API) functions
  103. */
  104. static cs_error_t
  105. coroipcc_msg_send_reply_receive (
  106. qb_ipcc_connection_t *c,
  107. const struct iovec *iov,
  108. unsigned int iov_len,
  109. void *res_msg,
  110. size_t res_len)
  111. {
  112. return qb_to_cs_error(qb_ipcc_sendv_recv(c, iov, iov_len, res_msg, res_len,
  113. CS_IPC_TIMEOUT_MS));
  114. }
  115. static void cpg_iteration_instance_finalize (struct cpg_iteration_instance_t *cpg_iteration_instance)
  116. {
  117. list_del (&cpg_iteration_instance->list);
  118. hdb_handle_destroy (&cpg_iteration_handle_t_db, cpg_iteration_instance->cpg_iteration_handle);
  119. }
  120. static void cpg_inst_free (void *inst)
  121. {
  122. struct cpg_inst *cpg_inst = (struct cpg_inst *)inst;
  123. qb_ipcc_disconnect(cpg_inst->c);
  124. }
  125. static void cpg_inst_finalize (struct cpg_inst *cpg_inst, hdb_handle_t handle)
  126. {
  127. struct list_head *iter, *iter_next;
  128. struct cpg_iteration_instance_t *cpg_iteration_instance;
  129. /*
  130. * Traverse thru iteration instances and delete them
  131. */
  132. for (iter = cpg_inst->iteration_list_head.next; iter != &cpg_inst->iteration_list_head;iter = iter_next) {
  133. iter_next = iter->next;
  134. cpg_iteration_instance = list_entry (iter, struct cpg_iteration_instance_t, list);
  135. cpg_iteration_instance_finalize (cpg_iteration_instance);
  136. }
  137. hdb_handle_destroy (&cpg_handle_t_db, handle);
  138. }
  139. /**
  140. * @defgroup cpg_coroipcc The closed process group API
  141. * @ingroup coroipcc
  142. *
  143. * @{
  144. */
  145. cs_error_t cpg_initialize (
  146. cpg_handle_t *handle,
  147. cpg_callbacks_t *callbacks)
  148. {
  149. cpg_model_v1_data_t model_v1_data;
  150. memset (&model_v1_data, 0, sizeof (cpg_model_v1_data_t));
  151. if (callbacks) {
  152. model_v1_data.cpg_deliver_fn = callbacks->cpg_deliver_fn;
  153. model_v1_data.cpg_confchg_fn = callbacks->cpg_confchg_fn;
  154. }
  155. return (cpg_model_initialize (handle, CPG_MODEL_V1, (cpg_model_data_t *)&model_v1_data, NULL));
  156. }
  157. cs_error_t cpg_model_initialize (
  158. cpg_handle_t *handle,
  159. cpg_model_t model,
  160. cpg_model_data_t *model_data,
  161. void *context)
  162. {
  163. cs_error_t error;
  164. struct cpg_inst *cpg_inst;
  165. if (model != CPG_MODEL_V1) {
  166. error = CS_ERR_INVALID_PARAM;
  167. goto error_no_destroy;
  168. }
  169. error = hdb_error_to_cs (hdb_handle_create (&cpg_handle_t_db, sizeof (struct cpg_inst), handle));
  170. if (error != CS_OK) {
  171. goto error_no_destroy;
  172. }
  173. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, *handle, (void *)&cpg_inst));
  174. if (error != CS_OK) {
  175. goto error_destroy;
  176. }
  177. cpg_inst->c = qb_ipcc_connect ("cpg", IPC_REQUEST_SIZE);
  178. if (cpg_inst->c == NULL) {
  179. error = qb_to_cs_error(-errno);
  180. goto error_put_destroy;
  181. }
  182. if (model_data != NULL) {
  183. switch (model) {
  184. case CPG_MODEL_V1:
  185. memcpy (&cpg_inst->model_v1_data, model_data, sizeof (cpg_model_v1_data_t));
  186. if ((cpg_inst->model_v1_data.flags & ~(CPG_MODEL_V1_DELIVER_INITIAL_TOTEM_CONF)) != 0) {
  187. error = CS_ERR_INVALID_PARAM;
  188. goto error_destroy;
  189. }
  190. break;
  191. }
  192. }
  193. /* Allow space for corosync internal headers */
  194. cpg_inst->max_msg_size = IPC_REQUEST_SIZE - 1024;
  195. cpg_inst->model_data.model = model;
  196. cpg_inst->context = context;
  197. list_init(&cpg_inst->iteration_list_head);
  198. hdb_handle_put (&cpg_handle_t_db, *handle);
  199. return (CS_OK);
  200. error_put_destroy:
  201. hdb_handle_put (&cpg_handle_t_db, *handle);
  202. error_destroy:
  203. hdb_handle_destroy (&cpg_handle_t_db, *handle);
  204. error_no_destroy:
  205. return (error);
  206. }
  207. cs_error_t cpg_finalize (
  208. cpg_handle_t handle)
  209. {
  210. struct cpg_inst *cpg_inst;
  211. struct iovec iov;
  212. struct req_lib_cpg_finalize req_lib_cpg_finalize;
  213. struct res_lib_cpg_finalize res_lib_cpg_finalize;
  214. cs_error_t error;
  215. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  216. if (error != CS_OK) {
  217. return (error);
  218. }
  219. /*
  220. * Another thread has already started finalizing
  221. */
  222. if (cpg_inst->finalize) {
  223. hdb_handle_put (&cpg_handle_t_db, handle);
  224. return (CS_ERR_BAD_HANDLE);
  225. }
  226. cpg_inst->finalize = 1;
  227. /*
  228. * Send service request
  229. */
  230. req_lib_cpg_finalize.header.size = sizeof (struct req_lib_cpg_finalize);
  231. req_lib_cpg_finalize.header.id = MESSAGE_REQ_CPG_FINALIZE;
  232. iov.iov_base = (void *)&req_lib_cpg_finalize;
  233. iov.iov_len = sizeof (struct req_lib_cpg_finalize);
  234. error = coroipcc_msg_send_reply_receive (cpg_inst->c,
  235. &iov,
  236. 1,
  237. &res_lib_cpg_finalize,
  238. sizeof (struct res_lib_cpg_finalize));
  239. cpg_inst_finalize (cpg_inst, handle);
  240. hdb_handle_put (&cpg_handle_t_db, handle);
  241. return (error);
  242. }
  243. cs_error_t cpg_fd_get (
  244. cpg_handle_t handle,
  245. int *fd)
  246. {
  247. cs_error_t error;
  248. struct cpg_inst *cpg_inst;
  249. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  250. if (error != CS_OK) {
  251. return (error);
  252. }
  253. error = qb_to_cs_error (qb_ipcc_fd_get (cpg_inst->c, fd));
  254. hdb_handle_put (&cpg_handle_t_db, handle);
  255. return (error);
  256. }
  257. cs_error_t cpg_max_atomic_msgsize_get (
  258. cpg_handle_t handle,
  259. uint32_t *size)
  260. {
  261. cs_error_t error;
  262. struct cpg_inst *cpg_inst;
  263. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  264. if (error != CS_OK) {
  265. return (error);
  266. }
  267. *size = cpg_inst->max_msg_size;
  268. hdb_handle_put (&cpg_handle_t_db, handle);
  269. return (error);
  270. }
  271. cs_error_t cpg_context_get (
  272. cpg_handle_t handle,
  273. void **context)
  274. {
  275. cs_error_t error;
  276. struct cpg_inst *cpg_inst;
  277. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  278. if (error != CS_OK) {
  279. return (error);
  280. }
  281. *context = cpg_inst->context;
  282. hdb_handle_put (&cpg_handle_t_db, handle);
  283. return (CS_OK);
  284. }
  285. cs_error_t cpg_context_set (
  286. cpg_handle_t handle,
  287. void *context)
  288. {
  289. cs_error_t error;
  290. struct cpg_inst *cpg_inst;
  291. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  292. if (error != CS_OK) {
  293. return (error);
  294. }
  295. cpg_inst->context = context;
  296. hdb_handle_put (&cpg_handle_t_db, handle);
  297. return (CS_OK);
  298. }
  299. cs_error_t cpg_dispatch (
  300. cpg_handle_t handle,
  301. cs_dispatch_flags_t dispatch_types)
  302. {
  303. int timeout = -1;
  304. cs_error_t error;
  305. int cont = 1; /* always continue do loop except when set to 0 */
  306. struct cpg_inst *cpg_inst;
  307. struct res_lib_cpg_confchg_callback *res_cpg_confchg_callback;
  308. struct res_lib_cpg_deliver_callback *res_cpg_deliver_callback;
  309. struct res_lib_cpg_partial_deliver_callback *res_cpg_partial_deliver_callback;
  310. struct res_lib_cpg_totem_confchg_callback *res_cpg_totem_confchg_callback;
  311. struct cpg_inst cpg_inst_copy;
  312. struct qb_ipc_response_header *dispatch_data;
  313. struct cpg_address member_list[CPG_MEMBERS_MAX];
  314. struct cpg_address left_list[CPG_MEMBERS_MAX];
  315. struct cpg_address joined_list[CPG_MEMBERS_MAX];
  316. struct cpg_name group_name;
  317. mar_cpg_address_t *left_list_start;
  318. mar_cpg_address_t *joined_list_start;
  319. unsigned int i;
  320. struct cpg_ring_id ring_id;
  321. uint32_t totem_member_list[CPG_MEMBERS_MAX];
  322. int32_t errno_res;
  323. char dispatch_buf[IPC_DISPATCH_SIZE];
  324. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  325. if (error != CS_OK) {
  326. return (error);
  327. }
  328. /*
  329. * Timeout instantly for CS_DISPATCH_ONE_NONBLOCKING or CS_DISPATCH_ALL and
  330. * wait indefinitely for CS_DISPATCH_ONE or CS_DISPATCH_BLOCKING
  331. */
  332. if (dispatch_types == CS_DISPATCH_ALL || dispatch_types == CS_DISPATCH_ONE_NONBLOCKING) {
  333. timeout = 0;
  334. }
  335. dispatch_data = (struct qb_ipc_response_header *)dispatch_buf;
  336. do {
  337. errno_res = qb_ipcc_event_recv (
  338. cpg_inst->c,
  339. dispatch_buf,
  340. IPC_DISPATCH_SIZE,
  341. timeout);
  342. error = qb_to_cs_error (errno_res);
  343. if (error == CS_ERR_BAD_HANDLE) {
  344. error = CS_OK;
  345. goto error_put;
  346. }
  347. if (error == CS_ERR_TRY_AGAIN) {
  348. if (dispatch_types == CS_DISPATCH_ONE_NONBLOCKING) {
  349. /*
  350. * Don't mask error
  351. */
  352. goto error_put;
  353. }
  354. error = CS_OK;
  355. if (dispatch_types == CS_DISPATCH_ALL) {
  356. break; /* exit do while cont is 1 loop */
  357. } else {
  358. continue; /* next poll */
  359. }
  360. }
  361. if (error != CS_OK) {
  362. goto error_put;
  363. }
  364. /*
  365. * Make copy of callbacks, message data, unlock instance, and call callback
  366. * A risk of this dispatch method is that the callback routines may
  367. * operate at the same time that cpgFinalize has been called.
  368. */
  369. memcpy (&cpg_inst_copy, cpg_inst, sizeof (struct cpg_inst));
  370. switch (cpg_inst_copy.model_data.model) {
  371. case CPG_MODEL_V1:
  372. /*
  373. * Dispatch incoming message
  374. */
  375. switch (dispatch_data->id) {
  376. case MESSAGE_RES_CPG_DELIVER_CALLBACK:
  377. if (cpg_inst_copy.model_v1_data.cpg_deliver_fn == NULL) {
  378. break;
  379. }
  380. res_cpg_deliver_callback = (struct res_lib_cpg_deliver_callback *)dispatch_data;
  381. marshall_from_mar_cpg_name_t (
  382. &group_name,
  383. &res_cpg_deliver_callback->group_name);
  384. cpg_inst_copy.model_v1_data.cpg_deliver_fn (handle,
  385. &group_name,
  386. res_cpg_deliver_callback->nodeid,
  387. res_cpg_deliver_callback->pid,
  388. &res_cpg_deliver_callback->message,
  389. res_cpg_deliver_callback->msglen);
  390. break;
  391. case MESSAGE_RES_CPG_PARTIAL_DELIVER_CALLBACK:
  392. res_cpg_partial_deliver_callback = (struct res_lib_cpg_partial_deliver_callback *)dispatch_data;
  393. marshall_from_mar_cpg_name_t (
  394. &group_name,
  395. &res_cpg_partial_deliver_callback->group_name);
  396. if (res_cpg_partial_deliver_callback->type == LIBCPG_PARTIAL_FIRST) {
  397. /*
  398. * Allocate a buffer to contain a full message.
  399. */
  400. cpg_inst->assembly_buf = malloc(res_cpg_partial_deliver_callback->msglen);
  401. if (!cpg_inst->assembly_buf) {
  402. error = CS_ERR_NO_MEMORY;
  403. goto error_put;
  404. }
  405. cpg_inst->assembling = 1;
  406. cpg_inst->assembly_buf_ptr = 0;
  407. }
  408. if (cpg_inst->assembling) {
  409. memcpy(cpg_inst->assembly_buf + cpg_inst->assembly_buf_ptr,
  410. res_cpg_partial_deliver_callback->message, res_cpg_partial_deliver_callback->fraglen);
  411. cpg_inst->assembly_buf_ptr += res_cpg_partial_deliver_callback->fraglen;
  412. if (res_cpg_partial_deliver_callback->type == LIBCPG_PARTIAL_LAST) {
  413. cpg_inst_copy.model_v1_data.cpg_deliver_fn (handle,
  414. &group_name,
  415. res_cpg_partial_deliver_callback->nodeid,
  416. res_cpg_partial_deliver_callback->pid,
  417. cpg_inst->assembly_buf,
  418. res_cpg_partial_deliver_callback->msglen);
  419. free(cpg_inst->assembly_buf);
  420. cpg_inst->assembling = 0;
  421. }
  422. }
  423. break;
  424. case MESSAGE_RES_CPG_CONFCHG_CALLBACK:
  425. if (cpg_inst_copy.model_v1_data.cpg_confchg_fn == NULL) {
  426. break;
  427. }
  428. res_cpg_confchg_callback = (struct res_lib_cpg_confchg_callback *)dispatch_data;
  429. for (i = 0; i < res_cpg_confchg_callback->member_list_entries; i++) {
  430. marshall_from_mar_cpg_address_t (&member_list[i],
  431. &res_cpg_confchg_callback->member_list[i]);
  432. }
  433. left_list_start = res_cpg_confchg_callback->member_list +
  434. res_cpg_confchg_callback->member_list_entries;
  435. for (i = 0; i < res_cpg_confchg_callback->left_list_entries; i++) {
  436. marshall_from_mar_cpg_address_t (&left_list[i],
  437. &left_list_start[i]);
  438. }
  439. joined_list_start = res_cpg_confchg_callback->member_list +
  440. res_cpg_confchg_callback->member_list_entries +
  441. res_cpg_confchg_callback->left_list_entries;
  442. for (i = 0; i < res_cpg_confchg_callback->joined_list_entries; i++) {
  443. marshall_from_mar_cpg_address_t (&joined_list[i],
  444. &joined_list_start[i]);
  445. }
  446. marshall_from_mar_cpg_name_t (
  447. &group_name,
  448. &res_cpg_confchg_callback->group_name);
  449. cpg_inst_copy.model_v1_data.cpg_confchg_fn (handle,
  450. &group_name,
  451. member_list,
  452. res_cpg_confchg_callback->member_list_entries,
  453. left_list,
  454. res_cpg_confchg_callback->left_list_entries,
  455. joined_list,
  456. res_cpg_confchg_callback->joined_list_entries);
  457. break;
  458. case MESSAGE_RES_CPG_TOTEM_CONFCHG_CALLBACK:
  459. if (cpg_inst_copy.model_v1_data.cpg_totem_confchg_fn == NULL) {
  460. break;
  461. }
  462. res_cpg_totem_confchg_callback = (struct res_lib_cpg_totem_confchg_callback *)dispatch_data;
  463. marshall_from_mar_cpg_ring_id_t (&ring_id, &res_cpg_totem_confchg_callback->ring_id);
  464. for (i = 0; i < res_cpg_totem_confchg_callback->member_list_entries; i++) {
  465. totem_member_list[i] = res_cpg_totem_confchg_callback->member_list[i];
  466. }
  467. cpg_inst_copy.model_v1_data.cpg_totem_confchg_fn (handle,
  468. ring_id,
  469. res_cpg_totem_confchg_callback->member_list_entries,
  470. totem_member_list);
  471. break;
  472. default:
  473. error = CS_ERR_LIBRARY;
  474. goto error_put;
  475. break;
  476. } /* - switch (dispatch_data->id) */
  477. break; /* case CPG_MODEL_V1 */
  478. } /* - switch (cpg_inst_copy.model_data.model) */
  479. if (cpg_inst_copy.finalize || cpg_inst->finalize) {
  480. /*
  481. * If the finalize has been called then get out of the dispatch.
  482. */
  483. cpg_inst->finalize = 1;
  484. error = CS_ERR_BAD_HANDLE;
  485. goto error_put;
  486. }
  487. /*
  488. * Determine if more messages should be processed
  489. */
  490. if (dispatch_types == CS_DISPATCH_ONE || dispatch_types == CS_DISPATCH_ONE_NONBLOCKING) {
  491. cont = 0;
  492. }
  493. } while (cont);
  494. error_put:
  495. hdb_handle_put (&cpg_handle_t_db, handle);
  496. return (error);
  497. }
  498. cs_error_t cpg_join (
  499. cpg_handle_t handle,
  500. const struct cpg_name *group)
  501. {
  502. cs_error_t error;
  503. struct cpg_inst *cpg_inst;
  504. struct iovec iov[2];
  505. struct req_lib_cpg_join req_lib_cpg_join;
  506. struct res_lib_cpg_join response;
  507. if (group->length > CPG_MAX_NAME_LENGTH) {
  508. return (CS_ERR_NAME_TOO_LONG);
  509. }
  510. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  511. if (error != CS_OK) {
  512. return (error);
  513. }
  514. /* Now join */
  515. req_lib_cpg_join.header.size = sizeof (struct req_lib_cpg_join);
  516. req_lib_cpg_join.header.id = MESSAGE_REQ_CPG_JOIN;
  517. req_lib_cpg_join.pid = getpid();
  518. req_lib_cpg_join.flags = 0;
  519. switch (cpg_inst->model_data.model) {
  520. case CPG_MODEL_V1:
  521. req_lib_cpg_join.flags = cpg_inst->model_v1_data.flags;
  522. break;
  523. }
  524. marshall_to_mar_cpg_name_t (&req_lib_cpg_join.group_name,
  525. group);
  526. iov[0].iov_base = (void *)&req_lib_cpg_join;
  527. iov[0].iov_len = sizeof (struct req_lib_cpg_join);
  528. do {
  529. error = coroipcc_msg_send_reply_receive (cpg_inst->c, iov, 1,
  530. &response, sizeof (struct res_lib_cpg_join));
  531. if (error != CS_OK) {
  532. goto error_exit;
  533. }
  534. } while (response.header.error == CS_ERR_BUSY);
  535. error = response.header.error;
  536. error_exit:
  537. hdb_handle_put (&cpg_handle_t_db, handle);
  538. return (error);
  539. }
  540. cs_error_t cpg_leave (
  541. cpg_handle_t handle,
  542. const struct cpg_name *group)
  543. {
  544. cs_error_t error;
  545. struct cpg_inst *cpg_inst;
  546. struct iovec iov[2];
  547. struct req_lib_cpg_leave req_lib_cpg_leave;
  548. struct res_lib_cpg_leave res_lib_cpg_leave;
  549. if (group->length > CPG_MAX_NAME_LENGTH) {
  550. return (CS_ERR_NAME_TOO_LONG);
  551. }
  552. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  553. if (error != CS_OK) {
  554. return (error);
  555. }
  556. req_lib_cpg_leave.header.size = sizeof (struct req_lib_cpg_leave);
  557. req_lib_cpg_leave.header.id = MESSAGE_REQ_CPG_LEAVE;
  558. req_lib_cpg_leave.pid = getpid();
  559. marshall_to_mar_cpg_name_t (&req_lib_cpg_leave.group_name,
  560. group);
  561. iov[0].iov_base = (void *)&req_lib_cpg_leave;
  562. iov[0].iov_len = sizeof (struct req_lib_cpg_leave);
  563. do {
  564. error = coroipcc_msg_send_reply_receive (cpg_inst->c, iov, 1,
  565. &res_lib_cpg_leave, sizeof (struct res_lib_cpg_leave));
  566. if (error != CS_OK) {
  567. goto error_exit;
  568. }
  569. } while (res_lib_cpg_leave.header.error == CS_ERR_BUSY);
  570. error = res_lib_cpg_leave.header.error;
  571. error_exit:
  572. hdb_handle_put (&cpg_handle_t_db, handle);
  573. return (error);
  574. }
  575. cs_error_t cpg_membership_get (
  576. cpg_handle_t handle,
  577. struct cpg_name *group_name,
  578. struct cpg_address *member_list,
  579. int *member_list_entries)
  580. {
  581. cs_error_t error;
  582. struct cpg_inst *cpg_inst;
  583. struct iovec iov;
  584. struct req_lib_cpg_membership_get req_lib_cpg_membership_get;
  585. struct res_lib_cpg_membership_get res_lib_cpg_membership_get;
  586. unsigned int i;
  587. if (group_name->length > CPG_MAX_NAME_LENGTH) {
  588. return (CS_ERR_NAME_TOO_LONG);
  589. }
  590. if (member_list == NULL) {
  591. return (CS_ERR_INVALID_PARAM);
  592. }
  593. if (member_list_entries == NULL) {
  594. return (CS_ERR_INVALID_PARAM);
  595. }
  596. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  597. if (error != CS_OK) {
  598. return (error);
  599. }
  600. req_lib_cpg_membership_get.header.size = sizeof (struct req_lib_cpg_membership_get);
  601. req_lib_cpg_membership_get.header.id = MESSAGE_REQ_CPG_MEMBERSHIP;
  602. marshall_to_mar_cpg_name_t (&req_lib_cpg_membership_get.group_name,
  603. group_name);
  604. iov.iov_base = (void *)&req_lib_cpg_membership_get;
  605. iov.iov_len = sizeof (struct req_lib_cpg_membership_get);
  606. error = coroipcc_msg_send_reply_receive (cpg_inst->c, &iov, 1,
  607. &res_lib_cpg_membership_get, sizeof (res_lib_cpg_membership_get));
  608. if (error != CS_OK) {
  609. goto error_exit;
  610. }
  611. error = res_lib_cpg_membership_get.header.error;
  612. /*
  613. * Copy results to caller
  614. */
  615. *member_list_entries = res_lib_cpg_membership_get.member_count;
  616. if (member_list) {
  617. for (i = 0; i < res_lib_cpg_membership_get.member_count; i++) {
  618. marshall_from_mar_cpg_address_t (&member_list[i],
  619. &res_lib_cpg_membership_get.member_list[i]);
  620. }
  621. }
  622. error_exit:
  623. hdb_handle_put (&cpg_handle_t_db, handle);
  624. return (error);
  625. }
  626. cs_error_t cpg_local_get (
  627. cpg_handle_t handle,
  628. unsigned int *local_nodeid)
  629. {
  630. cs_error_t error;
  631. struct cpg_inst *cpg_inst;
  632. struct iovec iov;
  633. struct req_lib_cpg_local_get req_lib_cpg_local_get;
  634. struct res_lib_cpg_local_get res_lib_cpg_local_get;
  635. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  636. if (error != CS_OK) {
  637. return (error);
  638. }
  639. req_lib_cpg_local_get.header.size = sizeof (struct qb_ipc_request_header);
  640. req_lib_cpg_local_get.header.id = MESSAGE_REQ_CPG_LOCAL_GET;
  641. iov.iov_base = (void *)&req_lib_cpg_local_get;
  642. iov.iov_len = sizeof (struct req_lib_cpg_local_get);
  643. error = coroipcc_msg_send_reply_receive (cpg_inst->c, &iov, 1,
  644. &res_lib_cpg_local_get, sizeof (res_lib_cpg_local_get));
  645. if (error != CS_OK) {
  646. goto error_exit;
  647. }
  648. error = res_lib_cpg_local_get.header.error;
  649. *local_nodeid = res_lib_cpg_local_get.local_nodeid;
  650. error_exit:
  651. hdb_handle_put (&cpg_handle_t_db, handle);
  652. return (error);
  653. }
  654. cs_error_t cpg_flow_control_state_get (
  655. cpg_handle_t handle,
  656. cpg_flow_control_state_t *flow_control_state)
  657. {
  658. cs_error_t error;
  659. struct cpg_inst *cpg_inst;
  660. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  661. if (error != CS_OK) {
  662. return (error);
  663. }
  664. *flow_control_state = CPG_FLOW_CONTROL_DISABLED;
  665. error = CS_OK;
  666. hdb_handle_put (&cpg_handle_t_db, handle);
  667. return (error);
  668. }
  669. static int
  670. memory_map (char *path, const char *file, void **buf, size_t bytes)
  671. {
  672. int32_t fd;
  673. void *addr;
  674. int32_t res;
  675. char *buffer;
  676. int32_t i;
  677. size_t written;
  678. size_t page_size;
  679. long int sysconf_page_size;
  680. mode_t old_umask;
  681. snprintf (path, PATH_MAX, "/dev/shm/%s", file);
  682. old_umask = umask(CPG_MEMORY_MAP_UMASK);
  683. fd = mkstemp (path);
  684. (void)umask(old_umask);
  685. if (fd == -1) {
  686. snprintf (path, PATH_MAX, LOCALSTATEDIR "/run/%s", file);
  687. old_umask = umask(CPG_MEMORY_MAP_UMASK);
  688. fd = mkstemp (path);
  689. (void)umask(old_umask);
  690. if (fd == -1) {
  691. return (-1);
  692. }
  693. }
  694. res = ftruncate (fd, bytes);
  695. if (res == -1) {
  696. goto error_close_unlink;
  697. }
  698. sysconf_page_size = sysconf(_SC_PAGESIZE);
  699. if (sysconf_page_size <= 0) {
  700. goto error_close_unlink;
  701. }
  702. page_size = sysconf_page_size;
  703. buffer = malloc (page_size);
  704. if (buffer == NULL) {
  705. goto error_close_unlink;
  706. }
  707. memset (buffer, 0, page_size);
  708. for (i = 0; i < (bytes / page_size); i++) {
  709. retry_write:
  710. written = write (fd, buffer, page_size);
  711. if (written == -1 && errno == EINTR) {
  712. goto retry_write;
  713. }
  714. if (written != page_size) {
  715. free (buffer);
  716. goto error_close_unlink;
  717. }
  718. }
  719. free (buffer);
  720. addr = mmap (NULL, bytes, PROT_READ | PROT_WRITE,
  721. MAP_SHARED, fd, 0);
  722. if (addr == MAP_FAILED) {
  723. goto error_close_unlink;
  724. }
  725. #ifdef MADV_NOSYNC
  726. madvise(addr, bytes, MADV_NOSYNC);
  727. #endif
  728. res = close (fd);
  729. if (res) {
  730. return (-1);
  731. }
  732. *buf = addr;
  733. return 0;
  734. error_close_unlink:
  735. close (fd);
  736. unlink(path);
  737. return -1;
  738. }
  739. cs_error_t cpg_zcb_alloc (
  740. cpg_handle_t handle,
  741. size_t size,
  742. void **buffer)
  743. {
  744. void *buf = NULL;
  745. char path[PATH_MAX];
  746. mar_req_coroipcc_zc_alloc_t req_coroipcc_zc_alloc;
  747. struct qb_ipc_response_header res_coroipcs_zc_alloc;
  748. size_t map_size;
  749. struct iovec iovec;
  750. struct coroipcs_zc_header *hdr;
  751. cs_error_t error;
  752. struct cpg_inst *cpg_inst;
  753. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  754. if (error != CS_OK) {
  755. return (error);
  756. }
  757. map_size = size + sizeof (struct req_lib_cpg_mcast) + sizeof (struct coroipcs_zc_header);
  758. assert(memory_map (path, "corosync_zerocopy-XXXXXX", &buf, map_size) != -1);
  759. if (strlen(path) >= CPG_ZC_PATH_LEN) {
  760. unlink(path);
  761. munmap (buf, map_size);
  762. return (CS_ERR_NAME_TOO_LONG);
  763. }
  764. req_coroipcc_zc_alloc.header.size = sizeof (mar_req_coroipcc_zc_alloc_t);
  765. req_coroipcc_zc_alloc.header.id = MESSAGE_REQ_CPG_ZC_ALLOC;
  766. req_coroipcc_zc_alloc.map_size = map_size;
  767. strcpy (req_coroipcc_zc_alloc.path_to_file, path);
  768. iovec.iov_base = (void *)&req_coroipcc_zc_alloc;
  769. iovec.iov_len = sizeof (mar_req_coroipcc_zc_alloc_t);
  770. error = coroipcc_msg_send_reply_receive (
  771. cpg_inst->c,
  772. &iovec,
  773. 1,
  774. &res_coroipcs_zc_alloc,
  775. sizeof (struct qb_ipc_response_header));
  776. if (error != CS_OK) {
  777. goto error_exit;
  778. }
  779. hdr = (struct coroipcs_zc_header *)buf;
  780. hdr->map_size = map_size;
  781. *buffer = ((char *)buf) + sizeof (struct coroipcs_zc_header) + sizeof (struct req_lib_cpg_mcast);
  782. error_exit:
  783. hdb_handle_put (&cpg_handle_t_db, handle);
  784. return (error);
  785. }
  786. cs_error_t cpg_zcb_free (
  787. cpg_handle_t handle,
  788. void *buffer)
  789. {
  790. cs_error_t error;
  791. unsigned int res;
  792. struct cpg_inst *cpg_inst;
  793. mar_req_coroipcc_zc_free_t req_coroipcc_zc_free;
  794. struct qb_ipc_response_header res_coroipcs_zc_free;
  795. struct iovec iovec;
  796. struct coroipcs_zc_header *header = (struct coroipcs_zc_header *)((char *)buffer - sizeof (struct coroipcs_zc_header) - sizeof (struct req_lib_cpg_mcast));
  797. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  798. if (error != CS_OK) {
  799. return (error);
  800. }
  801. req_coroipcc_zc_free.header.size = sizeof (mar_req_coroipcc_zc_free_t);
  802. req_coroipcc_zc_free.header.id = MESSAGE_REQ_CPG_ZC_FREE;
  803. req_coroipcc_zc_free.map_size = header->map_size;
  804. req_coroipcc_zc_free.server_address = header->server_address;
  805. iovec.iov_base = (void *)&req_coroipcc_zc_free;
  806. iovec.iov_len = sizeof (mar_req_coroipcc_zc_free_t);
  807. error = coroipcc_msg_send_reply_receive (
  808. cpg_inst->c,
  809. &iovec,
  810. 1,
  811. &res_coroipcs_zc_free,
  812. sizeof (struct qb_ipc_response_header));
  813. if (error != CS_OK) {
  814. goto error_exit;
  815. }
  816. res = munmap ((void *)header, header->map_size);
  817. if (res == -1) {
  818. error = qb_to_cs_error(-errno);
  819. goto error_exit;
  820. }
  821. error_exit:
  822. hdb_handle_put (&cpg_handle_t_db, handle);
  823. return (error);
  824. }
  825. cs_error_t cpg_zcb_mcast_joined (
  826. cpg_handle_t handle,
  827. cpg_guarantee_t guarantee,
  828. void *msg,
  829. size_t msg_len)
  830. {
  831. cs_error_t error;
  832. struct cpg_inst *cpg_inst;
  833. struct req_lib_cpg_mcast *req_lib_cpg_mcast;
  834. struct res_lib_cpg_mcast res_lib_cpg_mcast;
  835. mar_req_coroipcc_zc_execute_t req_coroipcc_zc_execute;
  836. struct coroipcs_zc_header *hdr;
  837. struct iovec iovec;
  838. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  839. if (error != CS_OK) {
  840. return (error);
  841. }
  842. if (msg_len > IPC_REQUEST_SIZE) {
  843. error = CS_ERR_TOO_BIG;
  844. goto error_exit;
  845. }
  846. req_lib_cpg_mcast = (struct req_lib_cpg_mcast *)(((char *)msg) - sizeof (struct req_lib_cpg_mcast));
  847. req_lib_cpg_mcast->header.size = sizeof (struct req_lib_cpg_mcast) +
  848. msg_len;
  849. req_lib_cpg_mcast->header.id = MESSAGE_REQ_CPG_MCAST;
  850. req_lib_cpg_mcast->guarantee = guarantee;
  851. req_lib_cpg_mcast->msglen = msg_len;
  852. hdr = (struct coroipcs_zc_header *)(((char *)req_lib_cpg_mcast) - sizeof (struct coroipcs_zc_header));
  853. req_coroipcc_zc_execute.header.size = sizeof (mar_req_coroipcc_zc_execute_t);
  854. req_coroipcc_zc_execute.header.id = MESSAGE_REQ_CPG_ZC_EXECUTE;
  855. req_coroipcc_zc_execute.server_address = hdr->server_address;
  856. iovec.iov_base = (void *)&req_coroipcc_zc_execute;
  857. iovec.iov_len = sizeof (mar_req_coroipcc_zc_execute_t);
  858. error = coroipcc_msg_send_reply_receive (
  859. cpg_inst->c,
  860. &iovec,
  861. 1,
  862. &res_lib_cpg_mcast,
  863. sizeof(res_lib_cpg_mcast));
  864. if (error != CS_OK) {
  865. goto error_exit;
  866. }
  867. error = res_lib_cpg_mcast.header.error;
  868. error_exit:
  869. hdb_handle_put (&cpg_handle_t_db, handle);
  870. return (error);
  871. }
  872. static cs_error_t send_fragments (
  873. struct cpg_inst *cpg_inst,
  874. cpg_guarantee_t guarantee,
  875. size_t msg_len,
  876. const struct iovec *iovec,
  877. unsigned int iov_len)
  878. {
  879. int i;
  880. cs_error_t error = CS_OK;
  881. struct iovec iov[2];
  882. struct req_lib_cpg_partial_mcast req_lib_cpg_mcast;
  883. struct res_lib_cpg_partial_send res_lib_cpg_partial_send;
  884. size_t sent = 0;
  885. size_t iov_sent = 0;
  886. int retry_count;
  887. req_lib_cpg_mcast.header.id = MESSAGE_REQ_CPG_PARTIAL_MCAST;
  888. req_lib_cpg_mcast.guarantee = guarantee;
  889. req_lib_cpg_mcast.msglen = msg_len;
  890. iov[0].iov_base = (void *)&req_lib_cpg_mcast;
  891. iov[0].iov_len = sizeof (struct req_lib_cpg_partial_mcast);
  892. i=0;
  893. iov_sent = 0 ;
  894. qb_ipcc_fc_enable_max_set(cpg_inst->c, 2);
  895. while (error == CS_OK && sent < msg_len) {
  896. retry_count = 0;
  897. if ( (iovec[i].iov_len - iov_sent) > cpg_inst->max_msg_size) {
  898. iov[1].iov_len = cpg_inst->max_msg_size;
  899. }
  900. else {
  901. iov[1].iov_len = iovec[i].iov_len - iov_sent;
  902. }
  903. if (sent == 0) {
  904. req_lib_cpg_mcast.type = LIBCPG_PARTIAL_FIRST;
  905. }
  906. else if ((sent + iov[1].iov_len) == msg_len) {
  907. req_lib_cpg_mcast.type = LIBCPG_PARTIAL_LAST;
  908. }
  909. else {
  910. req_lib_cpg_mcast.type = LIBCPG_PARTIAL_CONTINUED;
  911. }
  912. req_lib_cpg_mcast.fraglen = iov[1].iov_len;
  913. req_lib_cpg_mcast.header.size = sizeof (struct req_lib_cpg_partial_mcast) + iov[1].iov_len;
  914. iov[1].iov_base = (char *)iovec[i].iov_base + iov_sent;
  915. resend:
  916. error = coroipcc_msg_send_reply_receive (cpg_inst->c, iov, 2,
  917. &res_lib_cpg_partial_send,
  918. sizeof (res_lib_cpg_partial_send));
  919. if (error == CS_ERR_TRY_AGAIN) {
  920. fprintf(stderr, "sleep. counter=%d\n", retry_count);
  921. if (++retry_count > MAX_RETRIES) {
  922. goto error_exit;
  923. }
  924. usleep(10000);
  925. goto resend;
  926. }
  927. iov_sent += iov[1].iov_len;
  928. sent += iov[1].iov_len;
  929. /* Next iovec */
  930. if (iov_sent >= iovec[i].iov_len) {
  931. i++;
  932. iov_sent = 0;
  933. }
  934. error = res_lib_cpg_partial_send.header.error;
  935. }
  936. error_exit:
  937. qb_ipcc_fc_enable_max_set(cpg_inst->c, 1);
  938. return error;
  939. }
  940. cs_error_t cpg_mcast_joined (
  941. cpg_handle_t handle,
  942. cpg_guarantee_t guarantee,
  943. const struct iovec *iovec,
  944. unsigned int iov_len)
  945. {
  946. int i;
  947. cs_error_t error;
  948. struct cpg_inst *cpg_inst;
  949. struct iovec iov[64];
  950. struct req_lib_cpg_mcast req_lib_cpg_mcast;
  951. size_t msg_len = 0;
  952. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  953. if (error != CS_OK) {
  954. return (error);
  955. }
  956. for (i = 0; i < iov_len; i++ ) {
  957. msg_len += iovec[i].iov_len;
  958. }
  959. if (msg_len > cpg_inst->max_msg_size) {
  960. error = send_fragments(cpg_inst, guarantee, msg_len, iovec, iov_len);
  961. goto error_exit;
  962. }
  963. req_lib_cpg_mcast.header.size = sizeof (struct req_lib_cpg_mcast) +
  964. msg_len;
  965. req_lib_cpg_mcast.header.id = MESSAGE_REQ_CPG_MCAST;
  966. req_lib_cpg_mcast.guarantee = guarantee;
  967. req_lib_cpg_mcast.msglen = msg_len;
  968. iov[0].iov_base = (void *)&req_lib_cpg_mcast;
  969. iov[0].iov_len = sizeof (struct req_lib_cpg_mcast);
  970. memcpy (&iov[1], iovec, iov_len * sizeof (struct iovec));
  971. qb_ipcc_fc_enable_max_set(cpg_inst->c, 2);
  972. error = qb_to_cs_error(qb_ipcc_sendv(cpg_inst->c, iov, iov_len + 1));
  973. qb_ipcc_fc_enable_max_set(cpg_inst->c, 1);
  974. error_exit:
  975. hdb_handle_put (&cpg_handle_t_db, handle);
  976. return (error);
  977. }
  978. cs_error_t cpg_iteration_initialize(
  979. cpg_handle_t handle,
  980. cpg_iteration_type_t iteration_type,
  981. const struct cpg_name *group,
  982. cpg_iteration_handle_t *cpg_iteration_handle)
  983. {
  984. cs_error_t error;
  985. struct iovec iov;
  986. struct cpg_inst *cpg_inst;
  987. struct cpg_iteration_instance_t *cpg_iteration_instance;
  988. struct req_lib_cpg_iterationinitialize req_lib_cpg_iterationinitialize;
  989. struct res_lib_cpg_iterationinitialize res_lib_cpg_iterationinitialize;
  990. if (group && group->length > CPG_MAX_NAME_LENGTH) {
  991. return (CS_ERR_NAME_TOO_LONG);
  992. }
  993. if (cpg_iteration_handle == NULL) {
  994. return (CS_ERR_INVALID_PARAM);
  995. }
  996. if ((iteration_type == CPG_ITERATION_ONE_GROUP && group == NULL) ||
  997. (iteration_type != CPG_ITERATION_ONE_GROUP && group != NULL)) {
  998. return (CS_ERR_INVALID_PARAM);
  999. }
  1000. if (iteration_type != CPG_ITERATION_NAME_ONLY && iteration_type != CPG_ITERATION_ONE_GROUP &&
  1001. iteration_type != CPG_ITERATION_ALL) {
  1002. return (CS_ERR_INVALID_PARAM);
  1003. }
  1004. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  1005. if (error != CS_OK) {
  1006. return (error);
  1007. }
  1008. error = hdb_error_to_cs (hdb_handle_create (&cpg_iteration_handle_t_db,
  1009. sizeof (struct cpg_iteration_instance_t), cpg_iteration_handle));
  1010. if (error != CS_OK) {
  1011. goto error_put_cpg_db;
  1012. }
  1013. error = hdb_error_to_cs (hdb_handle_get (&cpg_iteration_handle_t_db, *cpg_iteration_handle,
  1014. (void *)&cpg_iteration_instance));
  1015. if (error != CS_OK) {
  1016. goto error_destroy;
  1017. }
  1018. cpg_iteration_instance->conn = cpg_inst->c;
  1019. list_init (&cpg_iteration_instance->list);
  1020. req_lib_cpg_iterationinitialize.header.size = sizeof (struct req_lib_cpg_iterationinitialize);
  1021. req_lib_cpg_iterationinitialize.header.id = MESSAGE_REQ_CPG_ITERATIONINITIALIZE;
  1022. req_lib_cpg_iterationinitialize.iteration_type = iteration_type;
  1023. if (group) {
  1024. marshall_to_mar_cpg_name_t (&req_lib_cpg_iterationinitialize.group_name, group);
  1025. }
  1026. iov.iov_base = (void *)&req_lib_cpg_iterationinitialize;
  1027. iov.iov_len = sizeof (struct req_lib_cpg_iterationinitialize);
  1028. error = coroipcc_msg_send_reply_receive (cpg_inst->c,
  1029. &iov,
  1030. 1,
  1031. &res_lib_cpg_iterationinitialize,
  1032. sizeof (struct res_lib_cpg_iterationinitialize));
  1033. if (error != CS_OK) {
  1034. goto error_put_destroy;
  1035. }
  1036. cpg_iteration_instance->executive_iteration_handle =
  1037. res_lib_cpg_iterationinitialize.iteration_handle;
  1038. cpg_iteration_instance->cpg_iteration_handle = *cpg_iteration_handle;
  1039. list_add (&cpg_iteration_instance->list, &cpg_inst->iteration_list_head);
  1040. hdb_handle_put (&cpg_iteration_handle_t_db, *cpg_iteration_handle);
  1041. hdb_handle_put (&cpg_handle_t_db, handle);
  1042. return (res_lib_cpg_iterationinitialize.header.error);
  1043. error_put_destroy:
  1044. hdb_handle_put (&cpg_iteration_handle_t_db, *cpg_iteration_handle);
  1045. error_destroy:
  1046. hdb_handle_destroy (&cpg_iteration_handle_t_db, *cpg_iteration_handle);
  1047. error_put_cpg_db:
  1048. hdb_handle_put (&cpg_handle_t_db, handle);
  1049. return (error);
  1050. }
  1051. cs_error_t cpg_iteration_next(
  1052. cpg_iteration_handle_t handle,
  1053. struct cpg_iteration_description_t *description)
  1054. {
  1055. cs_error_t error;
  1056. struct cpg_iteration_instance_t *cpg_iteration_instance;
  1057. struct req_lib_cpg_iterationnext req_lib_cpg_iterationnext;
  1058. struct res_lib_cpg_iterationnext res_lib_cpg_iterationnext;
  1059. if (description == NULL) {
  1060. return CS_ERR_INVALID_PARAM;
  1061. }
  1062. error = hdb_error_to_cs (hdb_handle_get (&cpg_iteration_handle_t_db, handle,
  1063. (void *)&cpg_iteration_instance));
  1064. if (error != CS_OK) {
  1065. goto error_exit;
  1066. }
  1067. req_lib_cpg_iterationnext.header.size = sizeof (struct req_lib_cpg_iterationnext);
  1068. req_lib_cpg_iterationnext.header.id = MESSAGE_REQ_CPG_ITERATIONNEXT;
  1069. req_lib_cpg_iterationnext.iteration_handle = cpg_iteration_instance->executive_iteration_handle;
  1070. error = qb_to_cs_error (qb_ipcc_send (cpg_iteration_instance->conn,
  1071. &req_lib_cpg_iterationnext,
  1072. req_lib_cpg_iterationnext.header.size));
  1073. if (error != CS_OK) {
  1074. goto error_put;
  1075. }
  1076. error = qb_to_cs_error (qb_ipcc_recv (cpg_iteration_instance->conn,
  1077. &res_lib_cpg_iterationnext,
  1078. sizeof(struct res_lib_cpg_iterationnext), -1));
  1079. if (error != CS_OK) {
  1080. goto error_put;
  1081. }
  1082. marshall_from_mar_cpg_iteration_description_t(
  1083. description,
  1084. &res_lib_cpg_iterationnext.description);
  1085. error = res_lib_cpg_iterationnext.header.error;
  1086. error_put:
  1087. hdb_handle_put (&cpg_iteration_handle_t_db, handle);
  1088. error_exit:
  1089. return (error);
  1090. }
  1091. cs_error_t cpg_iteration_finalize (
  1092. cpg_iteration_handle_t handle)
  1093. {
  1094. cs_error_t error;
  1095. struct iovec iov;
  1096. struct cpg_iteration_instance_t *cpg_iteration_instance;
  1097. struct req_lib_cpg_iterationfinalize req_lib_cpg_iterationfinalize;
  1098. struct res_lib_cpg_iterationfinalize res_lib_cpg_iterationfinalize;
  1099. error = hdb_error_to_cs (hdb_handle_get (&cpg_iteration_handle_t_db, handle,
  1100. (void *)&cpg_iteration_instance));
  1101. if (error != CS_OK) {
  1102. goto error_exit;
  1103. }
  1104. req_lib_cpg_iterationfinalize.header.size = sizeof (struct req_lib_cpg_iterationfinalize);
  1105. req_lib_cpg_iterationfinalize.header.id = MESSAGE_REQ_CPG_ITERATIONFINALIZE;
  1106. req_lib_cpg_iterationfinalize.iteration_handle = cpg_iteration_instance->executive_iteration_handle;
  1107. iov.iov_base = (void *)&req_lib_cpg_iterationfinalize;
  1108. iov.iov_len = sizeof (struct req_lib_cpg_iterationfinalize);
  1109. error = coroipcc_msg_send_reply_receive (cpg_iteration_instance->conn,
  1110. &iov,
  1111. 1,
  1112. &res_lib_cpg_iterationfinalize,
  1113. sizeof (struct req_lib_cpg_iterationfinalize));
  1114. if (error != CS_OK) {
  1115. goto error_put;
  1116. }
  1117. cpg_iteration_instance_finalize (cpg_iteration_instance);
  1118. hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_instance->cpg_iteration_handle);
  1119. return (res_lib_cpg_iterationfinalize.header.error);
  1120. error_put:
  1121. hdb_handle_put (&cpg_iteration_handle_t_db, handle);
  1122. error_exit:
  1123. return (error);
  1124. }
  1125. /** @} */