1 /*******************************************************************************
 
   3  * Module Name: nsalloc - Namespace allocation and deletion utilities
 
   5  ******************************************************************************/
 
   8  * Copyright (C) 2000 - 2005, R. Byron Moore
 
  11  * Redistribution and use in source and binary forms, with or without
 
  12  * modification, are permitted provided that the following conditions
 
  14  * 1. Redistributions of source code must retain the above copyright
 
  15  *    notice, this list of conditions, and the following disclaimer,
 
  16  *    without modification.
 
  17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
 
  18  *    substantially similar to the "NO WARRANTY" disclaimer below
 
  19  *    ("Disclaimer") and any redistribution must be conditioned upon
 
  20  *    including a substantially similar Disclaimer requirement for further
 
  21  *    binary redistribution.
 
  22  * 3. Neither the names of the above-listed copyright holders nor the names
 
  23  *    of any contributors may be used to endorse or promote products derived
 
  24  *    from this software without specific prior written permission.
 
  26  * Alternatively, this software may be distributed under the terms of the
 
  27  * GNU General Public License ("GPL") version 2 as published by the Free
 
  28  * Software Foundation.
 
  31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
  32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
  33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
 
  34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
  35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 
  36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 
  37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 
  38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 
  39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 
  40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 
  41  * POSSIBILITY OF SUCH DAMAGES.
 
  44 #include <acpi/acpi.h>
 
  45 #include <acpi/acnamesp.h>
 
  47 #define _COMPONENT          ACPI_NAMESPACE
 
  48 ACPI_MODULE_NAME("nsalloc")
 
  50 /* Local prototypes */
 
  51 static void acpi_ns_remove_reference(struct acpi_namespace_node *node);
 
  53 /*******************************************************************************
 
  55  * FUNCTION:    acpi_ns_create_node
 
  57  * PARAMETERS:  Name            - Name of the new node (4 char ACPI name)
 
  59  * RETURN:      New namespace node (Null on failure)
 
  61  * DESCRIPTION: Create a namespace node
 
  63  ******************************************************************************/
 
  65 struct acpi_namespace_node *acpi_ns_create_node(u32 name)
 
  67         struct acpi_namespace_node *node;
 
  69         ACPI_FUNCTION_TRACE("ns_create_node");
 
  71         node = ACPI_MEM_CALLOCATE(sizeof(struct acpi_namespace_node));
 
  76         ACPI_MEM_TRACKING(acpi_gbl_ns_node_list->total_allocated++);
 
  78         node->name.integer = name;
 
  79         node->reference_count = 1;
 
  80         ACPI_SET_DESCRIPTOR_TYPE(node, ACPI_DESC_TYPE_NAMED);
 
  85 /*******************************************************************************
 
  87  * FUNCTION:    acpi_ns_delete_node
 
  89  * PARAMETERS:  Node            - Node to be deleted
 
  93  * DESCRIPTION: Delete a namespace node
 
  95  ******************************************************************************/
 
  97 void acpi_ns_delete_node(struct acpi_namespace_node *node)
 
  99         struct acpi_namespace_node *parent_node;
 
 100         struct acpi_namespace_node *prev_node;
 
 101         struct acpi_namespace_node *next_node;
 
 103         ACPI_FUNCTION_TRACE_PTR("ns_delete_node", node);
 
 105         parent_node = acpi_ns_get_parent_node(node);
 
 108         next_node = parent_node->child;
 
 110         /* Find the node that is the previous peer in the parent's child list */
 
 112         while (next_node != node) {
 
 113                 prev_node = next_node;
 
 114                 next_node = prev_node->peer;
 
 118                 /* Node is not first child, unlink it */
 
 120                 prev_node->peer = next_node->peer;
 
 121                 if (next_node->flags & ANOBJ_END_OF_PEER_LIST) {
 
 122                         prev_node->flags |= ANOBJ_END_OF_PEER_LIST;
 
 125                 /* Node is first child (has no previous peer) */
 
 127                 if (next_node->flags & ANOBJ_END_OF_PEER_LIST) {
 
 128                         /* No peers at all */
 
 130                         parent_node->child = NULL;
 
 131                 } else {        /* Link peer list to parent */
 
 133                         parent_node->child = next_node->peer;
 
 137         ACPI_MEM_TRACKING(acpi_gbl_ns_node_list->total_freed++);
 
 140          * Detach an object if there is one then delete the node
 
 142         acpi_ns_detach_object(node);
 
 147 /*******************************************************************************
 
 149  * FUNCTION:    acpi_ns_install_node
 
 151  * PARAMETERS:  walk_state      - Current state of the walk
 
 152  *              parent_node     - The parent of the new Node
 
 153  *              Node            - The new Node to install
 
 154  *              Type            - ACPI object type of the new Node
 
 158  * DESCRIPTION: Initialize a new namespace node and install it amongst
 
 161  *              Note: Current namespace lookup is linear search. This appears
 
 162  *              to be sufficient as namespace searches consume only a small
 
 163  *              fraction of the execution time of the ACPI subsystem.
 
 165  ******************************************************************************/
 
 167 void acpi_ns_install_node(struct acpi_walk_state *walk_state, struct acpi_namespace_node *parent_node,  /* Parent */
 
 168                           struct acpi_namespace_node *node,     /* New Child */
 
 169                           acpi_object_type type)
 
 171         acpi_owner_id owner_id = 0;
 
 172         struct acpi_namespace_node *child_node;
 
 174         ACPI_FUNCTION_TRACE("ns_install_node");
 
 177          * Get the owner ID from the Walk state
 
 178          * The owner ID is used to track table deletion and
 
 179          * deletion of objects created by methods
 
 182                 owner_id = walk_state->owner_id;
 
 185         /* Link the new entry into the parent and existing children */
 
 187         child_node = parent_node->child;
 
 189                 parent_node->child = node;
 
 190                 node->flags |= ANOBJ_END_OF_PEER_LIST;
 
 191                 node->peer = parent_node;
 
 193                 while (!(child_node->flags & ANOBJ_END_OF_PEER_LIST)) {
 
 194                         child_node = child_node->peer;
 
 197                 child_node->peer = node;
 
 199                 /* Clear end-of-list flag */
 
 201                 child_node->flags &= ~ANOBJ_END_OF_PEER_LIST;
 
 202                 node->flags |= ANOBJ_END_OF_PEER_LIST;
 
 203                 node->peer = parent_node;
 
 206         /* Init the new entry */
 
 208         node->owner_id = owner_id;
 
 209         node->type = (u8) type;
 
 211         ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
 
 212                           "%4.4s (%s) [Node %p Owner %X] added to %4.4s (%s) [Node %p]\n",
 
 213                           acpi_ut_get_node_name(node),
 
 214                           acpi_ut_get_type_name(node->type), node, owner_id,
 
 215                           acpi_ut_get_node_name(parent_node),
 
 216                           acpi_ut_get_type_name(parent_node->type),
 
 220          * Increment the reference count(s) of all parents up to
 
 223         while ((node = acpi_ns_get_parent_node(node)) != NULL) {
 
 224                 node->reference_count++;
 
 230 /*******************************************************************************
 
 232  * FUNCTION:    acpi_ns_delete_children
 
 234  * PARAMETERS:  parent_node     - Delete this objects children
 
 238  * DESCRIPTION: Delete all children of the parent object. In other words,
 
 241  ******************************************************************************/
 
 243 void acpi_ns_delete_children(struct acpi_namespace_node *parent_node)
 
 245         struct acpi_namespace_node *child_node;
 
 246         struct acpi_namespace_node *next_node;
 
 247         struct acpi_namespace_node *node;
 
 250         ACPI_FUNCTION_TRACE_PTR("ns_delete_children", parent_node);
 
 256         /* If no children, all done! */
 
 258         child_node = parent_node->child;
 
 264          * Deallocate all children at this level
 
 267                 /* Get the things we need */
 
 269                 next_node = child_node->peer;
 
 270                 flags = child_node->flags;
 
 272                 /* Grandchildren should have all been deleted already */
 
 274                 if (child_node->child) {
 
 275                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
 
 276                                           "Found a grandchild! P=%p C=%p\n",
 
 277                                           parent_node, child_node));
 
 280                 /* Now we can free this child object */
 
 282                 ACPI_MEM_TRACKING(acpi_gbl_ns_node_list->total_freed++);
 
 284                 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
 
 285                                   "Object %p, Remaining %X\n", child_node,
 
 286                                   acpi_gbl_current_node_count));
 
 289                  * Detach an object if there is one, then free the child node
 
 291                 acpi_ns_detach_object(child_node);
 
 294                  * Decrement the reference count(s) of all parents up to
 
 295                  * the root! (counts were incremented when the node was created)
 
 298                 while ((node = acpi_ns_get_parent_node(node)) != NULL) {
 
 299                         node->reference_count--;
 
 302                 /* There should be only one reference remaining on this node */
 
 304                 if (child_node->reference_count != 1) {
 
 305                         ACPI_REPORT_WARNING(("Existing references (%d) on node being deleted (%p)\n", child_node->reference_count, child_node));
 
 308                 /* Now we can delete the node */
 
 310                 ACPI_MEM_FREE(child_node);
 
 312                 /* And move on to the next child in the list */
 
 314                 child_node = next_node;
 
 316         } while (!(flags & ANOBJ_END_OF_PEER_LIST));
 
 318         /* Clear the parent's child pointer */
 
 320         parent_node->child = NULL;
 
 325 /*******************************************************************************
 
 327  * FUNCTION:    acpi_ns_delete_namespace_subtree
 
 329  * PARAMETERS:  parent_node     - Root of the subtree to be deleted
 
 333  * DESCRIPTION: Delete a subtree of the namespace.  This includes all objects
 
 334  *              stored within the subtree.
 
 336  ******************************************************************************/
 
 338 void acpi_ns_delete_namespace_subtree(struct acpi_namespace_node *parent_node)
 
 340         struct acpi_namespace_node *child_node = NULL;
 
 343         ACPI_FUNCTION_TRACE("ns_delete_namespace_subtree");
 
 350          * Traverse the tree of objects until we bubble back up
 
 351          * to where we started.
 
 354                 /* Get the next node in this scope (NULL if none) */
 
 356                 child_node = acpi_ns_get_next_node(ACPI_TYPE_ANY, parent_node,
 
 359                         /* Found a child node - detach any attached object */
 
 361                         acpi_ns_detach_object(child_node);
 
 363                         /* Check if this node has any children */
 
 365                         if (acpi_ns_get_next_node
 
 366                             (ACPI_TYPE_ANY, child_node, NULL)) {
 
 368                                  * There is at least one child of this node,
 
 372                                 parent_node = child_node;
 
 377                          * No more children of this parent node.
 
 378                          * Move up to the grandparent.
 
 383                          * Now delete all of the children of this parent
 
 384                          * all at the same time.
 
 386                         acpi_ns_delete_children(parent_node);
 
 388                         /* New "last child" is this parent node */
 
 390                         child_node = parent_node;
 
 392                         /* Move up the tree to the grandparent */
 
 394                         parent_node = acpi_ns_get_parent_node(parent_node);
 
 401 /*******************************************************************************
 
 403  * FUNCTION:    acpi_ns_remove_reference
 
 405  * PARAMETERS:  Node           - Named node whose reference count is to be
 
 410  * DESCRIPTION: Remove a Node reference.  Decrements the reference count
 
 411  *              of all parent Nodes up to the root.  Any node along
 
 412  *              the way that reaches zero references is freed.
 
 414  ******************************************************************************/
 
 416 static void acpi_ns_remove_reference(struct acpi_namespace_node *node)
 
 418         struct acpi_namespace_node *parent_node;
 
 419         struct acpi_namespace_node *this_node;
 
 421         ACPI_FUNCTION_ENTRY();
 
 424          * Decrement the reference count(s) of this node and all
 
 425          * nodes up to the root,  Delete anything with zero remaining references.
 
 429                 /* Prepare to move up to parent */
 
 431                 parent_node = acpi_ns_get_parent_node(this_node);
 
 433                 /* Decrement the reference count on this node */
 
 435                 this_node->reference_count--;
 
 437                 /* Delete the node if no more references */
 
 439                 if (!this_node->reference_count) {
 
 440                         /* Delete all children and delete the node */
 
 442                         acpi_ns_delete_children(this_node);
 
 443                         acpi_ns_delete_node(this_node);
 
 446                 this_node = parent_node;
 
 450 /*******************************************************************************
 
 452  * FUNCTION:    acpi_ns_delete_namespace_by_owner
 
 454  * PARAMETERS:  owner_id    - All nodes with this owner will be deleted
 
 458  * DESCRIPTION: Delete entries within the namespace that are owned by a
 
 459  *              specific ID.  Used to delete entire ACPI tables.  All
 
 460  *              reference counts are updated.
 
 462  ******************************************************************************/
 
 464 void acpi_ns_delete_namespace_by_owner(acpi_owner_id owner_id)
 
 466         struct acpi_namespace_node *child_node;
 
 467         struct acpi_namespace_node *deletion_node;
 
 469         struct acpi_namespace_node *parent_node;
 
 471         ACPI_FUNCTION_TRACE_U32("ns_delete_namespace_by_owner", owner_id);
 
 477         parent_node = acpi_gbl_root_node;
 
 479         deletion_node = NULL;
 
 483          * Traverse the tree of nodes until we bubble back up
 
 484          * to where we started.
 
 488                  * Get the next child of this parent node. When child_node is NULL,
 
 489                  * the first child of the parent is returned
 
 492                     acpi_ns_get_next_node(ACPI_TYPE_ANY, parent_node,
 
 496                         acpi_ns_remove_reference(deletion_node);
 
 497                         deletion_node = NULL;
 
 501                         if (child_node->owner_id == owner_id) {
 
 502                                 /* Found a matching child node - detach any attached object */
 
 504                                 acpi_ns_detach_object(child_node);
 
 507                         /* Check if this node has any children */
 
 509                         if (acpi_ns_get_next_node
 
 510                             (ACPI_TYPE_ANY, child_node, NULL)) {
 
 512                                  * There is at least one child of this node,
 
 516                                 parent_node = child_node;
 
 518                         } else if (child_node->owner_id == owner_id) {
 
 519                                 deletion_node = child_node;
 
 523                          * No more children of this parent node.
 
 524                          * Move up to the grandparent.
 
 528                                 if (parent_node->owner_id == owner_id) {
 
 529                                         deletion_node = parent_node;
 
 533                         /* New "last child" is this parent node */
 
 535                         child_node = parent_node;
 
 537                         /* Move up the tree to the grandparent */
 
 539                         parent_node = acpi_ns_get_parent_node(parent_node);