8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / librdc / common / rdcconfig.c
blob92e735d66e41d76aec431bc4f2bd2227b887d019
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 #include <sys/types.h>
28 #include <stdio.h>
29 #include <errno.h>
30 #include <strings.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <netdb.h>
34 #include <sys/stream.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38 #include <ctype.h>
39 #include <thread.h>
40 #include <pthread.h>
42 #include <sys/unistat/spcs_s.h>
43 #include <sys/unistat/spcs_s_u.h>
44 #include <sys/unistat/spcs_s_impl.h>
45 #include <sys/unistat/spcs_errors.h>
47 #include <sys/nsctl/rdc_io.h>
48 #include <sys/nsctl/rdc_ioctl.h>
49 #include <sys/nsctl/rdc_prot.h>
50 #include <sys/nsctl/librdc.h>
51 #include <sys/nsctl/rdcrules.h>
52 #include <sys/nsctl/rdcerr.h>
53 #include <sys/nsctl/cfg.h>
55 #include <sys/unistat/spcs_dtrinkets.h>
56 #include <sys/unistat/spcs_etrinkets.h>
58 #include <sys/socket.h>
59 #include <netinet/in.h>
60 #include <arpa/inet.h>
61 #include <netinet/tcp.h>
62 #include <rpc/rpc_com.h>
63 #include <rpc/rpc.h>
65 struct netbuf svaddr, *svp;
66 struct netconfig nconf, *conf;
67 struct knetconfig knconf;
70 * libdscfg type stuff here
72 extern int sv_enable(CFGFILE *cfg, rdcconfig_t *rdc);
73 extern int add_to_rdc_cfg(rdcconfig_t *rdcs);
74 extern int remove_from_rdc_cfg(rdcconfig_t *rdcs);
75 extern int replace_cfgfield(rdcconfig_t *rdcs, char *field, char *value);
76 extern int reverse_in_cfg(rdcconfig_t *rdcs);
78 rdcconfig_t *
79 rdc_dup_config(rdcconfig_t *orig)
81 rdcconfig_t *rc;
83 rc = (rdcconfig_t *)calloc(1, sizeof (*rc));
84 if (!rc) {
85 rdc_set_error(NULL, RDC_OS, RDC_FATAL, NULL);
86 return (NULL);
89 *rc = *orig;
90 rc->next = NULL; /* don't want to hook into wrong chaing */
91 return (rc);
95 * takes in a chain of rdcconfig_t's and a chain
96 * of rdc_rc_t's, checks for success in the rdc_rc_t,
97 * then adds the corresponding rdcconfig_t to the return
98 * chain.
100 rdcconfig_t *
101 chain_successful(rdcconfig_t *rdcs, rdc_rc_t *rcs)
103 rdc_rc_t *rcp;
104 rdcconfig_t *rdcp;
105 rdcconfig_t *ret = NULL;
106 rdcconfig_t *retp = NULL;
108 rcp = rcs;
109 rdcp = rdcs;
111 while (rcp) {
112 if (rcp->rc == 0) {
113 if ((ret == NULL) && (rdcp->persist)) {
114 retp = ret = rdc_dup_config(rdcp);
116 } else if ((ret) && (rdcp->persist)) {
117 retp->next = rdc_dup_config(rdcp);
118 retp = retp->next;
121 rcp = rcp->next;
122 rdcp = rdcp->next;
124 return (ret);
128 rdc_set_t
129 config2set(rdcconfig_t *rdc)
131 rdc_set_t urdc;
133 bzero(&urdc, sizeof (rdc_set_t));
134 strncpy(urdc.primary.intf, rdc->phost, MAX_RDC_HOST_SIZE);
135 strncpy(urdc.primary.file, rdc->pfile, NSC_MAXPATH);
136 strncpy(urdc.primary.bitmap, rdc->pbmp, NSC_MAXPATH);
137 strncpy(urdc.secondary.intf, rdc->shost, MAX_RDC_HOST_SIZE);
138 strncpy(urdc.secondary.file, rdc->sfile, NSC_MAXPATH);
139 strncpy(urdc.secondary.bitmap, rdc->sbmp, NSC_MAXPATH);
140 strncpy(urdc.group_name, rdc->group, NSC_MAXPATH);
142 return (urdc);
145 rdc_rc_t *
146 new_rc()
148 rdc_rc_t *rc;
150 rc = (rdc_rc_t *)calloc(1, sizeof (*rc));
151 if (rc == NULL) {
152 rdc_set_error(NULL, RDC_OS, RDC_FATAL, NULL);
153 return (NULL);
155 return (rc);
158 rdc_rc_t
159 rdc_config(rdc_config_t *rdccfg)
161 rdc_rc_t rc;
162 rdc_set_t *set;
163 spcs_s_info_t ustatus;
165 bzero(&rc, sizeof (rc));
166 ustatus = spcs_s_ucreate();
168 if (self_check(rdccfg->rdc_set->primary.intf)) {
169 rdccfg->options |= RDC_OPT_PRIMARY;
170 /* this needs changin if we do campus */
171 rdccfg->rdc_set->direct_file[0] = 0;
172 } else {
173 rdccfg->options |= RDC_OPT_SECONDARY;
176 /* set up return stuff.. */
177 set = &rdccfg->rdc_set[0];
178 strncpy(rc.set.phost, set->primary.intf, MAX_RDC_HOST_SIZE);
179 strncpy(rc.set.pfile, set->primary.file, NSC_MAXPATH);
180 strncpy(rc.set.shost, set->secondary.intf, MAX_RDC_HOST_SIZE);
181 strncpy(rc.set.sfile, set->secondary.file, NSC_MAXPATH);
183 rc.rc = RDC_IOCTL(RDC_CONFIG, rdccfg, NULL, 0, 0, 0, ustatus);
185 if (rc.rc < 0) {
186 rdc_set_error(&ustatus, RDC_SPCS, 0, 0);
187 strncpy(rc.msg, rdc_error(NULL), RDC_ERR_SIZE);
190 return (rc);
193 void *
194 rdc_mtconfig(void *rdc)
196 rdc_rc_t *rc[1];
197 rdc_set_t *set;
198 spcs_s_info_t ustatus;
199 rdc_config_t *rdccfg = (rdc_config_t *)rdc;
201 ustatus = spcs_s_ucreate();
203 if (self_check(rdccfg->rdc_set->primary.intf)) {
204 rdccfg->options |= RDC_OPT_PRIMARY;
205 /* this needs changin if we do campus */
206 rdccfg->rdc_set->direct_file[0] = 0;
207 } else {
208 rdccfg->options |= RDC_OPT_SECONDARY;
211 set = &rdccfg->rdc_set[0];
212 *rc = new_rc();
214 strncpy(rc[0]->set.phost, set->primary.intf, MAX_RDC_HOST_SIZE);
215 strncpy(rc[0]->set.pfile, set->primary.file, NSC_MAXPATH);
216 strncpy(rc[0]->set.pbmp, set->primary.bitmap, NSC_MAXPATH);
217 strncpy(rc[0]->set.shost, set->secondary.intf, MAX_RDC_HOST_SIZE);
218 strncpy(rc[0]->set.sfile, set->secondary.file, NSC_MAXPATH);
219 strncpy(rc[0]->set.sbmp, set->secondary.bitmap, NSC_MAXPATH);
221 rc[0]->rc = RDC_IOCTL(RDC_CONFIG, rdccfg, NULL, 0, 0, 0, ustatus);
223 if (rc[0]->rc < 0) {
224 rdc_set_error(&ustatus, RDC_SPCS, 0, 0);
225 strncpy(rc[0]->msg, rdc_error(NULL), RDC_ERR_SIZE);
228 sleep(1); /* give thr_join a chance to be called */
229 free(rdccfg);
230 thr_exit((void **) *rc);
231 return (NULL);
234 populate_addrs(rdc_set_t *urdc, int isenable)
236 struct t_info tinfo;
237 struct hostent *hp;
238 char toname[MAX_RDC_HOST_SIZE];
239 char fromname[MAX_RDC_HOST_SIZE];
241 strncpy(fromname, urdc->primary.intf, MAX_RDC_HOST_SIZE);
242 strncpy(toname, urdc->secondary.intf, MAX_RDC_HOST_SIZE);
244 if ((fromname[0] == '\0') || (fromname[0] == '\0')) {
245 rdc_set_error(NULL, RDC_INTERNAL, RDC_FATAL,
246 "NULL hostname recieved");
247 return (-1);
250 hp = gethost_byname(fromname);
251 strncpy(fromname, hp->h_name, MAX_RDC_HOST_SIZE);
252 hp = gethost_byname(toname);
253 strncpy(toname, hp->h_name, MAX_RDC_HOST_SIZE);
255 if (self_check(fromname) && self_check(toname)) {
256 rdc_set_error(NULL, RDC_INTERNAL, RDC_FATAL, "");
259 if (isenable) {
260 svp = get_addr(toname, RDC_PROGRAM, RDC_VERS_MIN,
261 &conf, NULL, "rdc", &tinfo, 0);
262 if (svp == NULL)
263 return (-1);
264 svaddr = *svp;
265 } else {
266 bzero(&svaddr, sizeof (svaddr));
269 urdc->secondary.addr.len = svaddr.len;
270 urdc->secondary.addr.maxlen = svaddr.maxlen;
271 urdc->secondary.addr.buf = (void*)svaddr.buf;
273 if (isenable) {
274 svp = get_addr(fromname, RDC_PROGRAM, RDC_VERS_MIN,
275 &conf, NULL, "rdc", &tinfo, 0);
276 if (svp == NULL)
277 return (-1);
278 svaddr = *svp;
279 } else {
280 bzero(&svaddr, sizeof (svaddr));
283 urdc->primary.addr.len = svaddr.len;
284 urdc->primary.addr.maxlen = svaddr.maxlen;
285 urdc->primary.addr.buf = (void*)svaddr.buf;
287 if (isenable) {
288 convert_nconf_to_knconf(conf, &knconf);
289 urdc->netconfig = &knconf;
290 } else {
291 urdc->netconfig = NULL;
293 urdc->syshostid = (int32_t)gethostid();
295 return (1);
298 void
299 rdc_free_config(rdcconfig_t *rdc, int all)
301 rdcconfig_t *rdcp;
302 rdcconfig_t *rdcq;
304 rdcp = rdc;
305 if (all == RDC_FREEONE) {
306 free(rdcp);
307 } else while (rdcp) {
308 rdcq = rdcp->next;
309 free(rdcp);
310 rdcp = rdcq;
312 rdc = NULL;
315 void
316 rdc_free_rclist(rdc_rc_t *rc) {
317 rdc_rc_t *rcp, *rcq;
319 rcp = rc;
320 while (rcp) {
321 rcq = rcp->next;
322 free(rcp);
323 rcp = rcq;
327 /*ARGSUSED*/
328 rdcconfig_t *
329 rdc_alloc_config(const char *phost, const char *pfile,
330 const char *pbmp, const char *shost, const char *sfile, const char *sbmp,
331 const char *mode, const char *group, const char *ctag, const char *options,
332 int persist)
334 rdcconfig_t *rc;
336 rc = (rdcconfig_t *)calloc(1, sizeof (*rc));
337 if (!rc) {
338 rdc_set_error(NULL, RDC_OS, RDC_FATAL, NULL);
339 return (NULL);
341 if (phost)
342 strncpy(rc->phost, phost, NSC_MAXPATH);
343 if (pfile)
344 strncpy(rc->pfile, pfile, NSC_MAXPATH);
345 if (pbmp)
346 strncpy(rc->pbmp, pbmp, NSC_MAXPATH);
347 if (shost)
348 strncpy(rc->shost, shost, NSC_MAXPATH);
349 if (sfile)
350 strncpy(rc->sfile, sfile, NSC_MAXPATH);
351 if (sbmp)
352 strncpy(rc->sbmp, sbmp, NSC_MAXPATH);
354 strncpy(rc->direct, "ip", 2);
356 if (mode)
357 strncpy(rc->mode, mode, NSC_MAXPATH);
358 if (ctag)
359 strncpy(rc->ctag, ctag, NSC_MAXPATH);
360 if (options)
361 strncpy(rc->options, options, NSC_MAXPATH);
363 rc->persist = persist;
364 rc->next = NULL;
366 return (rc);
370 void
371 populate_rc(rdc_rc_t *rcp, rdcconfig_t *rdcp)
373 rcp->rc = -1;
374 strncpy(rcp->msg, rdc_error(NULL), RDC_ERR_SIZE);
375 strncpy(rcp->set.phost, rdcp->phost, NSC_MAXPATH);
376 strncpy(rcp->set.pfile, rdcp->pfile, NSC_MAXPATH);
377 strncpy(rcp->set.shost, rdcp->shost, NSC_MAXPATH);
378 strncpy(rcp->set.sfile, rdcp->sfile, NSC_MAXPATH);
382 * rdc_enable
383 * return values
384 * NULL on error
385 * pointer to rdc_rc_t list of return values
387 rdc_rc_t *
388 rdc_enable(rdcconfig_t *rdc)
390 rdc_config_t rdccfg;
391 rdcconfig_t *rdcp = NULL;
392 rdcconfig_t *cfg_rdcs = NULL;
393 rdc_rc_t *rc = NULL;
394 rdc_rc_t *rcp = NULL;
396 rdcp = rdc;
397 rc = new_rc();
398 if (!rc) { /* error already set */
399 return (NULL);
401 rcp = rc;
402 while (rdcp) {
403 if (!rdcp->mode) {
404 rdc_set_error(NULL, RDC_INTERNAL, RDC_NONFATAL,
405 RDC_EINVAL);
406 return (NULL);
408 bzero(&rdccfg, sizeof (rdc_config_t));
409 rdccfg.rdc_set[0] = config2set(rdcp);
410 rdccfg.command = RDC_CMD_ENABLE;
411 rdccfg.options = RDC_OPT_SETBMP;
412 if (strncmp(rdcp->mode, "sync", NSC_MAXPATH) == 0) {
413 rdccfg.options |= RDC_OPT_SYNC;
414 } else if (strncmp(rdc->mode, "async", NSC_MAXPATH) == 0) {
415 rdccfg.options |= RDC_OPT_ASYNC;
416 } else {
417 rdc_set_error(NULL, RDC_INTERNAL, RDC_NONFATAL,
418 RDC_EINVAL);
419 return (NULL);
422 populate_addrs(&rdccfg.rdc_set[0], 1);
424 if (can_enable(rdcp)) {
425 /* do the operation */
426 *rcp = rdc_config(&rdccfg);
428 } else { /* set up what rdc_config would've set up */
430 populate_rc(rcp, rdcp);
433 if ((rcp->rc == 0) && (!rdcp->persist)) {
435 * if we are not persisting, do this now,
436 * otherwise we will do it when
437 * we have a lock on the cfg in add_to_rdc_cfg
439 sv_enable(NULL, rdcp);
442 rdcp = rdcp->next;
443 if (!rdcp)
444 break;
446 rcp->next = new_rc();
447 rcp = rcp->next;
448 if (!rcp) {
449 /* dont free here, return what you have */
450 break;
455 * travel the rc chain and rdc chain checking results,
456 * building a new chain, and updating dscfg
458 rcp = rc;
459 rdcp = rdc;
461 cfg_rdcs = chain_successful(rdcp, rcp);
463 if (add_to_rdc_cfg(cfg_rdcs) < 0) {
464 /* XXX should disable or something here */
465 return (rc);
467 rdc_free_config(cfg_rdcs, RDC_FREEALL);
468 return (rc);
472 rdc_rc_t *
473 rdc_enable_clrbmp(rdcconfig_t *rdc)
475 rdc_config_t rdccfg;
476 rdcconfig_t *rdcp = NULL;
477 rdcconfig_t *cfg_rdcs = NULL;
478 rdc_rc_t *rc = NULL;
479 rdc_rc_t *rcp = NULL;
481 rdcp = rdc;
482 rc = (rdc_rc_t *)calloc(1, sizeof (rdc_rc_t));
483 if (!rc) {
484 rdc_set_error(NULL, RDC_OS, RDC_FATAL, NULL);
485 return (NULL);
487 rcp = rc;
488 while (rdcp) {
489 if (!rdcp->mode) {
490 rdc_set_error(NULL, RDC_INTERNAL, RDC_NONFATAL,
491 RDC_EINVAL);
492 return (NULL);
494 bzero(&rdccfg, sizeof (rdc_config_t));
495 rdccfg.rdc_set[0] = config2set(rdcp);
496 rdccfg.command = RDC_CMD_ENABLE;
497 rdccfg.options = RDC_OPT_CLRBMP;
498 if (strncmp(rdcp->mode, "sync", NSC_MAXPATH) == 0) {
499 rdccfg.options |= RDC_OPT_SYNC;
500 } else if (strncmp(rdc->mode, "async", NSC_MAXPATH) == 0) {
501 rdccfg.options |= RDC_OPT_ASYNC;
502 } else {
503 rdc_set_error(NULL, RDC_INTERNAL, RDC_NONFATAL,
504 RDC_EINVAL);
505 return (NULL);
508 populate_addrs(&rdccfg.rdc_set[0], 1);
510 if (can_enable(rdcp)) {
511 /* do the operation */
512 *rcp = rdc_config(&rdccfg);
514 } else { /* set up what rdc_config would've set up */
516 populate_rc(rcp, rdcp);
519 rdcp = rdcp->next;
520 if (!rdcp)
521 break;
523 rcp->next = (rdc_rc_t *)calloc(1, sizeof (rdc_rc_t));
524 rcp = rcp->next;
525 if (!rcp)
526 break;
530 * travel the rc chain and rdc chain checking results,
531 * building a new chain, and updating dscfg
533 rcp = rc;
534 rdcp = rdc;
536 cfg_rdcs = chain_successful(rdcp, rcp);
538 if (add_to_rdc_cfg(cfg_rdcs) < 0) {
539 /* XXX should disable or something here */
540 return (rc);
542 rdc_free_config(cfg_rdcs, RDC_FREEALL);
544 return (rc);
548 rdc_rc_t *
549 rdc_disable(rdcconfig_t *rdc)
551 rdc_config_t rdccfg;
552 rdcconfig_t *rdcp = NULL;
553 rdcconfig_t *cfg_rdcs = NULL;
554 rdc_rc_t *rc = NULL;
555 rdc_rc_t *rcp = NULL;
557 rdcp = rdc;
558 rc = new_rc();
559 if (!rc) {
560 return (NULL);
562 rcp = rc;
564 while (rdcp) {
566 bzero(&rdccfg, sizeof (rdc_config_t));
567 rdccfg.rdc_set[0] = config2set(rdcp);
568 rdccfg.command = RDC_CMD_DISABLE;
569 populate_addrs(&rdccfg.rdc_set[0], 0);
571 *rcp = rdc_config(&rdccfg);
573 rdcp = rdcp->next;
574 if (!rdcp)
575 break;
577 rcp->next = new_rc();
578 rcp = rcp->next;
579 if (!rcp)
580 return (rc);
583 rcp = rc;
584 rdcp = rdc;
586 cfg_rdcs = chain_successful(rdcp, rcp);
588 remove_from_rdc_cfg(cfg_rdcs);
590 rdc_free_config(cfg_rdcs, RDC_FREEALL);
592 return (rc);
595 rdc_rc_t *
596 rdc_log(rdcconfig_t *rdc)
598 rdc_config_t rdccfg;
599 rdcconfig_t *rdcp = NULL;
600 rdc_rc_t *rc = NULL;
601 rdc_rc_t *rcp = NULL;
603 rdcp = rdc;
604 rc = new_rc();
605 if (!rc) {
606 return (NULL);
608 rcp = rc;
610 while (rdcp) {
611 bzero(&rdccfg, sizeof (rdc_config_t));
612 rdccfg.rdc_set[0] = config2set(rdcp);
613 rdccfg.command = RDC_CMD_LOG;
614 populate_addrs(&rdccfg.rdc_set[0], 0);
616 *rcp = rdc_config(&rdccfg);
618 rdcp = rdcp->next;
619 if (!rdcp)
620 break;
622 rcp->next = new_rc();
623 rcp = rcp->next;
624 if (!rcp)
625 break;
627 return (rc);
630 rdc_rc_t *
631 rdc_usync(rdcconfig_t *rdc)
633 rdc_config_t *rdccfg;
634 rdcconfig_t *rdcp = NULL;
635 rdc_rc_t *rc = NULL;
636 rdc_rc_t *rcp = NULL;
637 rdc_rc_t *tmprc;
638 int trc;
640 rdcp = rdc;
642 while (rdcp) {
643 /* freed in rdc_mtconfig */
644 rdccfg = (rdc_config_t *)calloc(1, sizeof (rdc_config_t));
645 rdccfg->rdc_set[0] = config2set(rdcp);
646 rdccfg->command = RDC_CMD_COPY;
647 rdccfg->options = RDC_OPT_UPDATE|RDC_OPT_FORWARD;
648 populate_addrs(&rdccfg->rdc_set[0], 0);
649 trc = thr_create(NULL, 0, rdc_mtconfig,
650 (void **) rdccfg, THR_BOUND, NULL);
651 rdcp = rdcp->next;
652 if (!rdcp)
653 break;
658 * collect status here from thr_join-status,
659 * and add to rdc_rc_t chain ?
660 * this will block, but caller could always thread too
662 while (thr_join(NULL, NULL, (void**) &tmprc) == 0) {
663 if (rc == NULL) {
664 rcp = rc = (rdc_rc_t *)tmprc;
665 } else {
666 rcp->next = (rdc_rc_t *)tmprc;
667 rcp = rcp->next;
671 return (rc);
674 rdc_rc_t *
675 rdc_fsync(rdcconfig_t *rdc)
677 rdc_config_t *rdccfg;
678 rdcconfig_t *rdcp = NULL;
679 rdc_rc_t *rc = NULL;
680 rdc_rc_t *rcp = NULL;
681 rdc_rc_t *tmprc = NULL;
682 int trc;
684 rdcp = rdc;
685 rc = new_rc();
686 if (!rc) {
687 return (NULL);
689 rcp = rc;
691 while (rdcp) {
692 /* freed in rdc_mtconfig */
693 rdccfg = (rdc_config_t *)calloc(1, sizeof (rdc_config_t));
694 rdccfg->rdc_set[0] = config2set(rdcp);
695 rdccfg->command = RDC_CMD_COPY;
696 rdccfg->options = RDC_OPT_FULL|RDC_OPT_FORWARD;
697 populate_addrs(&rdccfg->rdc_set[0], 0);
698 trc = thr_create(NULL, 0, rdc_mtconfig,
699 (void **) rdccfg, THR_BOUND, NULL);
700 rdcp = rdcp->next;
701 if (!rdcp)
702 break;
707 * collect status here from thr_join-status,
708 * and add to rdc_rc_t chain ?
709 * this will block, but caller could always thread too
711 while (thr_join(NULL, NULL, (void**) &tmprc) == 0) {
712 if (rc == NULL) {
713 rcp = rc = (rdc_rc_t *)tmprc;
714 } else {
715 rcp->next = (rdc_rc_t *)tmprc;
716 rcp = rcp->next;
720 return (rc);
723 rdc_rc_t *
724 rdc_rsync(rdcconfig_t *rdc)
726 rdc_config_t *rdccfg;
727 rdcconfig_t *rdcp = NULL;
728 rdc_rc_t *rc = NULL;
729 rdc_rc_t *rcp = NULL;
730 rdc_rc_t *tmprc = NULL;
731 int trc;
733 rdcp = rdc;
734 rc = new_rc();
735 if (!rc) {
736 return (NULL);
738 rcp = rc;
740 while (rdcp) {
741 tmprc = cant_rsync(rdcp);
742 if (tmprc != NULL) {
743 if (rc == NULL) {
744 rcp = rc = tmprc;
745 } else {
746 rcp->next = tmprc;
747 rcp = rcp->next;
749 goto next;
752 /* freed in rdc_mtconfig */
753 rdccfg = (rdc_config_t *)calloc(1, sizeof (rdc_config_t));
754 rdccfg->rdc_set[0] = config2set(rdcp);
755 rdccfg->command = RDC_CMD_COPY;
756 rdccfg->options = RDC_OPT_REVERSE|RDC_OPT_FULL;
757 populate_addrs(&rdccfg->rdc_set[0], 0);
758 trc = thr_create(NULL, 0, rdc_mtconfig,
759 (void **) rdccfg, THR_BOUND, NULL);
760 next:
761 rdcp = rdcp->next;
762 if (!rdcp)
763 break;
767 * collect status here from thr_join-status,
768 * and add to rdc_rc_t chain ?
769 * this will block, but caller could always thread too
771 while (thr_join(NULL, NULL, (void**) &tmprc) == 0) {
772 if (rc == NULL) {
773 rcp = rc = (rdc_rc_t *)tmprc;
774 } else {
775 rcp->next = (rdc_rc_t *)tmprc;
776 rcp = rcp->next;
780 return (rc);
783 rdc_rc_t *
784 rdc_ursync(rdcconfig_t *rdc)
786 rdc_config_t *rdccfg;
787 rdcconfig_t *rdcp = NULL;
788 rdc_rc_t *rc = NULL;
789 rdc_rc_t *rcp = NULL;
790 rdc_rc_t *tmprc = NULL;
791 int trc;
793 rdcp = rdc;
795 while (rdcp) {
796 tmprc = cant_rsync(rdcp);
797 if (tmprc != NULL) {
798 if (rc == NULL) {
799 rcp = rc = tmprc;
800 } else {
801 rcp->next = tmprc;
802 rcp = rcp->next;
804 goto next;
807 /* freed in rdc_mtconfig */
808 rdccfg = (rdc_config_t *)calloc(1, sizeof (rdc_config_t));
809 rdccfg->rdc_set[0] = config2set(rdcp);
810 rdccfg->command = RDC_CMD_COPY;
811 rdccfg->options = RDC_OPT_REVERSE | RDC_OPT_UPDATE;
812 populate_addrs(&rdccfg->rdc_set[0], 0);
813 trc = thr_create(NULL, 0, rdc_mtconfig,
814 (void **) rdccfg, THR_BOUND, NULL);
815 next:
816 rdcp = rdcp->next;
817 if (!rdcp)
818 break;
823 * collect status here from thr_join-status,
824 * and add to rdc_rc_t chain ?
825 * this will block, but caller could always thread too
827 while (thr_join(NULL, NULL, (void**) &tmprc) == 0) {
828 if (rc == NULL) {
829 rcp = rc = (rdc_rc_t *)tmprc;
830 } else {
831 rcp->next = (rdc_rc_t *)tmprc;
832 rcp = rcp->next;
836 return (rc);
839 rdc_rc_t *
840 rdc_wait(rdcconfig_t *rdc)
842 rdc_config_t rdccfg;
843 rdcconfig_t *rdcp = NULL;
844 rdc_rc_t *rc = NULL;
845 rdc_rc_t *rcp = NULL;
847 rdcp = rdc;
848 rc = new_rc();
849 if (!rc) {
850 return (NULL);
852 rcp = rc;
854 while (rdcp) {
855 bzero(&rdccfg, sizeof (rdc_config_t));
856 rdccfg.rdc_set[0] = config2set(rdcp);
857 rdccfg.command = RDC_CMD_WAIT;
858 populate_addrs(&rdccfg.rdc_set[0], 0);
860 *rcp = rdc_config(&rdccfg);
862 rdcp = rdcp->next;
863 if (!rdcp)
864 break;
866 rcp->next = new_rc();
867 rcp = rcp->next;
868 if (!rcp)
869 break;
871 return (rc);
874 rdc_rc_t *
875 rdc_set_autosync(rdcconfig_t *rdc, int autosync)
877 rdc_config_t rdccfg;
878 rdcconfig_t *rdcp = NULL;
879 rdc_rc_t *rc = NULL;
880 rdc_rc_t *rcp = NULL;
882 rdcp = rdc;
883 rc = new_rc();
884 if (!rc) {
885 return (NULL);
887 rcp = rc;
889 while (rdcp) {
890 bzero(&rdccfg, sizeof (rdc_config_t));
891 rdccfg.rdc_set[0] = config2set(rdcp);
892 rdccfg.command = RDC_CMD_TUNABLE;
893 rdccfg.rdc_set[0].autosync = autosync;
894 rdccfg.rdc_set[0].maxqitems = -1;
895 rdccfg.rdc_set[0].maxqfbas = -1;
896 populate_addrs(&rdccfg.rdc_set[0], 0);
898 *rcp = rdc_config(&rdccfg);
900 rdcp = rdcp->next;
901 if (!rdcp)
902 break;
904 rcp->next = new_rc();
905 rcp = rcp->next;
906 if (!rcp)
907 break;
909 return (rc);
912 rdc_rc_t *
913 rdc_set_maxqfbas(rdcconfig_t *rdc, int maxqfbas)
915 rdc_config_t rdccfg;
916 rdcconfig_t *rdcp = NULL;
917 rdc_rc_t *rc = NULL;
918 rdc_rc_t *rcp = NULL;
920 rdcp = rdc;
921 rc = new_rc();
922 if (!rc) {
923 return (NULL);
925 rcp = rc;
927 while (rdcp) {
928 bzero(&rdccfg, sizeof (rdc_config_t));
929 rdccfg.rdc_set[0] = config2set(rdcp);
930 rdccfg.command = RDC_CMD_TUNABLE;
931 rdccfg.rdc_set[0].autosync = -1;
932 rdccfg.rdc_set[0].maxqitems = -1;
933 rdccfg.rdc_set[0].maxqfbas = maxqfbas;
934 populate_addrs(&rdccfg.rdc_set[0], 0);
936 *rcp = rdc_config(&rdccfg);
938 rdcp = rdcp->next;
939 if (!rdcp)
940 break;
942 rcp->next = new_rc();
943 rcp = rcp->next;
944 if (!rcp)
945 break;
947 return (rc);
950 rdc_rc_t *
951 rdc_set_maxqitems(rdcconfig_t *rdc, int maxqitems)
953 rdc_config_t rdccfg;
954 rdcconfig_t *rdcp = NULL;
955 rdc_rc_t *rc = NULL;
956 rdc_rc_t *rcp = NULL;
958 rdcp = rdc;
959 rc = new_rc();
961 if (!rc) {
962 return (NULL);
964 rcp = rc;
966 while (rdcp) {
967 bzero(&rdccfg, sizeof (rdc_config_t));
968 rdccfg.rdc_set[0] = config2set(rdc);
969 rdccfg.command = RDC_CMD_TUNABLE;
970 rdccfg.rdc_set[0].autosync = -1;
971 rdccfg.rdc_set[0].maxqitems = maxqitems;
972 rdccfg.rdc_set[0].maxqfbas = -1;
973 populate_addrs(&rdccfg.rdc_set[0], 0);
975 *rcp = rdc_config(&rdccfg);
977 rdcp = rdcp->next;
978 if (!rdcp)
979 break;
981 rcp->next = new_rc();
982 rcp = rcp->next;
983 if (!rcp)
984 break;
986 return (rc);
989 rdc_set_t
990 rdc_status(rdcconfig_t *rdc)
992 rdc_config_t rdccfg;
994 bzero(&rdccfg, sizeof (rdc_config_t));
995 rdccfg.rdc_set[0] = config2set(rdc);
996 rdccfg.command = RDC_CMD_STATUS;
997 populate_addrs(&rdccfg.rdc_set[0], 0);
998 rdc_config(&rdccfg);
1000 return (rdccfg.rdc_set[0]);
1004 rdc_get_autosync(rdcconfig_t *rdc)
1006 rdc_set_t rdcset;
1008 rdcset = rdc_status(rdc);
1009 return (rdcset.autosync);
1013 rdc_get_maxqfbas(rdcconfig_t *rdc)
1015 rdc_set_t rdcset;
1017 rdcset = rdc_status(rdc);
1018 return (rdcset.maxqfbas);
1023 rdc_get_maxqitems(rdcconfig_t *rdc)
1025 rdc_set_t rdcset;
1027 rdcset = rdc_status(rdc);
1028 return (rdcset.maxqitems);
1033 set_mode(rdcconfig_t *rdc)
1035 if (strcmp(rdc->mode, "async") == 0)
1036 return (RDC_OPT_ASYNC);
1037 else
1038 return (RDC_OPT_SYNC);
1042 * reconfig bitmaps are single set only ops
1043 * for obvious reasons
1045 rdc_rc_t *
1046 rdc_reconfig_pbmp(rdcconfig_t *rdc, char *pbmp)
1048 rdc_config_t rdccfg;
1049 rdc_rc_t *rc;
1051 rc = new_rc();
1052 if ((!rc) || (!pbmp))
1053 return (NULL);
1055 bzero(&rdccfg, sizeof (rdc_config_t));
1056 rdccfg.rdc_set[0] = config2set(rdc);
1057 strncpy(rdccfg.rdc_set[0].primary.bitmap, pbmp, NSC_MAXPATH);
1058 rdccfg.command = RDC_CMD_RECONFIG;
1059 rdccfg.options |= set_mode(rdc);
1060 populate_addrs(&rdccfg.rdc_set[0], 0);
1062 if (can_reconfig_pbmp(rdc, pbmp))
1063 *rc = rdc_config(&rdccfg);
1064 else
1065 populate_rc(rc, rdc);
1067 if ((rc->rc == 0) && (rdc->persist))
1068 if (replace_cfgfield(rdc, "pbitmap", pbmp) < 0) {
1069 rc->rc = -1;
1070 strncpy(rc->msg, rdc_error(NULL), RDC_ERR_SIZE);
1072 return (rc);
1075 rdc_rc_t *
1076 rdc_reconfig_sbmp(rdcconfig_t *rdc, char *sbmp)
1078 rdc_config_t rdccfg;
1079 rdc_rc_t *rc;
1081 rc = new_rc();
1082 if (!rc)
1083 return (NULL);
1085 bzero(&rdccfg, sizeof (rdc_config_t));
1086 rdccfg.rdc_set[0] = config2set(rdc);
1087 strncpy(rdccfg.rdc_set[0].secondary.bitmap, sbmp, NSC_MAXPATH);
1088 rdccfg.command = RDC_CMD_RECONFIG;
1089 rdccfg.options |= set_mode(rdc);
1090 populate_addrs(&rdccfg.rdc_set[0], 0);
1092 if (can_reconfig_sbmp(rdc, sbmp))
1093 *rc = rdc_config(&rdccfg);
1094 else
1095 populate_rc(rc, rdc);
1097 if ((rc->rc == 0) && (rdc->persist))
1098 replace_cfgfield(rdc, "sbitmap", sbmp);
1100 return (rc);
1103 rdc_rc_t *
1104 rdc_reconfig_group(rdcconfig_t *rdc, char *group)
1106 rdc_config_t rdccfg;
1107 rdcconfig_t *rdcp = NULL;
1108 rdcconfig_t *cfg_rdcs = NULL;
1109 rdc_rc_t *rc = NULL;
1110 rdc_rc_t *rcp = NULL;
1112 rdcp = rdc;
1113 rc = new_rc();
1114 if (!rc) {
1115 return (NULL);
1117 rcp = rc;
1119 while (rdcp) {
1120 bzero(&rdccfg, sizeof (rdc_config_t));
1121 /* just in case */
1122 strncpy(rdcp->group, group, NSC_MAXPATH);
1123 rdccfg.rdc_set[0] = config2set(rdcp);
1124 rdccfg.command = RDC_CMD_RECONFIG;
1125 rdccfg.options |= set_mode(rdcp);
1126 populate_addrs(&rdccfg.rdc_set[0], 0);
1128 /* reconfig group rules enforced in kernel */
1129 *rcp = rdc_config(&rdccfg);
1131 rdcp = rdcp->next;
1132 if (!rdcp)
1133 break;
1135 rcp->next = new_rc();
1136 rcp = rcp->next;
1137 if (!rcp)
1138 break;
1140 rcp = rc;
1141 rdcp = rdc;
1142 cfg_rdcs = chain_successful(rdcp, rcp);
1143 replace_cfgfield(cfg_rdcs, "group", group);
1144 rdc_free_config(cfg_rdcs, RDC_FREEALL);
1146 return (rc);
1148 /*ARGSUSED*/
1149 rdc_rc_t *
1150 rdc_reconfig_ctag(rdcconfig_t *rdc, char *ctag)
1152 return (NULL);
1155 rdc_rc_t *
1156 rdc_set_sync(rdcconfig_t *rdc)
1158 rdc_config_t rdccfg;
1159 rdcconfig_t *rdcp = NULL;
1160 rdcconfig_t *cfg_rdcs = NULL;
1161 rdc_rc_t *rc = NULL;
1162 rdc_rc_t *rcp = NULL;
1164 rdcp = rdc;
1165 rc = new_rc();
1166 if (!rc) {
1167 return (NULL);
1169 rcp = rc;
1171 while (rdcp) {
1172 bzero(&rdccfg, sizeof (rdc_config_t));
1173 rdccfg.rdc_set[0] = config2set(rdc);
1174 rdccfg.command = RDC_CMD_RECONFIG;
1175 rdccfg.options |= RDC_OPT_SYNC;
1176 populate_addrs(&rdccfg.rdc_set[0], 0);
1178 *rcp = rdc_config(&rdccfg);
1180 rdcp = rdcp->next;
1181 if (!rdcp)
1182 break;
1184 rcp->next = new_rc();
1185 rcp = rcp->next;
1186 if (!rcp)
1187 break;
1190 rcp = rc;
1191 rdcp = rdc;
1192 cfg_rdcs = chain_successful(rdcp, rcp);
1193 replace_cfgfield(cfg_rdcs, "mode", "sync");
1194 rdc_free_config(cfg_rdcs, RDC_FREEALL);
1196 return (rc);
1199 rdc_rc_t *
1200 rdc_set_async(rdcconfig_t *rdc)
1202 rdc_config_t rdccfg;
1203 rdcconfig_t *rdcp = NULL;
1204 rdcconfig_t *cfg_rdcs = NULL;
1205 rdc_rc_t *rc = NULL;
1206 rdc_rc_t *rcp = NULL;
1208 rdcp = rdc;
1209 rc = new_rc();
1210 if (!rc) {
1211 return (NULL);
1213 rcp = rc;
1215 while (rdcp) {
1216 bzero(&rdccfg, sizeof (rdc_config_t));
1217 rdccfg.rdc_set[0] = config2set(rdcp);
1218 rdccfg.command = RDC_CMD_RECONFIG;
1219 rdccfg.options |= RDC_OPT_ASYNC;
1220 populate_addrs(&rdccfg.rdc_set[0], 0);
1222 *rcp = rdc_config(&rdccfg);
1224 rdcp = rdcp->next;
1225 if (!rdcp)
1226 break;
1228 rcp->next = new_rc();
1229 rcp = rcp->next;
1230 if (!rcp)
1231 break;
1233 rcp = rc;
1234 rdcp = rdc;
1235 cfg_rdcs = chain_successful(rdcp, rcp);
1236 replace_cfgfield(cfg_rdcs, "mode", "async");
1237 rdc_free_config(cfg_rdcs, RDC_FREEALL);
1239 return (rc);
1242 rdc_rc_t *
1243 rdc_health(rdcconfig_t *rdc)
1245 rdc_config_t rdccfg;
1246 rdcconfig_t *rdcp = NULL;
1247 rdc_rc_t *rc = NULL;
1248 rdc_rc_t *rcp = NULL;
1250 rdcp = rdc;
1251 rc = new_rc();
1252 if (!rc) {
1253 return (NULL);
1255 rcp = rc;
1257 while (rdcp) {
1258 bzero(&rdccfg, sizeof (rdc_config_t));
1259 rdccfg.rdc_set[0] = config2set(rdcp);
1260 rdccfg.command = RDC_CMD_HEALTH;
1261 populate_addrs(&rdccfg.rdc_set[0], 0);
1263 *rcp = rdc_config(&rdccfg);
1265 rdcp = rdcp->next;
1266 if (!rdcp)
1267 break;
1269 rcp->next = new_rc();
1270 rcp = rcp->next;
1272 if (!rcp)
1273 break;
1276 return (rc);
1279 rdc_rc_t *
1280 rdc_reverse_role(rdcconfig_t *rdc)
1282 rdc_config_t rdccfg;
1283 rdcconfig_t *rdcp = NULL;
1284 rdcconfig_t *cfg_rdcs = NULL;
1285 rdc_rc_t *rc = NULL;
1286 rdc_rc_t *rcp = NULL;
1288 rdcp = rdc;
1289 rc = new_rc();
1290 if (!rc) {
1291 return (NULL);
1293 rcp = rc;
1295 while (rdcp) {
1296 bzero(&rdccfg, sizeof (rdc_config_t));
1297 rdccfg.rdc_set[0] = config2set(rdcp);
1298 rdccfg.command = RDC_CMD_RECONFIG;
1299 rdccfg.options |= RDC_OPT_REVERSE_ROLE;
1300 rdccfg.options |= set_mode(rdcp);
1301 populate_addrs(&rdccfg.rdc_set[0], 0);
1303 *rcp = rdc_config(&rdccfg);
1305 rdcp = rdcp->next;
1306 if (!rdcp)
1307 break;
1309 rcp->next = new_rc();
1310 rcp = rcp->next;
1311 if (!rcp)
1312 break;
1314 rcp = rc;
1315 rdcp = rdc;
1316 cfg_rdcs = chain_successful(rdcp, rcp);
1317 reverse_in_cfg(cfg_rdcs);
1318 rdc_free_config(cfg_rdcs, RDC_FREEALL);
1320 return (rc);