1 /*
2  * Copyright 2004-2021 the Pacemaker project contributors
3  *
4  * The version control history for this file may have further details.
5  *
6  * This source code is licensed under the GNU Lesser General Public License
7  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
8  */
9 
10 #include <crm_internal.h>
11 
12 #include <crm/pengine/rules.h>
13 #include <crm/pengine/internal.h>
14 #include <crm/msg_xml.h>
15 #include <crm/common/xml_internal.h>
16 
17 void populate_hash(xmlNode * nvpair_list, GHashTable * hash, const char **attrs, int attrs_length);
18 
19 resource_object_functions_t resource_class_functions[] = {
20     {
21          native_unpack,
22          native_find_rsc,
23          native_parameter,
24          native_print,
25          native_active,
26          native_resource_state,
27          native_location,
28          native_free,
29          pe__count_common,
30          pe__native_is_filtered,
31     },
32     {
33          group_unpack,
34          native_find_rsc,
35          native_parameter,
36          group_print,
37          group_active,
38          group_resource_state,
39          native_location,
40          group_free,
41          pe__count_common,
42          pe__group_is_filtered,
43     },
44     {
45          clone_unpack,
46          native_find_rsc,
47          native_parameter,
48          clone_print,
49          clone_active,
50          clone_resource_state,
51          native_location,
52          clone_free,
53          pe__count_common,
54          pe__clone_is_filtered,
55     },
56     {
57          pe__unpack_bundle,
58          native_find_rsc,
59          native_parameter,
60          pe__print_bundle,
61          pe__bundle_active,
62          pe__bundle_resource_state,
63          native_location,
64          pe__free_bundle,
65          pe__count_bundle,
66          pe__bundle_is_filtered,
67     }
68 };
69 
70 static enum pe_obj_types
get_resource_type(const char * name)71 get_resource_type(const char *name)
72 {
73     if (pcmk__str_eq(name, XML_CIB_TAG_RESOURCE, pcmk__str_casei)) {
74         return pe_native;
75 
76     } else if (pcmk__str_eq(name, XML_CIB_TAG_GROUP, pcmk__str_casei)) {
77         return pe_group;
78 
79     } else if (pcmk__str_eq(name, XML_CIB_TAG_INCARNATION, pcmk__str_casei)) {
80         return pe_clone;
81 
82     } else if (pcmk__str_eq(name, PCMK_XE_PROMOTABLE_LEGACY, pcmk__str_casei)) {
83         // @COMPAT deprecated since 2.0.0
84         return pe_clone;
85 
86     } else if (pcmk__str_eq(name, XML_CIB_TAG_CONTAINER, pcmk__str_casei)) {
87         return pe_container;
88     }
89 
90     return pe_unknown;
91 }
92 
93 static void
dup_attr(gpointer key,gpointer value,gpointer user_data)94 dup_attr(gpointer key, gpointer value, gpointer user_data)
95 {
96     add_hash_param(user_data, key, value);
97 }
98 
99 static void
expand_parents_fixed_nvpairs(pe_resource_t * rsc,pe_rule_eval_data_t * rule_data,GHashTable * meta_hash,pe_working_set_t * data_set)100 expand_parents_fixed_nvpairs(pe_resource_t * rsc, pe_rule_eval_data_t * rule_data, GHashTable * meta_hash, pe_working_set_t * data_set)
101 {
102     GHashTable *parent_orig_meta = pcmk__strkey_table(free, free);
103     pe_resource_t *p = rsc->parent;
104 
105     if (p == NULL) {
106         return ;
107     }
108 
109     /* Search all parent resources, get the fixed value of "meta_attributes" set only in the original xml, and stack it in the hash table. */
110     /* The fixed value of the lower parent resource takes precedence and is not overwritten. */
111     while(p != NULL) {
112         /* A hash table for comparison is generated, including the id-ref. */
113         pe__unpack_dataset_nvpairs(p->xml, XML_TAG_META_SETS,
114                                rule_data, parent_orig_meta, NULL, FALSE, data_set);
115         p = p->parent;
116     }
117 
118     /* If there is a fixed value of "meta_attributes" of the parent resource, it will be processed. */
119     if (parent_orig_meta != NULL) {
120         GHashTableIter iter;
121         char *key = NULL;
122         char *value = NULL;
123 
124         g_hash_table_iter_init(&iter, parent_orig_meta);
125         while (g_hash_table_iter_next(&iter, (gpointer *) &key, (gpointer *) &value)) {
126             /* Parameters set in the original xml of the parent resource will also try to overwrite the child resource. */
127             /* Attributes that already exist in the child lease are not updated. */
128             dup_attr(key, value, meta_hash);
129         }
130     }
131 
132     if (parent_orig_meta != NULL) {
133         g_hash_table_destroy(parent_orig_meta);
134     }
135 
136     return ;
137 
138 }
139 void
get_meta_attributes(GHashTable * meta_hash,pe_resource_t * rsc,pe_node_t * node,pe_working_set_t * data_set)140 get_meta_attributes(GHashTable * meta_hash, pe_resource_t * rsc,
141                     pe_node_t * node, pe_working_set_t * data_set)
142 {
143     pe_rsc_eval_data_t rsc_rule_data = {
144         .standard = crm_element_value(rsc->xml, XML_AGENT_ATTR_CLASS),
145         .provider = crm_element_value(rsc->xml, XML_AGENT_ATTR_PROVIDER),
146         .agent = crm_element_value(rsc->xml, XML_EXPR_ATTR_TYPE)
147     };
148 
149     pe_rule_eval_data_t rule_data = {
150         .node_hash = NULL,
151         .role = RSC_ROLE_UNKNOWN,
152         .now = data_set->now,
153         .match_data = NULL,
154         .rsc_data = &rsc_rule_data,
155         .op_data = NULL
156     };
157 
158     if (node) {
159         rule_data.node_hash = node->details->attrs;
160     }
161 
162     for (xmlAttrPtr a = pcmk__xe_first_attr(rsc->xml); a != NULL; a = a->next) {
163         const char *prop_name = (const char *) a->name;
164         const char *prop_value = crm_element_value(rsc->xml, prop_name);
165 
166         add_hash_param(meta_hash, prop_name, prop_value);
167     }
168 
169     pe__unpack_dataset_nvpairs(rsc->xml, XML_TAG_META_SETS, &rule_data,
170                                meta_hash, NULL, FALSE, data_set);
171 
172     /* Set the "meta_attributes" explicitly set in the parent resource to the hash table of the child resource. */
173     /* If it is already explicitly set as a child, it will not be overwritten. */
174     if (rsc->parent != NULL) {
175         expand_parents_fixed_nvpairs(rsc, &rule_data, meta_hash, data_set);
176     }
177 
178     /* check the defaults */
179     pe__unpack_dataset_nvpairs(data_set->rsc_defaults, XML_TAG_META_SETS,
180                                &rule_data, meta_hash, NULL, FALSE, data_set);
181 
182     /* If there is "meta_attributes" that the parent resource has not explicitly set, set a value that is not set from rsc_default either. */
183     /* The values already set up to this point will not be overwritten. */
184     if (rsc->parent) {
185         g_hash_table_foreach(rsc->parent->meta, dup_attr, meta_hash);
186     }
187 }
188 
189 void
get_rsc_attributes(GHashTable * meta_hash,pe_resource_t * rsc,pe_node_t * node,pe_working_set_t * data_set)190 get_rsc_attributes(GHashTable * meta_hash, pe_resource_t * rsc,
191                    pe_node_t * node, pe_working_set_t * data_set)
192 {
193     pe_rule_eval_data_t rule_data = {
194         .node_hash = NULL,
195         .role = RSC_ROLE_UNKNOWN,
196         .now = data_set->now,
197         .match_data = NULL,
198         .rsc_data = NULL,
199         .op_data = NULL
200     };
201 
202     if (node) {
203         rule_data.node_hash = node->details->attrs;
204     }
205 
206     pe__unpack_dataset_nvpairs(rsc->xml, XML_TAG_ATTR_SETS, &rule_data,
207                                meta_hash, NULL, FALSE, data_set);
208 
209     /* set anything else based on the parent */
210     if (rsc->parent != NULL) {
211         get_rsc_attributes(meta_hash, rsc->parent, node, data_set);
212 
213     } else {
214         /* and finally check the defaults */
215         pe__unpack_dataset_nvpairs(data_set->rsc_defaults, XML_TAG_ATTR_SETS,
216                                    &rule_data, meta_hash, NULL, FALSE, data_set);
217     }
218 }
219 
220 #if ENABLE_VERSIONED_ATTRS
221 void
pe_get_versioned_attributes(xmlNode * meta_hash,pe_resource_t * rsc,pe_node_t * node,pe_working_set_t * data_set)222 pe_get_versioned_attributes(xmlNode * meta_hash, pe_resource_t * rsc,
223                             pe_node_t * node, pe_working_set_t * data_set)
224 {
225     pe_rule_eval_data_t rule_data = {
226         .node_hash = (node == NULL)? NULL : node->details->attrs,
227         .role = RSC_ROLE_UNKNOWN,
228         .now = data_set->now,
229         .match_data = NULL,
230         .rsc_data = NULL,
231         .op_data = NULL
232     };
233 
234     pe_eval_versioned_attributes(data_set->input, rsc->xml, XML_TAG_ATTR_SETS,
235                                  &rule_data, meta_hash, NULL);
236 
237     /* set anything else based on the parent */
238     if (rsc->parent != NULL) {
239         pe_get_versioned_attributes(meta_hash, rsc->parent, node, data_set);
240 
241     } else {
242         /* and finally check the defaults */
243         pe_eval_versioned_attributes(data_set->input, data_set->rsc_defaults,
244                                      XML_TAG_ATTR_SETS, &rule_data, meta_hash,
245                                      NULL);
246     }
247 }
248 #endif
249 
250 static char *
template_op_key(xmlNode * op)251 template_op_key(xmlNode * op)
252 {
253     const char *name = crm_element_value(op, "name");
254     const char *role = crm_element_value(op, "role");
255     char *key = NULL;
256 
257     if ((role == NULL)
258         || pcmk__strcase_any_of(role, RSC_ROLE_STARTED_S, RSC_ROLE_UNPROMOTED_S,
259                                 RSC_ROLE_UNPROMOTED_LEGACY_S, NULL)) {
260         role = RSC_ROLE_UNKNOWN_S;
261     }
262 
263     key = crm_strdup_printf("%s-%s", name, role);
264     return key;
265 }
266 
267 static gboolean
unpack_template(xmlNode * xml_obj,xmlNode ** expanded_xml,pe_working_set_t * data_set)268 unpack_template(xmlNode * xml_obj, xmlNode ** expanded_xml, pe_working_set_t * data_set)
269 {
270     xmlNode *cib_resources = NULL;
271     xmlNode *template = NULL;
272     xmlNode *new_xml = NULL;
273     xmlNode *child_xml = NULL;
274     xmlNode *rsc_ops = NULL;
275     xmlNode *template_ops = NULL;
276     const char *template_ref = NULL;
277     const char *clone = NULL;
278     const char *id = NULL;
279 
280     if (xml_obj == NULL) {
281         pe_err("No resource object for template unpacking");
282         return FALSE;
283     }
284 
285     template_ref = crm_element_value(xml_obj, XML_CIB_TAG_RSC_TEMPLATE);
286     if (template_ref == NULL) {
287         return TRUE;
288     }
289 
290     id = ID(xml_obj);
291     if (id == NULL) {
292         pe_err("'%s' object must have a id", crm_element_name(xml_obj));
293         return FALSE;
294     }
295 
296     if (pcmk__str_eq(template_ref, id, pcmk__str_none)) {
297         pe_err("The resource object '%s' should not reference itself", id);
298         return FALSE;
299     }
300 
301     cib_resources = get_xpath_object("//"XML_CIB_TAG_RESOURCES, data_set->input, LOG_TRACE);
302     if (cib_resources == NULL) {
303         pe_err("No resources configured");
304         return FALSE;
305     }
306 
307     template = pcmk__xe_match(cib_resources, XML_CIB_TAG_RSC_TEMPLATE,
308                               XML_ATTR_ID, template_ref);
309     if (template == NULL) {
310         pe_err("No template named '%s'", template_ref);
311         return FALSE;
312     }
313 
314     new_xml = copy_xml(template);
315     xmlNodeSetName(new_xml, xml_obj->name);
316     crm_xml_replace(new_xml, XML_ATTR_ID, id);
317 
318     clone = crm_element_value(xml_obj, XML_RSC_ATTR_INCARNATION);
319     if(clone) {
320         crm_xml_add(new_xml, XML_RSC_ATTR_INCARNATION, clone);
321     }
322 
323     template_ops = find_xml_node(new_xml, "operations", FALSE);
324 
325     for (child_xml = pcmk__xe_first_child(xml_obj); child_xml != NULL;
326          child_xml = pcmk__xe_next(child_xml)) {
327         xmlNode *new_child = NULL;
328 
329         new_child = add_node_copy(new_xml, child_xml);
330 
331         if (pcmk__str_eq((const char *)new_child->name, "operations", pcmk__str_none)) {
332             rsc_ops = new_child;
333         }
334     }
335 
336     if (template_ops && rsc_ops) {
337         xmlNode *op = NULL;
338         GHashTable *rsc_ops_hash = pcmk__strkey_table(free, NULL);
339 
340         for (op = pcmk__xe_first_child(rsc_ops); op != NULL;
341              op = pcmk__xe_next(op)) {
342 
343             char *key = template_op_key(op);
344 
345             g_hash_table_insert(rsc_ops_hash, key, op);
346         }
347 
348         for (op = pcmk__xe_first_child(template_ops); op != NULL;
349              op = pcmk__xe_next(op)) {
350 
351             char *key = template_op_key(op);
352 
353             if (g_hash_table_lookup(rsc_ops_hash, key) == NULL) {
354                 add_node_copy(rsc_ops, op);
355             }
356 
357             free(key);
358         }
359 
360         if (rsc_ops_hash) {
361             g_hash_table_destroy(rsc_ops_hash);
362         }
363 
364         free_xml(template_ops);
365     }
366 
367     /*free_xml(*expanded_xml); */
368     *expanded_xml = new_xml;
369 
370     /* Disable multi-level templates for now */
371     /*if(unpack_template(new_xml, expanded_xml, data_set) == FALSE) {
372        free_xml(*expanded_xml);
373        *expanded_xml = NULL;
374 
375        return FALSE;
376        } */
377 
378     return TRUE;
379 }
380 
381 static gboolean
add_template_rsc(xmlNode * xml_obj,pe_working_set_t * data_set)382 add_template_rsc(xmlNode * xml_obj, pe_working_set_t * data_set)
383 {
384     const char *template_ref = NULL;
385     const char *id = NULL;
386 
387     if (xml_obj == NULL) {
388         pe_err("No resource object for processing resource list of template");
389         return FALSE;
390     }
391 
392     template_ref = crm_element_value(xml_obj, XML_CIB_TAG_RSC_TEMPLATE);
393     if (template_ref == NULL) {
394         return TRUE;
395     }
396 
397     id = ID(xml_obj);
398     if (id == NULL) {
399         pe_err("'%s' object must have a id", crm_element_name(xml_obj));
400         return FALSE;
401     }
402 
403     if (pcmk__str_eq(template_ref, id, pcmk__str_none)) {
404         pe_err("The resource object '%s' should not reference itself", id);
405         return FALSE;
406     }
407 
408     if (add_tag_ref(data_set->template_rsc_sets, template_ref, id) == FALSE) {
409         return FALSE;
410     }
411 
412     return TRUE;
413 }
414 
415 static bool
detect_promotable(pe_resource_t * rsc)416 detect_promotable(pe_resource_t *rsc)
417 {
418     const char *promotable = g_hash_table_lookup(rsc->meta,
419                                                  XML_RSC_ATTR_PROMOTABLE);
420 
421     if (crm_is_true(promotable)) {
422         return TRUE;
423     }
424 
425     // @COMPAT deprecated since 2.0.0
426     if (pcmk__str_eq(crm_element_name(rsc->xml), PCMK_XE_PROMOTABLE_LEGACY,
427                      pcmk__str_casei)) {
428         /* @TODO in some future version, pe_warn_once() here,
429          *       then drop support in even later version
430          */
431         g_hash_table_insert(rsc->meta, strdup(XML_RSC_ATTR_PROMOTABLE),
432                             strdup(XML_BOOLEAN_TRUE));
433         return TRUE;
434     }
435     return FALSE;
436 }
437 
438 static void
free_params_table(gpointer data)439 free_params_table(gpointer data)
440 {
441     g_hash_table_destroy((GHashTable *) data);
442 }
443 
444 /*!
445  * \brief Get a table of resource parameters
446  *
447  * \param[in] rsc       Resource to query
448  * \param[in] node      Node for evaluating rules (NULL for defaults)
449  * \param[in] data_set  Cluster working set
450  *
451  * \return Hash table containing resource parameter names and values
452  *         (or NULL if \p rsc or \p data_set is NULL)
453  * \note The returned table will be destroyed when the resource is freed, so
454  *       callers should not destroy it.
455  */
456 GHashTable *
pe_rsc_params(pe_resource_t * rsc,pe_node_t * node,pe_working_set_t * data_set)457 pe_rsc_params(pe_resource_t *rsc, pe_node_t *node, pe_working_set_t *data_set)
458 {
459     GHashTable *params_on_node = NULL;
460 
461     /* A NULL node is used to request the resource's default parameters
462      * (not evaluated for node), but we always want something non-NULL
463      * as a hash table key.
464      */
465     const char *node_name = "";
466 
467     // Sanity check
468     if ((rsc == NULL) || (data_set == NULL)) {
469         return NULL;
470     }
471     if ((node != NULL) && (node->details->uname != NULL)) {
472         node_name = node->details->uname;
473     }
474 
475     // Find the parameter table for given node
476     if (rsc->parameter_cache == NULL) {
477         rsc->parameter_cache = pcmk__strikey_table(free, free_params_table);
478     } else {
479         params_on_node = g_hash_table_lookup(rsc->parameter_cache, node_name);
480     }
481 
482     // If none exists yet, create one with parameters evaluated for node
483     if (params_on_node == NULL) {
484         params_on_node = pcmk__strkey_table(free, free);
485         get_rsc_attributes(params_on_node, rsc, node, data_set);
486         g_hash_table_insert(rsc->parameter_cache, strdup(node_name),
487                             params_on_node);
488     }
489     return params_on_node;
490 }
491 
492 gboolean
common_unpack(xmlNode * xml_obj,pe_resource_t ** rsc,pe_resource_t * parent,pe_working_set_t * data_set)493 common_unpack(xmlNode * xml_obj, pe_resource_t ** rsc,
494               pe_resource_t * parent, pe_working_set_t * data_set)
495 {
496     bool isdefault = FALSE;
497     xmlNode *expanded_xml = NULL;
498     xmlNode *ops = NULL;
499     const char *value = NULL;
500     const char *rclass = NULL; /* Look for this after any templates have been expanded */
501     const char *id = crm_element_value(xml_obj, XML_ATTR_ID);
502     bool guest_node = FALSE;
503     bool remote_node = FALSE;
504     bool has_versioned_params = FALSE;
505 
506     pe_rule_eval_data_t rule_data = {
507         .node_hash = NULL,
508         .role = RSC_ROLE_UNKNOWN,
509         .now = data_set->now,
510         .match_data = NULL,
511         .rsc_data = NULL,
512         .op_data = NULL
513     };
514 
515     crm_log_xml_trace(xml_obj, "Processing resource input...");
516 
517     if (id == NULL) {
518         pe_err("Must specify id tag in <resource>");
519         return FALSE;
520 
521     } else if (rsc == NULL) {
522         pe_err("Nowhere to unpack resource into");
523         return FALSE;
524 
525     }
526 
527     if (unpack_template(xml_obj, &expanded_xml, data_set) == FALSE) {
528         return FALSE;
529     }
530 
531     *rsc = calloc(1, sizeof(pe_resource_t));
532     (*rsc)->cluster = data_set;
533 
534     if (expanded_xml) {
535         crm_log_xml_trace(expanded_xml, "Expanded resource...");
536         (*rsc)->xml = expanded_xml;
537         (*rsc)->orig_xml = xml_obj;
538 
539     } else {
540         (*rsc)->xml = xml_obj;
541         (*rsc)->orig_xml = NULL;
542     }
543 
544     /* Do not use xml_obj from here on, use (*rsc)->xml in case templates are involved */
545     rclass = crm_element_value((*rsc)->xml, XML_AGENT_ATTR_CLASS);
546     (*rsc)->parent = parent;
547 
548     ops = find_xml_node((*rsc)->xml, "operations", FALSE);
549     (*rsc)->ops_xml = expand_idref(ops, data_set->input);
550 
551     (*rsc)->variant = get_resource_type(crm_element_name((*rsc)->xml));
552     if ((*rsc)->variant == pe_unknown) {
553         pe_err("Unknown resource type: %s", crm_element_name((*rsc)->xml));
554         free(*rsc);
555         return FALSE;
556     }
557 
558 #if ENABLE_VERSIONED_ATTRS
559     (*rsc)->versioned_parameters = create_xml_node(NULL, XML_TAG_RSC_VER_ATTRS);
560 #endif
561 
562     (*rsc)->meta = pcmk__strkey_table(free, free);
563     (*rsc)->allowed_nodes = pcmk__strkey_table(NULL, free);
564     (*rsc)->known_on = pcmk__strkey_table(NULL, free);
565 
566     value = crm_element_value((*rsc)->xml, XML_RSC_ATTR_INCARNATION);
567     if (value) {
568         (*rsc)->id = crm_strdup_printf("%s:%s", id, value);
569         add_hash_param((*rsc)->meta, XML_RSC_ATTR_INCARNATION, value);
570 
571     } else {
572         (*rsc)->id = strdup(id);
573     }
574 
575     (*rsc)->fns = &resource_class_functions[(*rsc)->variant];
576     pe_rsc_trace((*rsc), "Unpacking resource...");
577 
578     get_meta_attributes((*rsc)->meta, *rsc, NULL, data_set);
579     (*rsc)->parameters = pe_rsc_params(*rsc, NULL, data_set); // \deprecated
580 #if ENABLE_VERSIONED_ATTRS
581     pe_get_versioned_attributes((*rsc)->versioned_parameters, *rsc, NULL, data_set);
582 #endif
583 
584     (*rsc)->flags = 0;
585     pe__set_resource_flags(*rsc, pe_rsc_runnable|pe_rsc_provisional);
586 
587     if (!pcmk_is_set(data_set->flags, pe_flag_maintenance_mode)) {
588         pe__set_resource_flags(*rsc, pe_rsc_managed);
589     }
590 
591     (*rsc)->rsc_cons = NULL;
592     (*rsc)->rsc_tickets = NULL;
593     (*rsc)->actions = NULL;
594     (*rsc)->role = RSC_ROLE_STOPPED;
595     (*rsc)->next_role = RSC_ROLE_UNKNOWN;
596 
597     (*rsc)->recovery_type = recovery_stop_start;
598     (*rsc)->stickiness = 0;
599     (*rsc)->migration_threshold = INFINITY;
600     (*rsc)->failure_timeout = 0;
601 
602     value = g_hash_table_lookup((*rsc)->meta, XML_CIB_ATTR_PRIORITY);
603     (*rsc)->priority = char2score(value);
604 
605     value = g_hash_table_lookup((*rsc)->meta, XML_RSC_ATTR_CRITICAL);
606     if ((value == NULL) || crm_is_true(value)) {
607         pe__set_resource_flags(*rsc, pe_rsc_critical);
608     }
609 
610     value = g_hash_table_lookup((*rsc)->meta, XML_RSC_ATTR_NOTIFY);
611     if (crm_is_true(value)) {
612         pe__set_resource_flags(*rsc, pe_rsc_notify);
613     }
614 
615     if (xml_contains_remote_node((*rsc)->xml)) {
616         (*rsc)->is_remote_node = TRUE;
617         if (g_hash_table_lookup((*rsc)->meta, XML_RSC_ATTR_CONTAINER)) {
618             guest_node = TRUE;
619         } else {
620             remote_node = TRUE;
621         }
622     }
623 
624     value = g_hash_table_lookup((*rsc)->meta, XML_OP_ATTR_ALLOW_MIGRATE);
625 #if ENABLE_VERSIONED_ATTRS
626     has_versioned_params = xml_has_children((*rsc)->versioned_parameters);
627 #endif
628     if (crm_is_true(value) && has_versioned_params) {
629         pe_rsc_trace((*rsc), "Migration is disabled for resources with versioned parameters");
630     } else if (crm_is_true(value)) {
631         pe__set_resource_flags(*rsc, pe_rsc_allow_migrate);
632     } else if ((value == NULL) && remote_node && !has_versioned_params) {
633         /* By default, we want remote nodes to be able
634          * to float around the cluster without having to stop all the
635          * resources within the remote-node before moving. Allowing
636          * migration support enables this feature. If this ever causes
637          * problems, migration support can be explicitly turned off with
638          * allow-migrate=false.
639          * We don't support migration for versioned resources, though. */
640         pe__set_resource_flags(*rsc, pe_rsc_allow_migrate);
641     }
642 
643     value = g_hash_table_lookup((*rsc)->meta, XML_RSC_ATTR_MANAGED);
644     if (value != NULL && !pcmk__str_eq("default", value, pcmk__str_casei)) {
645         if (crm_is_true(value)) {
646             pe__set_resource_flags(*rsc, pe_rsc_managed);
647         } else {
648             pe__clear_resource_flags(*rsc, pe_rsc_managed);
649         }
650     }
651 
652     value = g_hash_table_lookup((*rsc)->meta, XML_RSC_ATTR_MAINTENANCE);
653     if (crm_is_true(value)) {
654         pe__clear_resource_flags(*rsc, pe_rsc_managed);
655         pe__set_resource_flags(*rsc, pe_rsc_maintenance);
656     }
657     if (pcmk_is_set(data_set->flags, pe_flag_maintenance_mode)) {
658         pe__clear_resource_flags(*rsc, pe_rsc_managed);
659         pe__set_resource_flags(*rsc, pe_rsc_maintenance);
660     }
661 
662     if (pe_rsc_is_clone(uber_parent(*rsc))) {
663         value = g_hash_table_lookup((*rsc)->meta, XML_RSC_ATTR_UNIQUE);
664         if (crm_is_true(value)) {
665             pe__set_resource_flags(*rsc, pe_rsc_unique);
666         }
667         if (detect_promotable(*rsc)) {
668             pe__set_resource_flags(*rsc, pe_rsc_promotable);
669         }
670     } else {
671         pe__set_resource_flags(*rsc, pe_rsc_unique);
672     }
673 
674     pe_rsc_trace((*rsc), "Options for %s", (*rsc)->id);
675 
676     value = g_hash_table_lookup((*rsc)->meta, XML_RSC_ATTR_RESTART);
677     if (pcmk__str_eq(value, "restart", pcmk__str_casei)) {
678         (*rsc)->restart_type = pe_restart_restart;
679         pe_rsc_trace((*rsc), "\tDependency restart handling: restart");
680         pe_warn_once(pe_wo_restart_type,
681                      "Support for restart-type is deprecated and will be removed in a future release");
682 
683     } else {
684         (*rsc)->restart_type = pe_restart_ignore;
685         pe_rsc_trace((*rsc), "\tDependency restart handling: ignore");
686     }
687 
688     value = g_hash_table_lookup((*rsc)->meta, XML_RSC_ATTR_MULTIPLE);
689     if (pcmk__str_eq(value, "stop_only", pcmk__str_casei)) {
690         (*rsc)->recovery_type = recovery_stop_only;
691         pe_rsc_trace((*rsc), "\tMultiple running resource recovery: stop only");
692 
693     } else if (pcmk__str_eq(value, "block", pcmk__str_casei)) {
694         (*rsc)->recovery_type = recovery_block;
695         pe_rsc_trace((*rsc), "\tMultiple running resource recovery: block");
696 
697     } else {
698         (*rsc)->recovery_type = recovery_stop_start;
699         pe_rsc_trace((*rsc), "\tMultiple running resource recovery: stop/start");
700     }
701 
702     value = g_hash_table_lookup((*rsc)->meta, XML_RSC_ATTR_STICKINESS);
703     if (value != NULL && !pcmk__str_eq("default", value, pcmk__str_casei)) {
704         (*rsc)->stickiness = char2score(value);
705     }
706 
707     value = g_hash_table_lookup((*rsc)->meta, XML_RSC_ATTR_FAIL_STICKINESS);
708     if (value != NULL && !pcmk__str_eq("default", value, pcmk__str_casei)) {
709         (*rsc)->migration_threshold = char2score(value);
710         if ((*rsc)->migration_threshold < 0) {
711             /* @TODO We use 1 here to preserve previous behavior, but this
712              * should probably use the default (INFINITY) or 0 (to disable)
713              * instead.
714              */
715             pe_warn_once(pe_wo_neg_threshold,
716                          XML_RSC_ATTR_FAIL_STICKINESS
717                          " must be non-negative, using 1 instead");
718             (*rsc)->migration_threshold = 1;
719         }
720     }
721 
722     if (pcmk__str_eq(rclass, PCMK_RESOURCE_CLASS_STONITH, pcmk__str_casei)) {
723         pe__set_working_set_flags(data_set, pe_flag_have_stonith_resource);
724         pe__set_resource_flags(*rsc, pe_rsc_fence_device);
725     }
726 
727     value = g_hash_table_lookup((*rsc)->meta, XML_RSC_ATTR_REQUIRES);
728 
729   handle_requires_pref:
730     if (pcmk__str_eq(value, "nothing", pcmk__str_casei)) {
731 
732     } else if (pcmk__str_eq(value, "quorum", pcmk__str_casei)) {
733         pe__set_resource_flags(*rsc, pe_rsc_needs_quorum);
734 
735     } else if (pcmk__str_eq(value, "unfencing", pcmk__str_casei)) {
736         if (pcmk_is_set((*rsc)->flags, pe_rsc_fence_device)) {
737             pcmk__config_warn("Resetting '" XML_RSC_ATTR_REQUIRES "' for %s "
738                               "to 'quorum' because fencing devices cannot "
739                               "require unfencing", (*rsc)->id);
740             value = "quorum";
741             isdefault = TRUE;
742             goto handle_requires_pref;
743 
744         } else if (!pcmk_is_set(data_set->flags, pe_flag_stonith_enabled)) {
745             pcmk__config_warn("Resetting '" XML_RSC_ATTR_REQUIRES "' for %s "
746                               "to 'quorum' because fencing is disabled",
747                               (*rsc)->id);
748             value = "quorum";
749             isdefault = TRUE;
750             goto handle_requires_pref;
751 
752         } else {
753             pe__set_resource_flags(*rsc, pe_rsc_needs_fencing
754                                            |pe_rsc_needs_unfencing);
755         }
756 
757     } else if (pcmk__str_eq(value, "fencing", pcmk__str_casei)) {
758         pe__set_resource_flags(*rsc, pe_rsc_needs_fencing);
759         if (!pcmk_is_set(data_set->flags, pe_flag_stonith_enabled)) {
760             pcmk__config_warn("%s requires fencing but fencing is disabled",
761                               (*rsc)->id);
762         }
763 
764     } else {
765         const char *orig_value = value;
766 
767         isdefault = TRUE;
768         if (pcmk_is_set((*rsc)->flags, pe_rsc_fence_device)) {
769             value = "quorum";
770 
771         } else if (((*rsc)->variant == pe_native)
772                    && pcmk__str_eq(crm_element_value((*rsc)->xml, XML_AGENT_ATTR_CLASS), PCMK_RESOURCE_CLASS_OCF, pcmk__str_casei)
773                    && pcmk__str_eq(crm_element_value((*rsc)->xml, XML_AGENT_ATTR_PROVIDER), "pacemaker", pcmk__str_casei)
774                    && pcmk__str_eq(crm_element_value((*rsc)->xml, XML_ATTR_TYPE), "remote", pcmk__str_casei)
775             ) {
776             value = "quorum";
777 
778         } else if (pcmk_is_set(data_set->flags, pe_flag_enable_unfencing)) {
779             value = "unfencing";
780 
781         } else if (pcmk_is_set(data_set->flags, pe_flag_stonith_enabled)) {
782             value = "fencing";
783 
784         } else if (data_set->no_quorum_policy == no_quorum_ignore) {
785             value = "nothing";
786 
787         } else {
788             value = "quorum";
789         }
790 
791         if (orig_value != NULL) {
792             pcmk__config_err("Resetting '" XML_RSC_ATTR_REQUIRES "' for %s "
793                              "to '%s' because '%s' is not valid",
794                               (*rsc)->id, value, orig_value);
795         }
796 
797         goto handle_requires_pref;
798     }
799 
800     pe_rsc_trace((*rsc), "\tRequired to start: %s%s", value, isdefault?" (default)":"");
801     value = g_hash_table_lookup((*rsc)->meta, XML_RSC_ATTR_FAIL_TIMEOUT);
802     if (value != NULL) {
803         // Stored as seconds
804         (*rsc)->failure_timeout = (int) (crm_parse_interval_spec(value) / 1000);
805     }
806 
807     if (remote_node) {
808         GHashTable *params = pe_rsc_params(*rsc, NULL, data_set);
809 
810         /* Grabbing the value now means that any rules based on node attributes
811          * will evaluate to false, so such rules should not be used with
812          * reconnect_interval.
813          *
814          * @TODO Evaluate per node before using
815          */
816         value = g_hash_table_lookup(params, XML_REMOTE_ATTR_RECONNECT_INTERVAL);
817         if (value) {
818             /* reconnect delay works by setting failure_timeout and preventing the
819              * connection from starting until the failure is cleared. */
820             (*rsc)->remote_reconnect_ms = crm_parse_interval_spec(value);
821             /* we want to override any default failure_timeout in use when remote
822              * reconnect_interval is in use. */
823             (*rsc)->failure_timeout = (*rsc)->remote_reconnect_ms / 1000;
824         }
825     }
826 
827     get_target_role(*rsc, &((*rsc)->next_role));
828     pe_rsc_trace((*rsc), "\tDesired next state: %s",
829                  (*rsc)->next_role != RSC_ROLE_UNKNOWN ? role2text((*rsc)->next_role) : "default");
830 
831     if ((*rsc)->fns->unpack(*rsc, data_set) == FALSE) {
832         return FALSE;
833     }
834 
835     if (pcmk_is_set(data_set->flags, pe_flag_symmetric_cluster)) {
836         // This tag must stay exactly the same because it is tested elsewhere
837         resource_location(*rsc, NULL, 0, "symmetric_default", data_set);
838     } else if (guest_node) {
839         /* remote resources tied to a container resource must always be allowed
840          * to opt-in to the cluster. Whether the connection resource is actually
841          * allowed to be placed on a node is dependent on the container resource */
842         resource_location(*rsc, NULL, 0, "remote_connection_default", data_set);
843     }
844 
845     pe_rsc_trace((*rsc), "\tAction notification: %s",
846                  pcmk_is_set((*rsc)->flags, pe_rsc_notify)? "required" : "not required");
847 
848     (*rsc)->utilization = pcmk__strkey_table(free, free);
849 
850     pe__unpack_dataset_nvpairs((*rsc)->xml, XML_TAG_UTILIZATION, &rule_data,
851                                (*rsc)->utilization, NULL, FALSE, data_set);
852 
853 /* 	data_set->resources = g_list_append(data_set->resources, (*rsc)); */
854 
855     if (expanded_xml) {
856         if (add_template_rsc(xml_obj, data_set) == FALSE) {
857             return FALSE;
858         }
859     }
860     return TRUE;
861 }
862 
863 void
common_update_score(pe_resource_t * rsc,const char * id,int score)864 common_update_score(pe_resource_t * rsc, const char *id, int score)
865 {
866     pe_node_t *node = NULL;
867 
868     node = pe_hash_table_lookup(rsc->allowed_nodes, id);
869     if (node != NULL) {
870         pe_rsc_trace(rsc, "Updating score for %s on %s: %d + %d", rsc->id, id, node->weight, score);
871         node->weight = pe__add_scores(node->weight, score);
872     }
873 
874     if (rsc->children) {
875         GList *gIter = rsc->children;
876 
877         for (; gIter != NULL; gIter = gIter->next) {
878             pe_resource_t *child_rsc = (pe_resource_t *) gIter->data;
879 
880             common_update_score(child_rsc, id, score);
881         }
882     }
883 }
884 
885 gboolean
is_parent(pe_resource_t * child,pe_resource_t * rsc)886 is_parent(pe_resource_t *child, pe_resource_t *rsc)
887 {
888     pe_resource_t *parent = child;
889 
890     if (parent == NULL || rsc == NULL) {
891         return FALSE;
892     }
893     while (parent->parent != NULL) {
894         if (parent->parent == rsc) {
895             return TRUE;
896         }
897         parent = parent->parent;
898     }
899     return FALSE;
900 }
901 
902 pe_resource_t *
uber_parent(pe_resource_t * rsc)903 uber_parent(pe_resource_t * rsc)
904 {
905     pe_resource_t *parent = rsc;
906 
907     if (parent == NULL) {
908         return NULL;
909     }
910     while (parent->parent != NULL && parent->parent->variant != pe_container) {
911         parent = parent->parent;
912     }
913     return parent;
914 }
915 
916 void
common_free(pe_resource_t * rsc)917 common_free(pe_resource_t * rsc)
918 {
919     if (rsc == NULL) {
920         return;
921     }
922 
923     pe_rsc_trace(rsc, "Freeing %s %d", rsc->id, rsc->variant);
924 
925     g_list_free(rsc->rsc_cons);
926     g_list_free(rsc->rsc_cons_lhs);
927     g_list_free(rsc->rsc_tickets);
928     g_list_free(rsc->dangling_migrations);
929 
930     if (rsc->parameter_cache != NULL) {
931         g_hash_table_destroy(rsc->parameter_cache);
932     }
933 #if ENABLE_VERSIONED_ATTRS
934     if (rsc->versioned_parameters != NULL) {
935         free_xml(rsc->versioned_parameters);
936     }
937 #endif
938     if (rsc->meta != NULL) {
939         g_hash_table_destroy(rsc->meta);
940     }
941     if (rsc->utilization != NULL) {
942         g_hash_table_destroy(rsc->utilization);
943     }
944 
945     if ((rsc->parent == NULL) && pcmk_is_set(rsc->flags, pe_rsc_orphan)) {
946         free_xml(rsc->xml);
947         rsc->xml = NULL;
948         free_xml(rsc->orig_xml);
949         rsc->orig_xml = NULL;
950 
951         /* if rsc->orig_xml, then rsc->xml is an expanded xml from a template */
952     } else if (rsc->orig_xml) {
953         free_xml(rsc->xml);
954         rsc->xml = NULL;
955     }
956     if (rsc->running_on) {
957         g_list_free(rsc->running_on);
958         rsc->running_on = NULL;
959     }
960     if (rsc->known_on) {
961         g_hash_table_destroy(rsc->known_on);
962         rsc->known_on = NULL;
963     }
964     if (rsc->actions) {
965         g_list_free(rsc->actions);
966         rsc->actions = NULL;
967     }
968     if (rsc->allowed_nodes) {
969         g_hash_table_destroy(rsc->allowed_nodes);
970         rsc->allowed_nodes = NULL;
971     }
972     g_list_free(rsc->fillers);
973     g_list_free(rsc->rsc_location);
974     pe_rsc_trace(rsc, "Resource freed");
975     free(rsc->id);
976     free(rsc->clone_name);
977     free(rsc->allocated_to);
978     free(rsc->variant_opaque);
979     free(rsc->pending_task);
980     free(rsc);
981 }
982 
983 /*!
984  * \brief
985  * \internal Find a node (and optionally count all) where resource is active
986  *
987  * \param[in]  rsc          Resource to check
988  * \param[out] count_all    If not NULL, will be set to count of active nodes
989  * \param[out] count_clean  If not NULL, will be set to count of clean nodes
990  *
991  * \return An active node (or NULL if resource is not active anywhere)
992  *
993  * \note The order of preference is: an active node that is the resource's
994  *       partial migration source; if the resource's "requires" is "quorum" or
995  *       "nothing", the first active node in the list that is clean and online;
996  *       the first active node in the list.
997  */
998 pe_node_t *
pe__find_active_on(const pe_resource_t * rsc,unsigned int * count_all,unsigned int * count_clean)999 pe__find_active_on(const pe_resource_t *rsc, unsigned int *count_all,
1000                    unsigned int *count_clean)
1001 {
1002     pe_node_t *active = NULL;
1003     pe_node_t *node = NULL;
1004     bool keep_looking = FALSE;
1005     bool is_happy = FALSE;
1006 
1007     if (count_all) {
1008         *count_all = 0;
1009     }
1010     if (count_clean) {
1011         *count_clean = 0;
1012     }
1013     if (rsc == NULL) {
1014         return NULL;
1015     }
1016 
1017     for (GList *node_iter = rsc->running_on; node_iter != NULL;
1018          node_iter = node_iter->next) {
1019 
1020         node = node_iter->data;
1021         keep_looking = FALSE;
1022 
1023         is_happy = node->details->online && !node->details->unclean;
1024 
1025         if (count_all) {
1026             ++*count_all;
1027         }
1028         if (count_clean && is_happy) {
1029             ++*count_clean;
1030         }
1031         if (count_all || count_clean) {
1032             // If we're counting, we need to go through entire list
1033             keep_looking = TRUE;
1034         }
1035 
1036         if (rsc->partial_migration_source != NULL) {
1037             if (node->details == rsc->partial_migration_source->details) {
1038                 // This is the migration source
1039                 active = node;
1040             } else {
1041                 keep_looking = TRUE;
1042             }
1043         } else if (!pcmk_is_set(rsc->flags, pe_rsc_needs_fencing)) {
1044             if (is_happy && (!active || !active->details->online
1045                              || active->details->unclean)) {
1046                 // This is the first clean node
1047                 active = node;
1048             } else {
1049                 keep_looking = TRUE;
1050             }
1051         }
1052         if (active == NULL) {
1053             // This is first node in list
1054             active = node;
1055         }
1056 
1057         if (keep_looking == FALSE) {
1058             // Don't waste time iterating if we don't have to
1059             break;
1060         }
1061     }
1062     return active;
1063 }
1064 
1065 /*!
1066  * \brief
1067  * \internal Find and count active nodes according to "requires"
1068  *
1069  * \param[in]  rsc    Resource to check
1070  * \param[out] count  If not NULL, will be set to count of active nodes
1071  *
1072  * \return An active node (or NULL if resource is not active anywhere)
1073  *
1074  * \note This is a convenience wrapper for pe__find_active_on() where the count
1075  *       of all active nodes or only clean active nodes is desired according to
1076  *       the "requires" meta-attribute.
1077  */
1078 pe_node_t *
pe__find_active_requires(const pe_resource_t * rsc,unsigned int * count)1079 pe__find_active_requires(const pe_resource_t *rsc, unsigned int *count)
1080 {
1081     if (rsc && !pcmk_is_set(rsc->flags, pe_rsc_needs_fencing)) {
1082         return pe__find_active_on(rsc, NULL, count);
1083     }
1084     return pe__find_active_on(rsc, count, NULL);
1085 }
1086 
1087 void
pe__count_common(pe_resource_t * rsc)1088 pe__count_common(pe_resource_t *rsc)
1089 {
1090     if (rsc->children != NULL) {
1091         for (GList *item = rsc->children; item != NULL; item = item->next) {
1092             ((pe_resource_t *) item->data)->fns->count(item->data);
1093         }
1094 
1095     } else if (!pcmk_is_set(rsc->flags, pe_rsc_orphan)
1096                || (rsc->role > RSC_ROLE_STOPPED)) {
1097         rsc->cluster->ninstances++;
1098         if (pe__resource_is_disabled(rsc)) {
1099             rsc->cluster->disabled_resources++;
1100         }
1101         if (pcmk_is_set(rsc->flags, pe_rsc_block)) {
1102             rsc->cluster->blocked_resources++;
1103         }
1104     }
1105 }
1106 
1107 /*!
1108  * \internal
1109  * \brief Update a resource's next role
1110  *
1111  * \param[in,out] rsc   Resource to be updated
1112  * \param[in]     role  Resource's new next role
1113  * \param[in]     why   Human-friendly reason why role is changing (for logs)
1114  */
1115 void
pe__set_next_role(pe_resource_t * rsc,enum rsc_role_e role,const char * why)1116 pe__set_next_role(pe_resource_t *rsc, enum rsc_role_e role, const char *why)
1117 {
1118     CRM_ASSERT((rsc != NULL) && (why != NULL));
1119     if (rsc->next_role != role) {
1120         pe_rsc_trace(rsc, "Resetting next role for %s from %s to %s (%s)",
1121                      rsc->id, role2text(rsc->next_role), role2text(role), why);
1122         rsc->next_role = role;
1123     }
1124 }
1125