nssearch.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*******************************************************************************
  2. *
  3. * Module Name: nssearch - Namespace search
  4. *
  5. ******************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2006, R. Byron Moore
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <acpi/acpi.h>
  43. #include <acpi/acnamesp.h>
  44. #define _COMPONENT ACPI_NAMESPACE
  45. ACPI_MODULE_NAME("nssearch")
  46. /* Local prototypes */
  47. static acpi_status
  48. acpi_ns_search_parent_tree(u32 target_name,
  49. struct acpi_namespace_node *node,
  50. acpi_object_type type,
  51. struct acpi_namespace_node **return_node);
  52. /*******************************************************************************
  53. *
  54. * FUNCTION: acpi_ns_search_node
  55. *
  56. * PARAMETERS: target_name - Ascii ACPI name to search for
  57. * Node - Starting node where search will begin
  58. * Type - Object type to match
  59. * return_node - Where the matched Named obj is returned
  60. *
  61. * RETURN: Status
  62. *
  63. * DESCRIPTION: Search a single level of the namespace. Performs a
  64. * simple search of the specified level, and does not add
  65. * entries or search parents.
  66. *
  67. *
  68. * Named object lists are built (and subsequently dumped) in the
  69. * order in which the names are encountered during the namespace load;
  70. *
  71. * All namespace searching is linear in this implementation, but
  72. * could be easily modified to support any improved search
  73. * algorithm. However, the linear search was chosen for simplicity
  74. * and because the trees are small and the other interpreter
  75. * execution overhead is relatively high.
  76. *
  77. ******************************************************************************/
  78. acpi_status
  79. acpi_ns_search_node(u32 target_name,
  80. struct acpi_namespace_node *node,
  81. acpi_object_type type,
  82. struct acpi_namespace_node **return_node)
  83. {
  84. struct acpi_namespace_node *next_node;
  85. ACPI_FUNCTION_TRACE("ns_search_node");
  86. #ifdef ACPI_DEBUG_OUTPUT
  87. if (ACPI_LV_NAMES & acpi_dbg_level) {
  88. char *scope_name;
  89. scope_name = acpi_ns_get_external_pathname(node);
  90. if (scope_name) {
  91. ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
  92. "Searching %s (%p) For [%4.4s] (%s)\n",
  93. scope_name, node, ACPI_CAST_PTR(char,
  94. &target_name),
  95. acpi_ut_get_type_name(type)));
  96. ACPI_MEM_FREE(scope_name);
  97. }
  98. }
  99. #endif
  100. /*
  101. * Search for name at this namespace level, which is to say that we
  102. * must search for the name among the children of this object
  103. */
  104. next_node = node->child;
  105. while (next_node) {
  106. /* Check for match against the name */
  107. if (next_node->name.integer == target_name) {
  108. /* Resolve a control method alias if any */
  109. if (acpi_ns_get_type(next_node) ==
  110. ACPI_TYPE_LOCAL_METHOD_ALIAS) {
  111. next_node =
  112. ACPI_CAST_PTR(struct acpi_namespace_node,
  113. next_node->object);
  114. }
  115. /*
  116. * Found matching entry.
  117. */
  118. ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
  119. "Name [%4.4s] (%s) %p found in scope [%4.4s] %p\n",
  120. ACPI_CAST_PTR(char, &target_name),
  121. acpi_ut_get_type_name(next_node->
  122. type),
  123. next_node,
  124. acpi_ut_get_node_name(node), node));
  125. *return_node = next_node;
  126. return_ACPI_STATUS(AE_OK);
  127. }
  128. /*
  129. * The last entry in the list points back to the parent,
  130. * so a flag is used to indicate the end-of-list
  131. */
  132. if (next_node->flags & ANOBJ_END_OF_PEER_LIST) {
  133. /* Searched entire list, we are done */
  134. break;
  135. }
  136. /* Didn't match name, move on to the next peer object */
  137. next_node = next_node->peer;
  138. }
  139. /* Searched entire namespace level, not found */
  140. ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
  141. "Name [%4.4s] (%s) not found in search in scope [%4.4s] %p first child %p\n",
  142. ACPI_CAST_PTR(char, &target_name),
  143. acpi_ut_get_type_name(type),
  144. acpi_ut_get_node_name(node), node, node->child));
  145. return_ACPI_STATUS(AE_NOT_FOUND);
  146. }
  147. /*******************************************************************************
  148. *
  149. * FUNCTION: acpi_ns_search_parent_tree
  150. *
  151. * PARAMETERS: target_name - Ascii ACPI name to search for
  152. * Node - Starting node where search will begin
  153. * Type - Object type to match
  154. * return_node - Where the matched Node is returned
  155. *
  156. * RETURN: Status
  157. *
  158. * DESCRIPTION: Called when a name has not been found in the current namespace
  159. * level. Before adding it or giving up, ACPI scope rules require
  160. * searching enclosing scopes in cases identified by acpi_ns_local().
  161. *
  162. * "A name is located by finding the matching name in the current
  163. * name space, and then in the parent name space. If the parent
  164. * name space does not contain the name, the search continues
  165. * recursively until either the name is found or the name space
  166. * does not have a parent (the root of the name space). This
  167. * indicates that the name is not found" (From ACPI Specification,
  168. * section 5.3)
  169. *
  170. ******************************************************************************/
  171. static acpi_status
  172. acpi_ns_search_parent_tree(u32 target_name,
  173. struct acpi_namespace_node *node,
  174. acpi_object_type type,
  175. struct acpi_namespace_node **return_node)
  176. {
  177. acpi_status status;
  178. struct acpi_namespace_node *parent_node;
  179. ACPI_FUNCTION_TRACE("ns_search_parent_tree");
  180. parent_node = acpi_ns_get_parent_node(node);
  181. /*
  182. * If there is no parent (i.e., we are at the root) or type is "local",
  183. * we won't be searching the parent tree.
  184. */
  185. if (!parent_node) {
  186. ACPI_DEBUG_PRINT((ACPI_DB_NAMES, "[%4.4s] has no parent\n",
  187. ACPI_CAST_PTR(char, &target_name)));
  188. return_ACPI_STATUS(AE_NOT_FOUND);
  189. }
  190. if (acpi_ns_local(type)) {
  191. ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
  192. "[%4.4s] type [%s] must be local to this scope (no parent search)\n",
  193. ACPI_CAST_PTR(char, &target_name),
  194. acpi_ut_get_type_name(type)));
  195. return_ACPI_STATUS(AE_NOT_FOUND);
  196. }
  197. /* Search the parent tree */
  198. ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
  199. "Searching parent [%4.4s] for [%4.4s]\n",
  200. acpi_ut_get_node_name(parent_node),
  201. ACPI_CAST_PTR(char, &target_name)));
  202. /*
  203. * Search parents until target is found or we have backed up to the root
  204. */
  205. while (parent_node) {
  206. /*
  207. * Search parent scope. Use TYPE_ANY because we don't care about the
  208. * object type at this point, we only care about the existence of
  209. * the actual name we are searching for. Typechecking comes later.
  210. */
  211. status = acpi_ns_search_node(target_name, parent_node,
  212. ACPI_TYPE_ANY, return_node);
  213. if (ACPI_SUCCESS(status)) {
  214. return_ACPI_STATUS(status);
  215. }
  216. /*
  217. * Not found here, go up another level
  218. * (until we reach the root)
  219. */
  220. parent_node = acpi_ns_get_parent_node(parent_node);
  221. }
  222. /* Not found in parent tree */
  223. return_ACPI_STATUS(AE_NOT_FOUND);
  224. }
  225. /*******************************************************************************
  226. *
  227. * FUNCTION: acpi_ns_search_and_enter
  228. *
  229. * PARAMETERS: target_name - Ascii ACPI name to search for (4 chars)
  230. * walk_state - Current state of the walk
  231. * Node - Starting node where search will begin
  232. * interpreter_mode - Add names only in ACPI_MODE_LOAD_PASS_x.
  233. * Otherwise,search only.
  234. * Type - Object type to match
  235. * Flags - Flags describing the search restrictions
  236. * return_node - Where the Node is returned
  237. *
  238. * RETURN: Status
  239. *
  240. * DESCRIPTION: Search for a name segment in a single namespace level,
  241. * optionally adding it if it is not found. If the passed
  242. * Type is not Any and the type previously stored in the
  243. * entry was Any (i.e. unknown), update the stored type.
  244. *
  245. * In ACPI_IMODE_EXECUTE, search only.
  246. * In other modes, search and add if not found.
  247. *
  248. ******************************************************************************/
  249. acpi_status
  250. acpi_ns_search_and_enter(u32 target_name,
  251. struct acpi_walk_state *walk_state,
  252. struct acpi_namespace_node *node,
  253. acpi_interpreter_mode interpreter_mode,
  254. acpi_object_type type,
  255. u32 flags, struct acpi_namespace_node **return_node)
  256. {
  257. acpi_status status;
  258. struct acpi_namespace_node *new_node;
  259. ACPI_FUNCTION_TRACE("ns_search_and_enter");
  260. /* Parameter validation */
  261. if (!node || !target_name || !return_node) {
  262. ACPI_ERROR((AE_INFO,
  263. "Null param: Node %p Name %X return_node %p",
  264. node, target_name, return_node));
  265. return_ACPI_STATUS(AE_BAD_PARAMETER);
  266. }
  267. /* Name must consist of printable characters */
  268. if (!acpi_ut_valid_acpi_name(target_name)) {
  269. ACPI_ERROR((AE_INFO, "Bad character in ACPI Name: %X",
  270. target_name));
  271. return_ACPI_STATUS(AE_BAD_CHARACTER);
  272. }
  273. /* Try to find the name in the namespace level specified by the caller */
  274. *return_node = ACPI_ENTRY_NOT_FOUND;
  275. status = acpi_ns_search_node(target_name, node, type, return_node);
  276. if (status != AE_NOT_FOUND) {
  277. /*
  278. * If we found it AND the request specifies that a find is an error,
  279. * return the error
  280. */
  281. if ((status == AE_OK) && (flags & ACPI_NS_ERROR_IF_FOUND)) {
  282. status = AE_ALREADY_EXISTS;
  283. }
  284. /*
  285. * Either found it or there was an error
  286. * -- finished either way
  287. */
  288. return_ACPI_STATUS(status);
  289. }
  290. /*
  291. * The name was not found. If we are NOT performing the first pass
  292. * (name entry) of loading the namespace, search the parent tree (all the
  293. * way to the root if necessary.) We don't want to perform the parent
  294. * search when the namespace is actually being loaded. We want to perform
  295. * the search when namespace references are being resolved (load pass 2)
  296. * and during the execution phase.
  297. */
  298. if ((interpreter_mode != ACPI_IMODE_LOAD_PASS1) &&
  299. (flags & ACPI_NS_SEARCH_PARENT)) {
  300. /*
  301. * Not found at this level - search parent tree according to the
  302. * ACPI specification
  303. */
  304. status =
  305. acpi_ns_search_parent_tree(target_name, node, type,
  306. return_node);
  307. if (ACPI_SUCCESS(status)) {
  308. return_ACPI_STATUS(status);
  309. }
  310. }
  311. /*
  312. * In execute mode, just search, never add names. Exit now.
  313. */
  314. if (interpreter_mode == ACPI_IMODE_EXECUTE) {
  315. ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
  316. "%4.4s Not found in %p [Not adding]\n",
  317. ACPI_CAST_PTR(char, &target_name), node));
  318. return_ACPI_STATUS(AE_NOT_FOUND);
  319. }
  320. /* Create the new named object */
  321. new_node = acpi_ns_create_node(target_name);
  322. if (!new_node) {
  323. return_ACPI_STATUS(AE_NO_MEMORY);
  324. }
  325. /* Install the new object into the parent's list of children */
  326. acpi_ns_install_node(walk_state, node, new_node, type);
  327. *return_node = new_node;
  328. return_ACPI_STATUS(AE_OK);
  329. }