2 Copyright (C) 2010 - 2020 Proxmox Server Solutions GmbH
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Affero General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Affero General Public License for more details.
14 You should have received a copy of the GNU Affero General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 Author: Dietmar Maurer <dietmar@proxmox.com>
21 #define G_LOG_DOMAIN "status"
25 #endif /* HAVE_CONFIG_H */
32 #include <sys/syslog.h>
34 #include <rrd_client.h>
38 #include "cfs-utils.h"
43 #define KVSTORE_CPG_GROUP_NAME "pve_kvstore_v1"
46 KVSTORE_MESSAGE_UPDATE
= 1,
47 KVSTORE_MESSAGE_UPDATE_COMPLETE
= 2,
48 KVSTORE_MESSAGE_LOG
= 3,
51 static uint32_t vminfo_version_counter
;
79 static memdb_change_t memdb_change_array
[] = {
80 { .path
= "corosync.conf" },
81 { .path
= "corosync.conf.new" },
82 { .path
= "storage.cfg" },
83 { .path
= "user.cfg" },
84 { .path
= "domains.cfg" },
85 { .path
= "priv/shadow.cfg" },
86 { .path
= "priv/acme/plugins.cfg" },
87 { .path
= "priv/tfa.cfg" },
88 { .path
= "priv/token.cfg" },
89 { .path
= "datacenter.cfg" },
90 { .path
= "vzdump.cron" },
91 { .path
= "ha/crm_commands" },
92 { .path
= "ha/manager_status" },
93 { .path
= "ha/resources.cfg" },
94 { .path
= "ha/groups.cfg" },
95 { .path
= "ha/fence.cfg" },
96 { .path
= "status.cfg" },
97 { .path
= "replication.cfg" },
98 { .path
= "ceph.conf" },
99 { .path
= "sdn/vnets.cfg" },
100 { .path
= "sdn/zones.cfg" },
101 { .path
= "sdn/controllers.cfg" },
102 { .path
= "sdn/subnets.cfg" },
103 { .path
= "sdn/ipams.cfg" },
104 { .path
= "sdn/.version" },
105 { .path
= "virtual-guest/cpu-models.conf" },
115 cfs_clinfo_t
*clinfo
;
116 uint32_t clinfo_version
;
119 uint32_t vmlist_version
;
126 GHashTable
*memdb_changes
;
128 clusterlog_t
*clusterlog
;
131 static cfs_status_t cfs_status
;
143 uint32_t cman_version
;
145 GHashTable
*nodes_byid
;
146 GHashTable
*nodes_byname
;
150 g_int32_hash (gconstpointer v
)
152 return *(const uint32_t *) v
;
156 g_int32_equal (gconstpointer v1
,
159 return *((const uint32_t*) v1
) == *((const uint32_t*) v2
);
162 static void vminfo_free(vminfo_t
*vminfo
)
164 g_return_if_fail(vminfo
!= NULL
);
166 if (vminfo
->nodename
)
167 g_free(vminfo
->nodename
);
173 static const char *vminfo_type_to_string(vminfo_t
*vminfo
)
175 if (vminfo
->vmtype
== VMTYPE_QEMU
) {
177 } else if (vminfo
->vmtype
== VMTYPE_OPENVZ
) {
178 // FIXME: remove openvz stuff for 7.x
180 } else if (vminfo
->vmtype
== VMTYPE_LXC
) {
187 static const char *vminfo_type_to_path_type(vminfo_t
*vminfo
)
189 if (vminfo
->vmtype
== VMTYPE_QEMU
) {
190 return "qemu-server"; // special case..
192 return vminfo_type_to_string(vminfo
);
196 int vminfo_to_path(vminfo_t
*vminfo
, GString
*path
)
198 g_return_val_if_fail(vminfo
!= NULL
, -1);
199 g_return_val_if_fail(path
!= NULL
, -1);
201 if (!vminfo
->nodename
)
204 const char *type
= vminfo_type_to_path_type(vminfo
);
205 g_string_printf(path
, "/nodes/%s/%s/%u.conf", vminfo
->nodename
, type
, vminfo
->vmid
);
210 void cfs_clnode_destroy(
211 cfs_clnode_t
*clnode
)
213 g_return_if_fail(clnode
!= NULL
);
216 g_hash_table_destroy(clnode
->kvhash
);
219 g_free(clnode
->name
);
224 cfs_clnode_t
*cfs_clnode_new(
229 g_return_val_if_fail(name
!= NULL
, NULL
);
231 cfs_clnode_t
*clnode
= g_new0(cfs_clnode_t
, 1);
235 clnode
->name
= g_strdup(name
);
236 clnode
->nodeid
= nodeid
;
237 clnode
->votes
= votes
;
242 gboolean
cfs_clinfo_destroy(
243 cfs_clinfo_t
*clinfo
)
245 g_return_val_if_fail(clinfo
!= NULL
, FALSE
);
247 if (clinfo
->cluster_name
)
248 g_free(clinfo
->cluster_name
);
250 if (clinfo
->nodes_byname
)
251 g_hash_table_destroy(clinfo
->nodes_byname
);
253 if (clinfo
->nodes_byid
)
254 g_hash_table_destroy(clinfo
->nodes_byid
);
261 cfs_clinfo_t
*cfs_clinfo_new(
262 const char *cluster_name
,
263 uint32_t cman_version
)
265 g_return_val_if_fail(cluster_name
!= NULL
, NULL
);
267 cfs_clinfo_t
*clinfo
= g_new0(cfs_clinfo_t
, 1);
271 clinfo
->cluster_name
= g_strdup(cluster_name
);
272 clinfo
->cman_version
= cman_version
;
274 if (!(clinfo
->nodes_byid
= g_hash_table_new_full(
275 g_int32_hash
, g_int32_equal
, NULL
,
276 (GDestroyNotify
)cfs_clnode_destroy
)))
279 if (!(clinfo
->nodes_byname
= g_hash_table_new(g_str_hash
, g_str_equal
)))
285 cfs_clinfo_destroy(clinfo
);
290 gboolean
cfs_clinfo_add_node(
291 cfs_clinfo_t
*clinfo
,
292 cfs_clnode_t
*clnode
)
294 g_return_val_if_fail(clinfo
!= NULL
, FALSE
);
295 g_return_val_if_fail(clnode
!= NULL
, FALSE
);
297 g_hash_table_replace(clinfo
->nodes_byid
, &clnode
->nodeid
, clnode
);
298 g_hash_table_replace(clinfo
->nodes_byname
, clnode
->name
, clnode
);
304 cfs_create_memberlist_msg(
307 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
309 g_mutex_lock (&mutex
);
311 g_string_append_printf(str
,"{\n");
315 cfs_clinfo_t
*clinfo
= cfs_status
.clinfo
;
317 if (clinfo
&& clinfo
->nodes_byid
)
318 nodecount
= g_hash_table_size(clinfo
->nodes_byid
);
321 g_string_append_printf(str
, "\"nodename\": \"%s\",\n", cfs
.nodename
);
322 g_string_append_printf(str
, "\"version\": %u,\n", cfs_status
.clinfo_version
);
324 g_string_append_printf(str
, "\"cluster\": { ");
325 g_string_append_printf(str
, "\"name\": \"%s\", \"version\": %d, "
326 "\"nodes\": %d, \"quorate\": %d ",
327 clinfo
->cluster_name
, clinfo
->cman_version
,
328 nodecount
, cfs_status
.quorate
);
330 g_string_append_printf(str
,"},\n");
331 g_string_append_printf(str
,"\"nodelist\": {\n");
333 GHashTable
*ht
= clinfo
->nodes_byid
;
337 g_hash_table_iter_init (&iter
, ht
);
340 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
341 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
342 if (i
) g_string_append_printf(str
, ",\n");
345 g_string_append_printf(str
, " \"%s\": { \"id\": %d, \"online\": %d",
346 node
->name
, node
->nodeid
, node
->online
);
349 char *ip
= (char *)g_hash_table_lookup(cfs_status
.iphash
, node
->name
);
351 g_string_append_printf(str
, ", \"ip\": \"%s\"", ip
);
354 g_string_append_printf(str
, "}");
357 g_string_append_printf(str
,"\n }\n");
359 g_string_append_printf(str
, "\"nodename\": \"%s\",\n", cfs
.nodename
);
360 g_string_append_printf(str
, "\"version\": %u\n", cfs_status
.clinfo_version
);
363 g_string_append_printf(str
,"}\n");
365 g_mutex_unlock (&mutex
);
371 kventry_free(kventry_t
*entry
)
373 g_return_if_fail(entry
!= NULL
);
381 kventry_hash_new(void)
383 return g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
,
384 (GDestroyNotify
)kventry_free
);
388 rrdentry_free(rrdentry_t
*entry
)
390 g_return_if_fail(entry
!= NULL
);
398 rrdentry_hash_new(void)
400 return g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
,
401 (GDestroyNotify
)rrdentry_free
);
405 cfs_cluster_log_dump(GString
*str
, const char *user
, guint max_entries
)
407 clusterlog_dump(cfs_status
.clusterlog
, str
, user
, max_entries
);
411 cfs_cluster_log(clog_entry_t
*entry
)
413 g_return_if_fail(entry
!= NULL
);
415 clusterlog_insert(cfs_status
.clusterlog
, entry
);
417 if (cfs_status
.kvstore
) {
419 iov
[0].iov_base
= (char *)entry
;
420 iov
[0].iov_len
= clog_entry_size(entry
);
422 if (dfsm_is_initialized(cfs_status
.kvstore
))
423 dfsm_send_message(cfs_status
.kvstore
, KVSTORE_MESSAGE_LOG
, iov
, 1);
427 void cfs_status_init(void)
429 g_mutex_lock (&mutex
);
431 cfs_status
.start_time
= time(NULL
);
433 cfs_status
.vmlist
= vmlist_hash_new();
435 cfs_status
.kvhash
= kventry_hash_new();
437 cfs_status
.rrdhash
= rrdentry_hash_new();
439 cfs_status
.iphash
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
441 cfs_status
.memdb_changes
= g_hash_table_new(g_str_hash
, g_str_equal
);
443 for (int i
= 0; i
< G_N_ELEMENTS(memdb_change_array
); i
++) {
444 g_hash_table_replace(cfs_status
.memdb_changes
,
445 memdb_change_array
[i
].path
,
446 &memdb_change_array
[i
]);
449 cfs_status
.clusterlog
= clusterlog_new();
452 clusterlog_add(cfs_status
.clusterlog
, "root", "cluster", getpid(),
453 LOG_INFO
, "starting cluster log");
455 g_mutex_unlock (&mutex
);
458 void cfs_status_cleanup(void)
460 g_mutex_lock (&mutex
);
462 cfs_status
.clinfo_version
++;
464 if (cfs_status
.clinfo
) {
465 cfs_clinfo_destroy(cfs_status
.clinfo
);
466 cfs_status
.clinfo
= NULL
;
469 if (cfs_status
.vmlist
) {
470 g_hash_table_destroy(cfs_status
.vmlist
);
471 cfs_status
.vmlist
= NULL
;
474 if (cfs_status
.kvhash
) {
475 g_hash_table_destroy(cfs_status
.kvhash
);
476 cfs_status
.kvhash
= NULL
;
479 if (cfs_status
.rrdhash
) {
480 g_hash_table_destroy(cfs_status
.rrdhash
);
481 cfs_status
.rrdhash
= NULL
;
484 if (cfs_status
.iphash
) {
485 g_hash_table_destroy(cfs_status
.iphash
);
486 cfs_status
.iphash
= NULL
;
489 if (cfs_status
.clusterlog
)
490 clusterlog_destroy(cfs_status
.clusterlog
);
492 g_mutex_unlock (&mutex
);
495 void cfs_status_set_clinfo(
496 cfs_clinfo_t
*clinfo
)
498 g_return_if_fail(clinfo
!= NULL
);
500 g_mutex_lock (&mutex
);
502 cfs_status
.clinfo_version
++;
504 cfs_clinfo_t
*old
= cfs_status
.clinfo
;
506 cfs_status
.clinfo
= clinfo
;
508 cfs_message("update cluster info (cluster name %s, version = %d)",
509 clinfo
->cluster_name
, clinfo
->cman_version
);
512 if (old
&& old
->nodes_byid
&& clinfo
->nodes_byid
) {
514 GHashTable
*ht
= clinfo
->nodes_byid
;
518 g_hash_table_iter_init (&iter
, ht
);
520 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
521 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
522 cfs_clnode_t
*oldnode
;
523 if ((oldnode
= g_hash_table_lookup(old
->nodes_byid
, key
))) {
524 node
->online
= oldnode
->online
;
525 node
->kvhash
= oldnode
->kvhash
;
526 oldnode
->kvhash
= NULL
;
533 cfs_clinfo_destroy(old
);
536 g_mutex_unlock (&mutex
);
540 dump_kvstore_versions(
543 const char *nodename
)
545 g_return_if_fail(kvhash
!= NULL
);
546 g_return_if_fail(str
!= NULL
);
547 g_return_if_fail(nodename
!= NULL
);
549 GHashTable
*ht
= kvhash
;
553 g_string_append_printf(str
, "\"%s\": {\n", nodename
);
555 g_hash_table_iter_init (&iter
, ht
);
558 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
559 kventry_t
*entry
= (kventry_t
*)value
;
560 if (i
) g_string_append_printf(str
, ",\n");
562 g_string_append_printf(str
,"\"%s\": %u", entry
->key
, entry
->version
);
565 g_string_append_printf(str
, "}\n");
569 cfs_create_version_msg(GString
*str
)
571 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
573 g_mutex_lock (&mutex
);
575 g_string_append_printf(str
,"{\n");
577 g_string_append_printf(str
, "\"starttime\": %lu,\n", (unsigned long)cfs_status
.start_time
);
579 g_string_append_printf(str
, "\"clinfo\": %u,\n", cfs_status
.clinfo_version
);
581 g_string_append_printf(str
, "\"vmlist\": %u,\n", cfs_status
.vmlist_version
);
583 for (int i
= 0; i
< G_N_ELEMENTS(memdb_change_array
); i
++) {
584 g_string_append_printf(str
, "\"%s\": %u,\n",
585 memdb_change_array
[i
].path
,
586 memdb_change_array
[i
].version
);
589 g_string_append_printf(str
, "\"kvstore\": {\n");
591 dump_kvstore_versions(str
, cfs_status
.kvhash
, cfs
.nodename
);
593 cfs_clinfo_t
*clinfo
= cfs_status
.clinfo
;
595 if (clinfo
&& clinfo
->nodes_byid
) {
596 GHashTable
*ht
= clinfo
->nodes_byid
;
600 g_hash_table_iter_init (&iter
, ht
);
602 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
603 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
606 g_string_append_printf(str
, ",\n");
607 dump_kvstore_versions(str
, node
->kvhash
, node
->name
);
611 g_string_append_printf(str
,"}\n");
613 g_string_append_printf(str
,"}\n");
615 g_mutex_unlock (&mutex
);
621 vmlist_hash_new(void)
623 return g_hash_table_new_full(g_int_hash
, g_int_equal
, NULL
,
624 (GDestroyNotify
)vminfo_free
);
628 vmlist_hash_insert_vm(
632 const char *nodename
,
635 g_return_val_if_fail(vmlist
!= NULL
, FALSE
);
636 g_return_val_if_fail(nodename
!= NULL
, FALSE
);
637 g_return_val_if_fail(vmid
!= 0, FALSE
);
638 // FIXME: remove openvz stuff for 7.x
639 g_return_val_if_fail(vmtype
== VMTYPE_QEMU
|| vmtype
== VMTYPE_OPENVZ
||
640 vmtype
== VMTYPE_LXC
, FALSE
);
642 if (!replace
&& g_hash_table_lookup(vmlist
, &vmid
)) {
643 cfs_critical("detected duplicate VMID %d", vmid
);
647 vminfo_t
*vminfo
= g_new0(vminfo_t
, 1);
650 vminfo
->vmtype
= vmtype
;
651 vminfo
->nodename
= g_strdup(nodename
);
653 vminfo
->version
= ++vminfo_version_counter
;
655 g_hash_table_replace(vmlist
, &vminfo
->vmid
, vminfo
);
664 const char *nodename
)
666 g_return_if_fail(cfs_status
.vmlist
!= NULL
);
667 g_return_if_fail(nodename
!= NULL
);
668 g_return_if_fail(vmid
!= 0);
669 // FIXME: remove openvz stuff for 7.x
670 g_return_if_fail(vmtype
== VMTYPE_QEMU
|| vmtype
== VMTYPE_OPENVZ
||
671 vmtype
== VMTYPE_LXC
);
673 cfs_debug("vmlist_register_vm: %s/%u %d", nodename
, vmid
, vmtype
);
675 g_mutex_lock (&mutex
);
677 cfs_status
.vmlist_version
++;
679 vmlist_hash_insert_vm(cfs_status
.vmlist
, vmtype
, vmid
, nodename
, TRUE
);
681 g_mutex_unlock (&mutex
);
685 vmlist_different_vm_exists(
688 const char *nodename
)
690 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, FALSE
);
691 g_return_val_if_fail(vmid
!= 0, FALSE
);
693 gboolean res
= FALSE
;
695 g_mutex_lock (&mutex
);
698 if ((vminfo
= (vminfo_t
*)g_hash_table_lookup(cfs_status
.vmlist
, &vmid
))) {
699 if (!(vminfo
->vmtype
== vmtype
&& strcmp(vminfo
->nodename
, nodename
) == 0))
702 g_mutex_unlock (&mutex
);
711 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, FALSE
);
712 g_return_val_if_fail(vmid
!= 0, FALSE
);
714 g_mutex_lock (&mutex
);
716 gpointer res
= g_hash_table_lookup(cfs_status
.vmlist
, &vmid
);
718 g_mutex_unlock (&mutex
);
727 g_return_if_fail(cfs_status
.vmlist
!= NULL
);
728 g_return_if_fail(vmid
!= 0);
730 g_mutex_lock (&mutex
);
732 cfs_status
.vmlist_version
++;
734 g_hash_table_remove(cfs_status
.vmlist
, &vmid
);
736 g_mutex_unlock (&mutex
);
739 void cfs_status_set_vmlist(
742 g_return_if_fail(vmlist
!= NULL
);
744 g_mutex_lock (&mutex
);
746 cfs_status
.vmlist_version
++;
748 if (cfs_status
.vmlist
)
749 g_hash_table_destroy(cfs_status
.vmlist
);
751 cfs_status
.vmlist
= vmlist
;
753 g_mutex_unlock (&mutex
);
757 cfs_create_vmlist_msg(GString
*str
)
759 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, -EINVAL
);
760 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
762 g_mutex_lock (&mutex
);
764 g_string_append_printf(str
,"{\n");
766 GHashTable
*ht
= cfs_status
.vmlist
;
768 guint count
= g_hash_table_size(ht
);
771 g_string_append_printf(str
,"\"version\": %u\n", cfs_status
.vmlist_version
);
773 g_string_append_printf(str
,"\"version\": %u,\n", cfs_status
.vmlist_version
);
775 g_string_append_printf(str
,"\"ids\": {\n");
780 g_hash_table_iter_init (&iter
, ht
);
783 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
784 vminfo_t
*vminfo
= (vminfo_t
*)value
;
785 const char *type
= vminfo_type_to_string(vminfo
);
788 g_string_append_printf(str
, ",\n");
791 g_string_append_printf(str
,"\"%u\": { \"node\": \"%s\", \"type\": \"%s\", \"version\": %u }",
792 vminfo
->vmid
, vminfo
->nodename
, type
, vminfo
->version
);
795 g_string_append_printf(str
,"}\n");
797 g_string_append_printf(str
,"\n}\n");
799 g_mutex_unlock (&mutex
);
804 // checks the conf for a line starting with '$prop:' and returns the value
805 // afterwards, whitout initial whitespace(s), we only deal with the format
806 // restricion imposed by our perl VM config parser, main reference is
807 // PVE::QemuServer::parse_vm_config this allows to be very fast and still
809 // main restrictions used for our advantage is the properties match reges:
810 // ($line =~ m/^([a-z][a-z_]*\d*):\s*(.+?)\s*$/) from parse_vm_config
811 // currently we only look at the current configuration in place, i.e., *no*
812 // snapshort and *no* pending changes
814 _get_property_value(char *conf
, int conf_size
, const char *prop
, int prop_len
)
816 const char *const conf_end
= conf
+ conf_size
;
818 size_t remaining_size
;
820 char *next_newline
= memchr(conf
, '\n', conf_size
);
821 if (next_newline
== NULL
) {
822 return NULL
; // valid property lines end with \n, but none in the config
824 *next_newline
= '\0';
826 while (line
!= NULL
) {
827 if (!line
[0]) goto next
;
829 // snapshot or pending section start, but nothing found yet -> not found
830 if (line
[0] == '[') return NULL
;
831 // properties start with /^[a-z]/, so continue early if not
832 if (line
[0] < 'a' || line
[0] > 'z') goto next
;
834 int line_len
= strlen(line
);
835 if (line_len
<= prop_len
+ 1) goto next
;
837 if (line
[prop_len
] == ':' && memcmp(line
, prop
, prop_len
) == 0) { // found
838 char *v_start
= &line
[prop_len
+ 1];
840 // drop initial value whitespaces here already
841 while (*v_start
&& isspace(*v_start
)) v_start
++;
843 if (!*v_start
) return NULL
;
845 char *v_end
= &line
[line_len
- 1];
846 while (v_end
> v_start
&& isspace(*v_end
)) v_end
--;
852 line
= next_newline
+ 1;
853 remaining_size
= conf_end
- line
;
854 if (remaining_size
<= prop_len
) {
857 next_newline
= memchr(line
, '\n', remaining_size
);
858 if (next_newline
== NULL
) {
859 return NULL
; // valid property lines end with \n, but none in the config
861 *next_newline
= '\0';
864 return NULL
; // not found
868 _g_str_append_kv_jsonescaped(GString
*str
, const char *k
, const char *v
)
870 g_string_append_printf(str
, "\"%s\": \"", k
);
873 if (*v
== '\\' || *v
== '"') {
874 g_string_append_c(str
, '\\');
876 g_string_append_c(str
, *v
);
879 g_string_append_c(str
, '"');
883 cfs_create_guest_conf_property_msg(GString
*str
, memdb_t
*memdb
, const char *prop
, uint32_t vmid
)
885 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, -EINVAL
);
886 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
888 int prop_len
= strlen(prop
);
890 GString
*path
= NULL
;
892 // Prelock &memdb->mutex in order to enable the usage of memdb_read_nolock
893 // to prevent Deadlocks as in #2553
894 g_mutex_lock (&memdb
->mutex
);
895 g_mutex_lock (&mutex
);
897 g_string_printf(str
,"{\n");
899 GHashTable
*ht
= cfs_status
.vmlist
;
901 if (!g_hash_table_size(ht
)) {
905 if ((path
= g_string_sized_new(256)) == NULL
) {
911 vminfo_t
*vminfo
= (vminfo_t
*) g_hash_table_lookup(cfs_status
.vmlist
, &vmid
);
912 if (vminfo
== NULL
) goto enoent
;
914 if (!vminfo_to_path(vminfo
, path
)) goto err
;
916 // use memdb_read_nolock because lock is handled here
917 int size
= memdb_read_nolock(memdb
, path
->str
, &tmp
);
918 if (tmp
== NULL
) goto err
;
919 if (size
<= prop_len
) goto ret
;
921 char *val
= _get_property_value(tmp
, size
, prop
, prop_len
);
922 if (val
== NULL
) goto ret
;
924 g_string_append_printf(str
, "\"%u\":{", vmid
);
925 _g_str_append_kv_jsonescaped(str
, prop
, val
);
926 g_string_append_c(str
, '}');
930 g_hash_table_iter_init (&iter
, ht
);
934 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
935 vminfo_t
*vminfo
= (vminfo_t
*)value
;
937 if (!vminfo_to_path(vminfo
, path
)) goto err
;
939 g_free(tmp
); // no-op if already null
941 // use memdb_read_nolock because lock is handled here
942 int size
= memdb_read_nolock(memdb
, path
->str
, &tmp
);
943 if (tmp
== NULL
|| size
<= prop_len
) continue;
945 char *val
= _get_property_value(tmp
, size
, prop
, prop_len
);
946 if (val
== NULL
) continue;
948 if (!first
) g_string_append_printf(str
, ",\n");
951 g_string_append_printf(str
, "\"%u\":{", vminfo
->vmid
);
952 _g_str_append_kv_jsonescaped(str
, prop
, val
);
953 g_string_append_c(str
, '}');
959 g_string_free(path
, TRUE
);
961 g_string_append_printf(str
,"\n}\n");
962 g_mutex_unlock (&mutex
);
963 g_mutex_unlock (&memdb
->mutex
);
974 record_memdb_change(const char *path
)
976 g_return_if_fail(cfs_status
.memdb_changes
!= 0);
980 if ((ce
= (memdb_change_t
*)g_hash_table_lookup(cfs_status
.memdb_changes
, path
))) {
986 record_memdb_reload(void)
988 for (int i
= 0; i
< G_N_ELEMENTS(memdb_change_array
); i
++) {
989 memdb_change_array
[i
].version
++;
1000 g_return_val_if_fail(kvhash
!= NULL
, FALSE
);
1001 g_return_val_if_fail(key
!= NULL
, FALSE
);
1002 g_return_val_if_fail(data
!= NULL
, FALSE
);
1006 g_hash_table_remove(kvhash
, key
);
1007 } else if ((entry
= (kventry_t
*)g_hash_table_lookup(kvhash
, key
))) {
1008 g_free(entry
->data
);
1009 entry
->data
= g_memdup(data
, len
);
1013 kventry_t
*entry
= g_new0(kventry_t
, 1);
1015 entry
->key
= g_strdup(key
);
1016 entry
->data
= g_memdup(data
, len
);
1019 g_hash_table_replace(kvhash
, entry
->key
, entry
);
1025 static const char *rrd_def_node
[] = {
1026 "DS:loadavg:GAUGE:120:0:U",
1027 "DS:maxcpu:GAUGE:120:0:U",
1028 "DS:cpu:GAUGE:120:0:U",
1029 "DS:iowait:GAUGE:120:0:U",
1030 "DS:memtotal:GAUGE:120:0:U",
1031 "DS:memused:GAUGE:120:0:U",
1032 "DS:swaptotal:GAUGE:120:0:U",
1033 "DS:swapused:GAUGE:120:0:U",
1034 "DS:roottotal:GAUGE:120:0:U",
1035 "DS:rootused:GAUGE:120:0:U",
1036 "DS:netin:DERIVE:120:0:U",
1037 "DS:netout:DERIVE:120:0:U",
1039 "RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
1040 "RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
1041 "RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
1042 "RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
1043 "RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
1045 "RRA:MAX:0.5:1:70", // 1 min max - one hour
1046 "RRA:MAX:0.5:30:70", // 30 min max - one day
1047 "RRA:MAX:0.5:180:70", // 3 hour max - one week
1048 "RRA:MAX:0.5:720:70", // 12 hour max - one month
1049 "RRA:MAX:0.5:10080:70", // 7 day max - ony year
1053 static const char *rrd_def_vm
[] = {
1054 "DS:maxcpu:GAUGE:120:0:U",
1055 "DS:cpu:GAUGE:120:0:U",
1056 "DS:maxmem:GAUGE:120:0:U",
1057 "DS:mem:GAUGE:120:0:U",
1058 "DS:maxdisk:GAUGE:120:0:U",
1059 "DS:disk:GAUGE:120:0:U",
1060 "DS:netin:DERIVE:120:0:U",
1061 "DS:netout:DERIVE:120:0:U",
1062 "DS:diskread:DERIVE:120:0:U",
1063 "DS:diskwrite:DERIVE:120:0:U",
1065 "RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
1066 "RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
1067 "RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
1068 "RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
1069 "RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
1071 "RRA:MAX:0.5:1:70", // 1 min max - one hour
1072 "RRA:MAX:0.5:30:70", // 30 min max - one day
1073 "RRA:MAX:0.5:180:70", // 3 hour max - one week
1074 "RRA:MAX:0.5:720:70", // 12 hour max - one month
1075 "RRA:MAX:0.5:10080:70", // 7 day max - ony year
1079 static const char *rrd_def_storage
[] = {
1080 "DS:total:GAUGE:120:0:U",
1081 "DS:used:GAUGE:120:0:U",
1083 "RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
1084 "RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
1085 "RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
1086 "RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
1087 "RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
1089 "RRA:MAX:0.5:1:70", // 1 min max - one hour
1090 "RRA:MAX:0.5:30:70", // 30 min max - one day
1091 "RRA:MAX:0.5:180:70", // 3 hour max - one week
1092 "RRA:MAX:0.5:720:70", // 12 hour max - one month
1093 "RRA:MAX:0.5:10080:70", // 7 day max - ony year
1097 #define RRDDIR "/var/lib/rrdcached/db"
1101 const char *filename
,
1103 const char *rrddef
[])
1105 /* start at day boundary */
1108 struct tm
*ltm
= localtime(&ctime
);
1114 if (rrd_create_r(filename
, 60, timelocal(ltm
), argcount
, rrddef
)) {
1115 cfs_message("RRD create error %s: %s", filename
, rrd_get_error());
1119 static inline const char *
1125 while (*data
&& found
< count
) {
1138 g_return_if_fail(key
!= NULL
);
1139 g_return_if_fail(data
!= NULL
);
1140 g_return_if_fail(len
> 0);
1141 g_return_if_fail(len
< 4096);
1143 static const char *rrdcsock
= "unix:/var/run/rrdcached.sock";
1146 if (rrdc_connect(rrdcsock
) != 0)
1149 char *filename
= NULL
;
1153 if (strncmp(key
, "pve2-node/", 10) == 0) {
1154 const char *node
= key
+ 10;
1158 if (strchr(node
, '/') != NULL
)
1161 if (strlen(node
) < 1)
1164 filename
= g_strdup_printf(RRDDIR
"/%s", key
);
1166 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
)) {
1168 mkdir(RRDDIR
"/pve2-node", 0755);
1169 int argcount
= sizeof(rrd_def_node
)/sizeof(void*) - 1;
1170 create_rrd_file(filename
, argcount
, rrd_def_node
);
1173 } else if ((strncmp(key
, "pve2-vm/", 8) == 0) ||
1174 (strncmp(key
, "pve2.3-vm/", 10) == 0)) {
1177 if (strncmp(key
, "pve2-vm/", 8) == 0) {
1185 if (strchr(vmid
, '/') != NULL
)
1188 if (strlen(vmid
) < 1)
1191 filename
= g_strdup_printf(RRDDIR
"/%s/%s", "pve2-vm", vmid
);
1193 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
)) {
1195 mkdir(RRDDIR
"/pve2-vm", 0755);
1196 int argcount
= sizeof(rrd_def_vm
)/sizeof(void*) - 1;
1197 create_rrd_file(filename
, argcount
, rrd_def_vm
);
1200 } else if (strncmp(key
, "pve2-storage/", 13) == 0) {
1201 const char *node
= key
+ 13;
1203 const char *storage
= node
;
1204 while (*storage
&& *storage
!= '/')
1207 if (*storage
!= '/' || ((storage
- node
) < 1))
1212 if (strchr(storage
, '/') != NULL
)
1215 if (strlen(storage
) < 1)
1218 filename
= g_strdup_printf(RRDDIR
"/%s", key
);
1220 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
)) {
1222 mkdir(RRDDIR
"/pve2-storage", 0755);
1224 char *dir
= g_path_get_dirname(filename
);
1228 int argcount
= sizeof(rrd_def_storage
)/sizeof(void*) - 1;
1229 create_rrd_file(filename
, argcount
, rrd_def_storage
);
1236 const char *dp
= skip
? rrd_skip_data(data
, skip
) : data
;
1238 const char *update_args
[] = { dp
, NULL
};
1242 if ((status
= rrdc_update(filename
, 1, update_args
)) != 0) {
1243 cfs_message("RRDC update error %s: %d", filename
, status
);
1246 if (rrd_update_r(filename
, NULL
, 1, update_args
) != 0) {
1247 cfs_message("RRD update error %s: %s", filename
, rrd_get_error());
1253 if (rrd_update_r(filename
, NULL
, 1, update_args
) != 0) {
1254 cfs_message("RRD update error %s: %s", filename
, rrd_get_error());
1265 cfs_critical("RRD update error: unknown/wrong key %s", key
);
1275 rrdentry_t
*entry
= (rrdentry_t
*)value
;
1276 uint32_t ctime
= GPOINTER_TO_UINT(user_data
);
1278 int diff
= ctime
- entry
->time
;
1280 /* remove everything older than 5 minutes */
1283 return (diff
> expire
) ? TRUE
: FALSE
;
1286 static char *rrd_dump_buf
= NULL
;
1287 static time_t rrd_dump_last
= 0;
1290 cfs_rrd_dump(GString
*str
)
1294 g_mutex_lock (&mutex
);
1297 if (rrd_dump_buf
&& (ctime
- rrd_dump_last
) < 2) {
1298 g_string_assign(str
, rrd_dump_buf
);
1299 g_mutex_unlock (&mutex
);
1303 /* remove old data */
1304 g_hash_table_foreach_remove(cfs_status
.rrdhash
, rrd_entry_is_old
,
1305 GUINT_TO_POINTER(ctime
));
1307 g_string_set_size(str
, 0);
1309 GHashTableIter iter
;
1310 gpointer key
, value
;
1312 g_hash_table_iter_init (&iter
, cfs_status
.rrdhash
);
1314 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1315 rrdentry_t
*entry
= (rrdentry_t
*)value
;
1316 g_string_append(str
, key
);
1317 g_string_append(str
, ":");
1318 g_string_append(str
, entry
->data
);
1319 g_string_append(str
, "\n");
1322 g_string_append_c(str
, 0); // never return undef
1324 rrd_dump_last
= ctime
;
1326 g_free(rrd_dump_buf
);
1327 rrd_dump_buf
= g_strdup(str
->str
);
1329 g_mutex_unlock (&mutex
);
1335 const char *nodename
,
1339 g_return_val_if_fail(iphash
!= NULL
, FALSE
);
1340 g_return_val_if_fail(nodename
!= NULL
, FALSE
);
1341 g_return_val_if_fail(ip
!= NULL
, FALSE
);
1342 g_return_val_if_fail(len
> 0, FALSE
);
1343 g_return_val_if_fail(len
< 256, FALSE
);
1344 g_return_val_if_fail(ip
[len
-1] == 0, FALSE
);
1346 char *oldip
= (char *)g_hash_table_lookup(iphash
, nodename
);
1348 if (!oldip
|| (strcmp(oldip
, ip
) != 0)) {
1349 cfs_status
.clinfo_version
++;
1350 g_hash_table_replace(iphash
, g_strdup(nodename
), g_strdup(ip
));
1358 GHashTable
*rrdhash
,
1363 g_return_val_if_fail(rrdhash
!= NULL
, FALSE
);
1364 g_return_val_if_fail(key
!= NULL
, FALSE
);
1365 g_return_val_if_fail(data
!= NULL
, FALSE
);
1366 g_return_val_if_fail(len
> 0, FALSE
);
1367 g_return_val_if_fail(len
< 4096, FALSE
);
1368 g_return_val_if_fail(data
[len
-1] == 0, FALSE
);
1371 if ((entry
= (rrdentry_t
*)g_hash_table_lookup(rrdhash
, key
))) {
1372 g_free(entry
->data
);
1373 entry
->data
= g_memdup(data
, len
);
1375 entry
->time
= time(NULL
);
1377 rrdentry_t
*entry
= g_new0(rrdentry_t
, 1);
1379 entry
->key
= g_strdup(key
);
1380 entry
->data
= g_memdup(data
, len
);
1382 entry
->time
= time(NULL
);
1384 g_hash_table_replace(rrdhash
, entry
->key
, entry
);
1387 update_rrd_data(key
, data
, len
);
1393 kvstore_send_update_message(
1399 if (!dfsm_is_initialized(dfsm
))
1402 struct iovec iov
[2];
1405 g_strlcpy(name
, key
, sizeof(name
));
1407 iov
[0].iov_base
= &name
;
1408 iov
[0].iov_len
= sizeof(name
);
1410 iov
[1].iov_base
= (char *)data
;
1411 iov
[1].iov_len
= len
;
1413 if (dfsm_send_message(dfsm
, KVSTORE_MESSAGE_UPDATE
, iov
, 2) == CS_OK
)
1419 static clog_entry_t
*
1420 kvstore_parse_log_message(
1424 g_return_val_if_fail(msg
!= NULL
, NULL
);
1426 if (msg_len
< sizeof(clog_entry_t
)) {
1427 cfs_critical("received short log message (%zu < %zu)", msg_len
, sizeof(clog_entry_t
));
1431 clog_entry_t
*entry
= (clog_entry_t
*)msg
;
1433 uint32_t size
= sizeof(clog_entry_t
) + entry
->node_len
+
1434 entry
->ident_len
+ entry
->tag_len
+ entry
->msg_len
;
1436 if (msg_len
!= size
) {
1437 cfs_critical("received log message with wrong size (%zu != %u)", msg_len
, size
);
1441 char *msgptr
= entry
->data
;
1443 if (*((char *)msgptr
+ entry
->node_len
- 1)) {
1444 cfs_critical("unterminated string in log message");
1447 msgptr
+= entry
->node_len
;
1449 if (*((char *)msgptr
+ entry
->ident_len
- 1)) {
1450 cfs_critical("unterminated string in log message");
1453 msgptr
+= entry
->ident_len
;
1455 if (*((char *)msgptr
+ entry
->tag_len
- 1)) {
1456 cfs_critical("unterminated string in log message");
1459 msgptr
+= entry
->tag_len
;
1461 if (*((char *)msgptr
+ entry
->msg_len
- 1)) {
1462 cfs_critical("unterminated string in log message");
1470 kvstore_parse_update_message(
1474 gconstpointer
*data
,
1477 g_return_val_if_fail(msg
!= NULL
, FALSE
);
1478 g_return_val_if_fail(key
!= NULL
, FALSE
);
1479 g_return_val_if_fail(data
!= NULL
, FALSE
);
1480 g_return_val_if_fail(len
!= NULL
, FALSE
);
1482 if (msg_len
< 256) {
1483 cfs_critical("received short kvstore message (%zu < 256)", msg_len
);
1487 /* test if key is null terminated */
1489 for (i
= 0; i
< 256; i
++)
1490 if (((char *)msg
)[i
] == 0)
1497 *len
= msg_len
- 256;
1499 *data
= (char *) msg
+ 256;
1505 cfs_create_status_msg(
1507 const char *nodename
,
1510 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
1511 g_return_val_if_fail(key
!= NULL
, -EINVAL
);
1515 GHashTable
*kvhash
= NULL
;
1517 g_mutex_lock (&mutex
);
1519 if (!nodename
|| !nodename
[0] || !strcmp(nodename
, cfs
.nodename
)) {
1520 kvhash
= cfs_status
.kvhash
;
1521 } else if (cfs_status
.clinfo
&& cfs_status
.clinfo
->nodes_byname
) {
1522 cfs_clnode_t
*clnode
;
1523 if ((clnode
= g_hash_table_lookup(cfs_status
.clinfo
->nodes_byname
, nodename
)))
1524 kvhash
= clnode
->kvhash
;
1528 if (kvhash
&& (entry
= (kventry_t
*)g_hash_table_lookup(kvhash
, key
))) {
1529 g_string_append_len(str
, entry
->data
, entry
->len
);
1533 g_mutex_unlock (&mutex
);
1544 g_return_val_if_fail(key
!= NULL
, FALSE
);
1545 g_return_val_if_fail(data
!= NULL
, FALSE
);
1546 g_return_val_if_fail(cfs_status
.kvhash
!= NULL
, FALSE
);
1548 if (len
> CFS_MAX_STATUS_SIZE
)
1551 g_mutex_lock (&mutex
);
1555 if (strncmp(key
, "rrd/", 4) == 0) {
1556 res
= rrdentry_hash_set(cfs_status
.rrdhash
, key
+ 4, data
, len
);
1557 } else if (!strcmp(key
, "nodeip")) {
1558 res
= nodeip_hash_set(cfs_status
.iphash
, cfs
.nodename
, data
, len
);
1560 res
= kventry_hash_set(cfs_status
.kvhash
, key
, data
, len
);
1562 g_mutex_unlock (&mutex
);
1564 if (cfs_status
.kvstore
)
1565 kvstore_send_update_message(cfs_status
.kvstore
, key
, data
, len
);
1567 return res
? 0 : -ENOMEM
;
1571 cfs_kvstore_node_set(
1577 g_return_val_if_fail(nodeid
!= 0, FALSE
);
1578 g_return_val_if_fail(key
!= NULL
, FALSE
);
1579 g_return_val_if_fail(data
!= NULL
, FALSE
);
1581 g_mutex_lock (&mutex
);
1583 if (!cfs_status
.clinfo
|| !cfs_status
.clinfo
->nodes_byid
)
1584 goto ret
; /* ignore */
1586 cfs_clnode_t
*clnode
= g_hash_table_lookup(cfs_status
.clinfo
->nodes_byid
, &nodeid
);
1588 goto ret
; /* ignore */
1590 cfs_debug("got node %d status update %s", nodeid
, key
);
1592 if (strncmp(key
, "rrd/", 4) == 0) {
1593 rrdentry_hash_set(cfs_status
.rrdhash
, key
+ 4, data
, len
);
1594 } else if (!strcmp(key
, "nodeip")) {
1595 nodeip_hash_set(cfs_status
.iphash
, clnode
->name
, data
, len
);
1597 if (!clnode
->kvhash
) {
1598 if (!(clnode
->kvhash
= kventry_hash_new())) {
1599 goto ret
; /*ignore */
1603 kventry_hash_set(clnode
->kvhash
, key
, data
, len
);
1607 g_mutex_unlock (&mutex
);
1613 cfs_kvstore_sync(void)
1615 g_return_val_if_fail(cfs_status
.kvhash
!= NULL
, FALSE
);
1616 g_return_val_if_fail(cfs_status
.kvstore
!= NULL
, FALSE
);
1618 gboolean res
= TRUE
;
1620 g_mutex_lock (&mutex
);
1622 GHashTable
*ht
= cfs_status
.kvhash
;
1623 GHashTableIter iter
;
1624 gpointer key
, value
;
1626 g_hash_table_iter_init (&iter
, ht
);
1628 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1629 kventry_t
*entry
= (kventry_t
*)value
;
1630 kvstore_send_update_message(cfs_status
.kvstore
, entry
->key
, entry
->data
, entry
->len
);
1633 g_mutex_unlock (&mutex
);
1650 g_return_val_if_fail(dfsm
!= NULL
, -1);
1651 g_return_val_if_fail(msg
!= NULL
, -1);
1652 g_return_val_if_fail(res_ptr
!= NULL
, -1);
1654 /* ignore message for ourself */
1655 if (dfsm_nodeid_is_local(dfsm
, nodeid
, pid
))
1658 if (msg_type
== KVSTORE_MESSAGE_UPDATE
) {
1662 if (kvstore_parse_update_message(msg
, msg_len
, &key
, &data
, &len
)) {
1663 cfs_kvstore_node_set(nodeid
, key
, data
, len
);
1665 cfs_critical("cant parse update message");
1667 } else if (msg_type
== KVSTORE_MESSAGE_LOG
) {
1668 cfs_message("received log"); // fixme: remove
1669 const clog_entry_t
*entry
;
1670 if ((entry
= kvstore_parse_log_message(msg
, msg_len
))) {
1671 clusterlog_insert(cfs_status
.clusterlog
, entry
);
1673 cfs_critical("cant parse log message");
1676 cfs_critical("received unknown message type %d\n", msg_type
);
1693 const struct cpg_address
*member_list
,
1694 size_t member_list_entries
)
1696 g_return_if_fail(dfsm
!= NULL
);
1697 g_return_if_fail(member_list
!= NULL
);
1699 cfs_debug("enter %s", __func__
);
1701 g_mutex_lock (&mutex
);
1703 cfs_clinfo_t
*clinfo
= cfs_status
.clinfo
;
1705 if (clinfo
&& clinfo
->nodes_byid
) {
1707 GHashTable
*ht
= clinfo
->nodes_byid
;
1708 GHashTableIter iter
;
1709 gpointer key
, value
;
1711 g_hash_table_iter_init (&iter
, ht
);
1713 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1714 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
1715 node
->online
= FALSE
;
1718 for (int i
= 0; i
< member_list_entries
; i
++) {
1720 if ((node
= g_hash_table_lookup(clinfo
->nodes_byid
, &member_list
[i
].nodeid
))) {
1721 node
->online
= TRUE
;
1725 cfs_status
.clinfo_version
++;
1728 g_mutex_unlock (&mutex
);
1735 unsigned int *res_len
)
1737 g_return_val_if_fail(dfsm
!= NULL
, NULL
);
1739 gpointer msg
= clusterlog_get_state(cfs_status
.clusterlog
, res_len
);
1745 dfsm_process_update(
1748 dfsm_sync_info_t
*syncinfo
,
1754 cfs_critical("%s: received unexpected update message", __func__
);
1760 dfsm_process_state_update(
1763 dfsm_sync_info_t
*syncinfo
)
1765 g_return_val_if_fail(dfsm
!= NULL
, -1);
1766 g_return_val_if_fail(syncinfo
!= NULL
, -1);
1768 clog_base_t
*clog
[syncinfo
->node_count
];
1770 int local_index
= -1;
1771 for (int i
= 0; i
< syncinfo
->node_count
; i
++) {
1772 dfsm_node_info_t
*ni
= &syncinfo
->nodes
[i
];
1775 if (syncinfo
->local
== ni
)
1778 clog_base_t
*base
= (clog_base_t
*)ni
->state
;
1779 if (ni
->state_len
> 8 && ni
->state_len
== clog_size(base
)) {
1780 clog
[i
] = ni
->state
;
1782 cfs_critical("received log with wrong size %u", ni
->state_len
);
1787 if (!clusterlog_merge(cfs_status
.clusterlog
, clog
, syncinfo
->node_count
, local_index
)) {
1788 cfs_critical("unable to merge log files");
1800 dfsm_sync_info_t
*syncinfo
)
1802 g_return_val_if_fail(dfsm
!= NULL
, -1);
1803 g_return_val_if_fail(syncinfo
!= NULL
, -1);
1809 dfsm_synced(dfsm_t
*dfsm
)
1811 g_return_if_fail(dfsm
!= NULL
);
1813 char *ip
= (char *)g_hash_table_lookup(cfs_status
.iphash
, cfs
.nodename
);
1817 cfs_status_set("nodeip", ip
, strlen(ip
) + 1);
1824 dfsm_sync_info_t
*syncinfo
)
1829 static dfsm_callbacks_t kvstore_dfsm_callbacks
= {
1830 .dfsm_deliver_fn
= dfsm_deliver
,
1831 .dfsm_confchg_fn
= dfsm_confchg
,
1833 .dfsm_get_state_fn
= dfsm_get_state
,
1834 .dfsm_process_state_update_fn
= dfsm_process_state_update
,
1835 .dfsm_process_update_fn
= dfsm_process_update
,
1836 .dfsm_commit_fn
= dfsm_commit
,
1837 .dfsm_cleanup_fn
= dfsm_cleanup
,
1838 .dfsm_synced_fn
= dfsm_synced
,
1842 cfs_status_dfsm_new(void)
1844 g_mutex_lock (&mutex
);
1846 cfs_status
.kvstore
= dfsm_new(NULL
, KVSTORE_CPG_GROUP_NAME
, G_LOG_DOMAIN
,
1847 0, &kvstore_dfsm_callbacks
);
1848 g_mutex_unlock (&mutex
);
1850 return cfs_status
.kvstore
;
1854 cfs_is_quorate(void)
1856 g_mutex_lock (&mutex
);
1857 gboolean res
= cfs_status
.quorate
;
1858 g_mutex_unlock (&mutex
);
1868 g_mutex_lock (&mutex
);
1870 uint32_t prev_quorate
= cfs_status
.quorate
;
1871 cfs_status
.quorate
= quorate
;
1873 if (!prev_quorate
&& cfs_status
.quorate
) {
1875 cfs_message("node has quorum");
1878 if (prev_quorate
&& !cfs_status
.quorate
) {
1880 cfs_message("node lost quorum");
1883 g_mutex_unlock (&mutex
);