root/lib/pengine/complex.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. get_resource_type
  2. dup_attr
  3. expand_parents_fixed_nvpairs
  4. get_meta_attributes
  5. get_rsc_attributes
  6. template_op_key
  7. unpack_template
  8. add_template_rsc
  9. detect_promotable
  10. free_params_table
  11. pe_rsc_params
  12. unpack_requires
  13. pe__unpack_resource
  14. common_update_score
  15. is_parent
  16. uber_parent
  17. common_free
  18. pe__find_active_on
  19. pe__find_active_requires
  20. pe__count_common
  21. pe__set_next_role

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

/* [previous][next][first][last][top][bottom][index][help] */