assoc_array.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736
  1. /* Generic associative array implementation.
  2. *
  3. * See Documentation/assoc_array.txt for information.
  4. *
  5. * Copyright (C) 2013 Red Hat, Inc. All Rights Reserved.
  6. * Written by David Howells (dhowells@redhat.com)
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public Licence
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the Licence, or (at your option) any later version.
  12. */
  13. //#define DEBUG
  14. #include <linux/rcupdate.h>
  15. #include <linux/slab.h>
  16. #include <linux/err.h>
  17. #include <linux/assoc_array_priv.h>
  18. /*
  19. * Iterate over an associative array. The caller must hold the RCU read lock
  20. * or better.
  21. */
  22. static int assoc_array_subtree_iterate(const struct assoc_array_ptr *root,
  23. const struct assoc_array_ptr *stop,
  24. int (*iterator)(const void *leaf,
  25. void *iterator_data),
  26. void *iterator_data)
  27. {
  28. const struct assoc_array_shortcut *shortcut;
  29. const struct assoc_array_node *node;
  30. const struct assoc_array_ptr *cursor, *ptr, *parent;
  31. unsigned long has_meta;
  32. int slot, ret;
  33. cursor = root;
  34. begin_node:
  35. if (assoc_array_ptr_is_shortcut(cursor)) {
  36. /* Descend through a shortcut */
  37. shortcut = assoc_array_ptr_to_shortcut(cursor);
  38. smp_read_barrier_depends();
  39. cursor = ACCESS_ONCE(shortcut->next_node);
  40. }
  41. node = assoc_array_ptr_to_node(cursor);
  42. smp_read_barrier_depends();
  43. slot = 0;
  44. /* We perform two passes of each node.
  45. *
  46. * The first pass does all the leaves in this node. This means we
  47. * don't miss any leaves if the node is split up by insertion whilst
  48. * we're iterating over the branches rooted here (we may, however, see
  49. * some leaves twice).
  50. */
  51. has_meta = 0;
  52. for (; slot < ASSOC_ARRAY_FAN_OUT; slot++) {
  53. ptr = ACCESS_ONCE(node->slots[slot]);
  54. has_meta |= (unsigned long)ptr;
  55. if (ptr && assoc_array_ptr_is_leaf(ptr)) {
  56. /* We need a barrier between the read of the pointer
  57. * and dereferencing the pointer - but only if we are
  58. * actually going to dereference it.
  59. */
  60. smp_read_barrier_depends();
  61. /* Invoke the callback */
  62. ret = iterator(assoc_array_ptr_to_leaf(ptr),
  63. iterator_data);
  64. if (ret)
  65. return ret;
  66. }
  67. }
  68. /* The second pass attends to all the metadata pointers. If we follow
  69. * one of these we may find that we don't come back here, but rather go
  70. * back to a replacement node with the leaves in a different layout.
  71. *
  72. * We are guaranteed to make progress, however, as the slot number for
  73. * a particular portion of the key space cannot change - and we
  74. * continue at the back pointer + 1.
  75. */
  76. if (!(has_meta & ASSOC_ARRAY_PTR_META_TYPE))
  77. goto finished_node;
  78. slot = 0;
  79. continue_node:
  80. node = assoc_array_ptr_to_node(cursor);
  81. smp_read_barrier_depends();
  82. for (; slot < ASSOC_ARRAY_FAN_OUT; slot++) {
  83. ptr = ACCESS_ONCE(node->slots[slot]);
  84. if (assoc_array_ptr_is_meta(ptr)) {
  85. cursor = ptr;
  86. goto begin_node;
  87. }
  88. }
  89. finished_node:
  90. /* Move up to the parent (may need to skip back over a shortcut) */
  91. parent = ACCESS_ONCE(node->back_pointer);
  92. slot = node->parent_slot;
  93. if (parent == stop)
  94. return 0;
  95. if (assoc_array_ptr_is_shortcut(parent)) {
  96. shortcut = assoc_array_ptr_to_shortcut(parent);
  97. smp_read_barrier_depends();
  98. cursor = parent;
  99. parent = ACCESS_ONCE(shortcut->back_pointer);
  100. slot = shortcut->parent_slot;
  101. if (parent == stop)
  102. return 0;
  103. }
  104. /* Ascend to next slot in parent node */
  105. cursor = parent;
  106. slot++;
  107. goto continue_node;
  108. }
  109. /**
  110. * assoc_array_iterate - Pass all objects in the array to a callback
  111. * @array: The array to iterate over.
  112. * @iterator: The callback function.
  113. * @iterator_data: Private data for the callback function.
  114. *
  115. * Iterate over all the objects in an associative array. Each one will be
  116. * presented to the iterator function.
  117. *
  118. * If the array is being modified concurrently with the iteration then it is
  119. * possible that some objects in the array will be passed to the iterator
  120. * callback more than once - though every object should be passed at least
  121. * once. If this is undesirable then the caller must lock against modification
  122. * for the duration of this function.
  123. *
  124. * The function will return 0 if no objects were in the array or else it will
  125. * return the result of the last iterator function called. Iteration stops
  126. * immediately if any call to the iteration function results in a non-zero
  127. * return.
  128. *
  129. * The caller should hold the RCU read lock or better if concurrent
  130. * modification is possible.
  131. */
  132. int assoc_array_iterate(const struct assoc_array *array,
  133. int (*iterator)(const void *object,
  134. void *iterator_data),
  135. void *iterator_data)
  136. {
  137. struct assoc_array_ptr *root = ACCESS_ONCE(array->root);
  138. if (!root)
  139. return 0;
  140. return assoc_array_subtree_iterate(root, NULL, iterator, iterator_data);
  141. }
  142. enum assoc_array_walk_status {
  143. assoc_array_walk_tree_empty,
  144. assoc_array_walk_found_terminal_node,
  145. assoc_array_walk_found_wrong_shortcut,
  146. };
  147. struct assoc_array_walk_result {
  148. struct {
  149. struct assoc_array_node *node; /* Node in which leaf might be found */
  150. int level;
  151. int slot;
  152. } terminal_node;
  153. struct {
  154. struct assoc_array_shortcut *shortcut;
  155. int level;
  156. int sc_level;
  157. unsigned long sc_segments;
  158. unsigned long dissimilarity;
  159. } wrong_shortcut;
  160. };
  161. /*
  162. * Navigate through the internal tree looking for the closest node to the key.
  163. */
  164. static enum assoc_array_walk_status
  165. assoc_array_walk(const struct assoc_array *array,
  166. const struct assoc_array_ops *ops,
  167. const void *index_key,
  168. struct assoc_array_walk_result *result)
  169. {
  170. struct assoc_array_shortcut *shortcut;
  171. struct assoc_array_node *node;
  172. struct assoc_array_ptr *cursor, *ptr;
  173. unsigned long sc_segments, dissimilarity;
  174. unsigned long segments;
  175. int level, sc_level, next_sc_level;
  176. int slot;
  177. pr_devel("-->%s()\n", __func__);
  178. cursor = ACCESS_ONCE(array->root);
  179. if (!cursor)
  180. return assoc_array_walk_tree_empty;
  181. level = 0;
  182. /* Use segments from the key for the new leaf to navigate through the
  183. * internal tree, skipping through nodes and shortcuts that are on
  184. * route to the destination. Eventually we'll come to a slot that is
  185. * either empty or contains a leaf at which point we've found a node in
  186. * which the leaf we're looking for might be found or into which it
  187. * should be inserted.
  188. */
  189. jumped:
  190. segments = ops->get_key_chunk(index_key, level);
  191. pr_devel("segments[%d]: %lx\n", level, segments);
  192. if (assoc_array_ptr_is_shortcut(cursor))
  193. goto follow_shortcut;
  194. consider_node:
  195. node = assoc_array_ptr_to_node(cursor);
  196. smp_read_barrier_depends();
  197. slot = segments >> (level & ASSOC_ARRAY_KEY_CHUNK_MASK);
  198. slot &= ASSOC_ARRAY_FAN_MASK;
  199. ptr = ACCESS_ONCE(node->slots[slot]);
  200. pr_devel("consider slot %x [ix=%d type=%lu]\n",
  201. slot, level, (unsigned long)ptr & 3);
  202. if (!assoc_array_ptr_is_meta(ptr)) {
  203. /* The node doesn't have a node/shortcut pointer in the slot
  204. * corresponding to the index key that we have to follow.
  205. */
  206. result->terminal_node.node = node;
  207. result->terminal_node.level = level;
  208. result->terminal_node.slot = slot;
  209. pr_devel("<--%s() = terminal_node\n", __func__);
  210. return assoc_array_walk_found_terminal_node;
  211. }
  212. if (assoc_array_ptr_is_node(ptr)) {
  213. /* There is a pointer to a node in the slot corresponding to
  214. * this index key segment, so we need to follow it.
  215. */
  216. cursor = ptr;
  217. level += ASSOC_ARRAY_LEVEL_STEP;
  218. if ((level & ASSOC_ARRAY_KEY_CHUNK_MASK) != 0)
  219. goto consider_node;
  220. goto jumped;
  221. }
  222. /* There is a shortcut in the slot corresponding to the index key
  223. * segment. We follow the shortcut if its partial index key matches
  224. * this leaf's. Otherwise we need to split the shortcut.
  225. */
  226. cursor = ptr;
  227. follow_shortcut:
  228. shortcut = assoc_array_ptr_to_shortcut(cursor);
  229. smp_read_barrier_depends();
  230. pr_devel("shortcut to %d\n", shortcut->skip_to_level);
  231. sc_level = level + ASSOC_ARRAY_LEVEL_STEP;
  232. BUG_ON(sc_level > shortcut->skip_to_level);
  233. do {
  234. /* Check the leaf against the shortcut's index key a word at a
  235. * time, trimming the final word (the shortcut stores the index
  236. * key completely from the root to the shortcut's target).
  237. */
  238. if ((sc_level & ASSOC_ARRAY_KEY_CHUNK_MASK) == 0)
  239. segments = ops->get_key_chunk(index_key, sc_level);
  240. sc_segments = shortcut->index_key[sc_level >> ASSOC_ARRAY_KEY_CHUNK_SHIFT];
  241. dissimilarity = segments ^ sc_segments;
  242. if (round_up(sc_level, ASSOC_ARRAY_KEY_CHUNK_SIZE) > shortcut->skip_to_level) {
  243. /* Trim segments that are beyond the shortcut */
  244. int shift = shortcut->skip_to_level & ASSOC_ARRAY_KEY_CHUNK_MASK;
  245. dissimilarity &= ~(ULONG_MAX << shift);
  246. next_sc_level = shortcut->skip_to_level;
  247. } else {
  248. next_sc_level = sc_level + ASSOC_ARRAY_KEY_CHUNK_SIZE;
  249. next_sc_level = round_down(next_sc_level, ASSOC_ARRAY_KEY_CHUNK_SIZE);
  250. }
  251. if (dissimilarity != 0) {
  252. /* This shortcut points elsewhere */
  253. result->wrong_shortcut.shortcut = shortcut;
  254. result->wrong_shortcut.level = level;
  255. result->wrong_shortcut.sc_level = sc_level;
  256. result->wrong_shortcut.sc_segments = sc_segments;
  257. result->wrong_shortcut.dissimilarity = dissimilarity;
  258. return assoc_array_walk_found_wrong_shortcut;
  259. }
  260. sc_level = next_sc_level;
  261. } while (sc_level < shortcut->skip_to_level);
  262. /* The shortcut matches the leaf's index to this point. */
  263. cursor = ACCESS_ONCE(shortcut->next_node);
  264. if (((level ^ sc_level) & ~ASSOC_ARRAY_KEY_CHUNK_MASK) != 0) {
  265. level = sc_level;
  266. goto jumped;
  267. } else {
  268. level = sc_level;
  269. goto consider_node;
  270. }
  271. }
  272. /**
  273. * assoc_array_find - Find an object by index key
  274. * @array: The associative array to search.
  275. * @ops: The operations to use.
  276. * @index_key: The key to the object.
  277. *
  278. * Find an object in an associative array by walking through the internal tree
  279. * to the node that should contain the object and then searching the leaves
  280. * there. NULL is returned if the requested object was not found in the array.
  281. *
  282. * The caller must hold the RCU read lock or better.
  283. */
  284. void *assoc_array_find(const struct assoc_array *array,
  285. const struct assoc_array_ops *ops,
  286. const void *index_key)
  287. {
  288. struct assoc_array_walk_result result;
  289. const struct assoc_array_node *node;
  290. const struct assoc_array_ptr *ptr;
  291. const void *leaf;
  292. int slot;
  293. if (assoc_array_walk(array, ops, index_key, &result) !=
  294. assoc_array_walk_found_terminal_node)
  295. return NULL;
  296. node = result.terminal_node.node;
  297. smp_read_barrier_depends();
  298. /* If the target key is available to us, it's has to be pointed to by
  299. * the terminal node.
  300. */
  301. for (slot = 0; slot < ASSOC_ARRAY_FAN_OUT; slot++) {
  302. ptr = ACCESS_ONCE(node->slots[slot]);
  303. if (ptr && assoc_array_ptr_is_leaf(ptr)) {
  304. /* We need a barrier between the read of the pointer
  305. * and dereferencing the pointer - but only if we are
  306. * actually going to dereference it.
  307. */
  308. leaf = assoc_array_ptr_to_leaf(ptr);
  309. smp_read_barrier_depends();
  310. if (ops->compare_object(leaf, index_key))
  311. return (void *)leaf;
  312. }
  313. }
  314. return NULL;
  315. }
  316. /*
  317. * Destructively iterate over an associative array. The caller must prevent
  318. * other simultaneous accesses.
  319. */
  320. static void assoc_array_destroy_subtree(struct assoc_array_ptr *root,
  321. const struct assoc_array_ops *ops)
  322. {
  323. struct assoc_array_shortcut *shortcut;
  324. struct assoc_array_node *node;
  325. struct assoc_array_ptr *cursor, *parent = NULL;
  326. int slot = -1;
  327. pr_devel("-->%s()\n", __func__);
  328. cursor = root;
  329. if (!cursor) {
  330. pr_devel("empty\n");
  331. return;
  332. }
  333. move_to_meta:
  334. if (assoc_array_ptr_is_shortcut(cursor)) {
  335. /* Descend through a shortcut */
  336. pr_devel("[%d] shortcut\n", slot);
  337. BUG_ON(!assoc_array_ptr_is_shortcut(cursor));
  338. shortcut = assoc_array_ptr_to_shortcut(cursor);
  339. BUG_ON(shortcut->back_pointer != parent);
  340. BUG_ON(slot != -1 && shortcut->parent_slot != slot);
  341. parent = cursor;
  342. cursor = shortcut->next_node;
  343. slot = -1;
  344. BUG_ON(!assoc_array_ptr_is_node(cursor));
  345. }
  346. pr_devel("[%d] node\n", slot);
  347. node = assoc_array_ptr_to_node(cursor);
  348. BUG_ON(node->back_pointer != parent);
  349. BUG_ON(slot != -1 && node->parent_slot != slot);
  350. slot = 0;
  351. continue_node:
  352. pr_devel("Node %p [back=%p]\n", node, node->back_pointer);
  353. for (; slot < ASSOC_ARRAY_FAN_OUT; slot++) {
  354. struct assoc_array_ptr *ptr = node->slots[slot];
  355. if (!ptr)
  356. continue;
  357. if (assoc_array_ptr_is_meta(ptr)) {
  358. parent = cursor;
  359. cursor = ptr;
  360. goto move_to_meta;
  361. }
  362. if (ops) {
  363. pr_devel("[%d] free leaf\n", slot);
  364. ops->free_object(assoc_array_ptr_to_leaf(ptr));
  365. }
  366. }
  367. parent = node->back_pointer;
  368. slot = node->parent_slot;
  369. pr_devel("free node\n");
  370. kfree(node);
  371. if (!parent)
  372. return; /* Done */
  373. /* Move back up to the parent (may need to free a shortcut on
  374. * the way up) */
  375. if (assoc_array_ptr_is_shortcut(parent)) {
  376. shortcut = assoc_array_ptr_to_shortcut(parent);
  377. BUG_ON(shortcut->next_node != cursor);
  378. cursor = parent;
  379. parent = shortcut->back_pointer;
  380. slot = shortcut->parent_slot;
  381. pr_devel("free shortcut\n");
  382. kfree(shortcut);
  383. if (!parent)
  384. return;
  385. BUG_ON(!assoc_array_ptr_is_node(parent));
  386. }
  387. /* Ascend to next slot in parent node */
  388. pr_devel("ascend to %p[%d]\n", parent, slot);
  389. cursor = parent;
  390. node = assoc_array_ptr_to_node(cursor);
  391. slot++;
  392. goto continue_node;
  393. }
  394. /**
  395. * assoc_array_destroy - Destroy an associative array
  396. * @array: The array to destroy.
  397. * @ops: The operations to use.
  398. *
  399. * Discard all metadata and free all objects in an associative array. The
  400. * array will be empty and ready to use again upon completion. This function
  401. * cannot fail.
  402. *
  403. * The caller must prevent all other accesses whilst this takes place as no
  404. * attempt is made to adjust pointers gracefully to permit RCU readlock-holding
  405. * accesses to continue. On the other hand, no memory allocation is required.
  406. */
  407. void assoc_array_destroy(struct assoc_array *array,
  408. const struct assoc_array_ops *ops)
  409. {
  410. assoc_array_destroy_subtree(array->root, ops);
  411. array->root = NULL;
  412. }
  413. /*
  414. * Handle insertion into an empty tree.
  415. */
  416. static bool assoc_array_insert_in_empty_tree(struct assoc_array_edit *edit)
  417. {
  418. struct assoc_array_node *new_n0;
  419. pr_devel("-->%s()\n", __func__);
  420. new_n0 = kzalloc(sizeof(struct assoc_array_node), GFP_KERNEL);
  421. if (!new_n0)
  422. return false;
  423. edit->new_meta[0] = assoc_array_node_to_ptr(new_n0);
  424. edit->leaf_p = &new_n0->slots[0];
  425. edit->adjust_count_on = new_n0;
  426. edit->set[0].ptr = &edit->array->root;
  427. edit->set[0].to = assoc_array_node_to_ptr(new_n0);
  428. pr_devel("<--%s() = ok [no root]\n", __func__);
  429. return true;
  430. }
  431. /*
  432. * Handle insertion into a terminal node.
  433. */
  434. static bool assoc_array_insert_into_terminal_node(struct assoc_array_edit *edit,
  435. const struct assoc_array_ops *ops,
  436. const void *index_key,
  437. struct assoc_array_walk_result *result)
  438. {
  439. struct assoc_array_shortcut *shortcut, *new_s0;
  440. struct assoc_array_node *node, *new_n0, *new_n1, *side;
  441. struct assoc_array_ptr *ptr;
  442. unsigned long dissimilarity, base_seg, blank;
  443. size_t keylen;
  444. bool have_meta;
  445. int level, diff;
  446. int slot, next_slot, free_slot, i, j;
  447. node = result->terminal_node.node;
  448. level = result->terminal_node.level;
  449. edit->segment_cache[ASSOC_ARRAY_FAN_OUT] = result->terminal_node.slot;
  450. pr_devel("-->%s()\n", __func__);
  451. /* We arrived at a node which doesn't have an onward node or shortcut
  452. * pointer that we have to follow. This means that (a) the leaf we
  453. * want must go here (either by insertion or replacement) or (b) we
  454. * need to split this node and insert in one of the fragments.
  455. */
  456. free_slot = -1;
  457. /* Firstly, we have to check the leaves in this node to see if there's
  458. * a matching one we should replace in place.
  459. */
  460. for (i = 0; i < ASSOC_ARRAY_FAN_OUT; i++) {
  461. ptr = node->slots[i];
  462. if (!ptr) {
  463. free_slot = i;
  464. continue;
  465. }
  466. if (assoc_array_ptr_is_leaf(ptr) &&
  467. ops->compare_object(assoc_array_ptr_to_leaf(ptr),
  468. index_key)) {
  469. pr_devel("replace in slot %d\n", i);
  470. edit->leaf_p = &node->slots[i];
  471. edit->dead_leaf = node->slots[i];
  472. pr_devel("<--%s() = ok [replace]\n", __func__);
  473. return true;
  474. }
  475. }
  476. /* If there is a free slot in this node then we can just insert the
  477. * leaf here.
  478. */
  479. if (free_slot >= 0) {
  480. pr_devel("insert in free slot %d\n", free_slot);
  481. edit->leaf_p = &node->slots[free_slot];
  482. edit->adjust_count_on = node;
  483. pr_devel("<--%s() = ok [insert]\n", __func__);
  484. return true;
  485. }
  486. /* The node has no spare slots - so we're either going to have to split
  487. * it or insert another node before it.
  488. *
  489. * Whatever, we're going to need at least two new nodes - so allocate
  490. * those now. We may also need a new shortcut, but we deal with that
  491. * when we need it.
  492. */
  493. new_n0 = kzalloc(sizeof(struct assoc_array_node), GFP_KERNEL);
  494. if (!new_n0)
  495. return false;
  496. edit->new_meta[0] = assoc_array_node_to_ptr(new_n0);
  497. new_n1 = kzalloc(sizeof(struct assoc_array_node), GFP_KERNEL);
  498. if (!new_n1)
  499. return false;
  500. edit->new_meta[1] = assoc_array_node_to_ptr(new_n1);
  501. /* We need to find out how similar the leaves are. */
  502. pr_devel("no spare slots\n");
  503. have_meta = false;
  504. for (i = 0; i < ASSOC_ARRAY_FAN_OUT; i++) {
  505. ptr = node->slots[i];
  506. if (assoc_array_ptr_is_meta(ptr)) {
  507. edit->segment_cache[i] = 0xff;
  508. have_meta = true;
  509. continue;
  510. }
  511. base_seg = ops->get_object_key_chunk(
  512. assoc_array_ptr_to_leaf(ptr), level);
  513. base_seg >>= level & ASSOC_ARRAY_KEY_CHUNK_MASK;
  514. edit->segment_cache[i] = base_seg & ASSOC_ARRAY_FAN_MASK;
  515. }
  516. if (have_meta) {
  517. pr_devel("have meta\n");
  518. goto split_node;
  519. }
  520. /* The node contains only leaves */
  521. dissimilarity = 0;
  522. base_seg = edit->segment_cache[0];
  523. for (i = 1; i < ASSOC_ARRAY_FAN_OUT; i++)
  524. dissimilarity |= edit->segment_cache[i] ^ base_seg;
  525. pr_devel("only leaves; dissimilarity=%lx\n", dissimilarity);
  526. if ((dissimilarity & ASSOC_ARRAY_FAN_MASK) == 0) {
  527. /* The old leaves all cluster in the same slot. We will need
  528. * to insert a shortcut if the new node wants to cluster with them.
  529. */
  530. if ((edit->segment_cache[ASSOC_ARRAY_FAN_OUT] ^ base_seg) == 0)
  531. goto all_leaves_cluster_together;
  532. /* Otherwise all the old leaves cluster in the same slot, but
  533. * the new leaf wants to go into a different slot - so we
  534. * create a new node (n0) to hold the new leaf and a pointer to
  535. * a new node (n1) holding all the old leaves.
  536. *
  537. * This can be done by falling through to the node splitting
  538. * path.
  539. */
  540. pr_devel("present leaves cluster but not new leaf\n");
  541. }
  542. split_node:
  543. pr_devel("split node\n");
  544. /* We need to split the current node. The node must contain anything
  545. * from a single leaf (in the one leaf case, this leaf will cluster
  546. * with the new leaf) and the rest meta-pointers, to all leaves, some
  547. * of which may cluster.
  548. *
  549. * It won't contain the case in which all the current leaves plus the
  550. * new leaves want to cluster in the same slot.
  551. *
  552. * We need to expel at least two leaves out of a set consisting of the
  553. * leaves in the node and the new leaf. The current meta pointers can
  554. * just be copied as they shouldn't cluster with any of the leaves.
  555. *
  556. * We need a new node (n0) to replace the current one and a new node to
  557. * take the expelled nodes (n1).
  558. */
  559. edit->set[0].to = assoc_array_node_to_ptr(new_n0);
  560. new_n0->back_pointer = node->back_pointer;
  561. new_n0->parent_slot = node->parent_slot;
  562. new_n1->back_pointer = assoc_array_node_to_ptr(new_n0);
  563. new_n1->parent_slot = -1; /* Need to calculate this */
  564. do_split_node:
  565. pr_devel("do_split_node\n");
  566. new_n0->nr_leaves_on_branch = node->nr_leaves_on_branch;
  567. new_n1->nr_leaves_on_branch = 0;
  568. /* Begin by finding two matching leaves. There have to be at least two
  569. * that match - even if there are meta pointers - because any leaf that
  570. * would match a slot with a meta pointer in it must be somewhere
  571. * behind that meta pointer and cannot be here. Further, given N
  572. * remaining leaf slots, we now have N+1 leaves to go in them.
  573. */
  574. for (i = 0; i < ASSOC_ARRAY_FAN_OUT; i++) {
  575. slot = edit->segment_cache[i];
  576. if (slot != 0xff)
  577. for (j = i + 1; j < ASSOC_ARRAY_FAN_OUT + 1; j++)
  578. if (edit->segment_cache[j] == slot)
  579. goto found_slot_for_multiple_occupancy;
  580. }
  581. found_slot_for_multiple_occupancy:
  582. pr_devel("same slot: %x %x [%02x]\n", i, j, slot);
  583. BUG_ON(i >= ASSOC_ARRAY_FAN_OUT);
  584. BUG_ON(j >= ASSOC_ARRAY_FAN_OUT + 1);
  585. BUG_ON(slot >= ASSOC_ARRAY_FAN_OUT);
  586. new_n1->parent_slot = slot;
  587. /* Metadata pointers cannot change slot */
  588. for (i = 0; i < ASSOC_ARRAY_FAN_OUT; i++)
  589. if (assoc_array_ptr_is_meta(node->slots[i]))
  590. new_n0->slots[i] = node->slots[i];
  591. else
  592. new_n0->slots[i] = NULL;
  593. BUG_ON(new_n0->slots[slot] != NULL);
  594. new_n0->slots[slot] = assoc_array_node_to_ptr(new_n1);
  595. /* Filter the leaf pointers between the new nodes */
  596. free_slot = -1;
  597. next_slot = 0;
  598. for (i = 0; i < ASSOC_ARRAY_FAN_OUT; i++) {
  599. if (assoc_array_ptr_is_meta(node->slots[i]))
  600. continue;
  601. if (edit->segment_cache[i] == slot) {
  602. new_n1->slots[next_slot++] = node->slots[i];
  603. new_n1->nr_leaves_on_branch++;
  604. } else {
  605. do {
  606. free_slot++;
  607. } while (new_n0->slots[free_slot] != NULL);
  608. new_n0->slots[free_slot] = node->slots[i];
  609. }
  610. }
  611. pr_devel("filtered: f=%x n=%x\n", free_slot, next_slot);
  612. if (edit->segment_cache[ASSOC_ARRAY_FAN_OUT] != slot) {
  613. do {
  614. free_slot++;
  615. } while (new_n0->slots[free_slot] != NULL);
  616. edit->leaf_p = &new_n0->slots[free_slot];
  617. edit->adjust_count_on = new_n0;
  618. } else {
  619. edit->leaf_p = &new_n1->slots[next_slot++];
  620. edit->adjust_count_on = new_n1;
  621. }
  622. BUG_ON(next_slot <= 1);
  623. edit->set_backpointers_to = assoc_array_node_to_ptr(new_n0);
  624. for (i = 0; i < ASSOC_ARRAY_FAN_OUT; i++) {
  625. if (edit->segment_cache[i] == 0xff) {
  626. ptr = node->slots[i];
  627. BUG_ON(assoc_array_ptr_is_leaf(ptr));
  628. if (assoc_array_ptr_is_node(ptr)) {
  629. side = assoc_array_ptr_to_node(ptr);
  630. edit->set_backpointers[i] = &side->back_pointer;
  631. } else {
  632. shortcut = assoc_array_ptr_to_shortcut(ptr);
  633. edit->set_backpointers[i] = &shortcut->back_pointer;
  634. }
  635. }
  636. }
  637. ptr = node->back_pointer;
  638. if (!ptr)
  639. edit->set[0].ptr = &edit->array->root;
  640. else if (assoc_array_ptr_is_node(ptr))
  641. edit->set[0].ptr = &assoc_array_ptr_to_node(ptr)->slots[node->parent_slot];
  642. else
  643. edit->set[0].ptr = &assoc_array_ptr_to_shortcut(ptr)->next_node;
  644. edit->excised_meta[0] = assoc_array_node_to_ptr(node);
  645. pr_devel("<--%s() = ok [split node]\n", __func__);
  646. return true;
  647. all_leaves_cluster_together:
  648. /* All the leaves, new and old, want to cluster together in this node
  649. * in the same slot, so we have to replace this node with a shortcut to
  650. * skip over the identical parts of the key and then place a pair of
  651. * nodes, one inside the other, at the end of the shortcut and
  652. * distribute the keys between them.
  653. *
  654. * Firstly we need to work out where the leaves start diverging as a
  655. * bit position into their keys so that we know how big the shortcut
  656. * needs to be.
  657. *
  658. * We only need to make a single pass of N of the N+1 leaves because if
  659. * any keys differ between themselves at bit X then at least one of
  660. * them must also differ with the base key at bit X or before.
  661. */
  662. pr_devel("all leaves cluster together\n");
  663. diff = INT_MAX;
  664. for (i = 0; i < ASSOC_ARRAY_FAN_OUT; i++) {
  665. int x = ops->diff_objects(assoc_array_ptr_to_leaf(node->slots[i]),
  666. index_key);
  667. if (x < diff) {
  668. BUG_ON(x < 0);
  669. diff = x;
  670. }
  671. }
  672. BUG_ON(diff == INT_MAX);
  673. BUG_ON(diff < level + ASSOC_ARRAY_LEVEL_STEP);
  674. keylen = round_up(diff, ASSOC_ARRAY_KEY_CHUNK_SIZE);
  675. keylen >>= ASSOC_ARRAY_KEY_CHUNK_SHIFT;
  676. new_s0 = kzalloc(sizeof(struct assoc_array_shortcut) +
  677. keylen * sizeof(unsigned long), GFP_KERNEL);
  678. if (!new_s0)
  679. return false;
  680. edit->new_meta[2] = assoc_array_shortcut_to_ptr(new_s0);
  681. edit->set[0].to = assoc_array_shortcut_to_ptr(new_s0);
  682. new_s0->back_pointer = node->back_pointer;
  683. new_s0->parent_slot = node->parent_slot;
  684. new_s0->next_node = assoc_array_node_to_ptr(new_n0);
  685. new_n0->back_pointer = assoc_array_shortcut_to_ptr(new_s0);
  686. new_n0->parent_slot = 0;
  687. new_n1->back_pointer = assoc_array_node_to_ptr(new_n0);
  688. new_n1->parent_slot = -1; /* Need to calculate this */
  689. new_s0->skip_to_level = level = diff & ~ASSOC_ARRAY_LEVEL_STEP_MASK;
  690. pr_devel("skip_to_level = %d [diff %d]\n", level, diff);
  691. BUG_ON(level <= 0);
  692. for (i = 0; i < keylen; i++)
  693. new_s0->index_key[i] =
  694. ops->get_key_chunk(index_key, i * ASSOC_ARRAY_KEY_CHUNK_SIZE);
  695. if (level & ASSOC_ARRAY_KEY_CHUNK_MASK) {
  696. blank = ULONG_MAX << (level & ASSOC_ARRAY_KEY_CHUNK_MASK);
  697. pr_devel("blank off [%zu] %d: %lx\n", keylen - 1, level, blank);
  698. new_s0->index_key[keylen - 1] &= ~blank;
  699. }
  700. /* This now reduces to a node splitting exercise for which we'll need
  701. * to regenerate the disparity table.
  702. */
  703. for (i = 0; i < ASSOC_ARRAY_FAN_OUT; i++) {
  704. ptr = node->slots[i];
  705. base_seg = ops->get_object_key_chunk(assoc_array_ptr_to_leaf(ptr),
  706. level);
  707. base_seg >>= level & ASSOC_ARRAY_KEY_CHUNK_MASK;
  708. edit->segment_cache[i] = base_seg & ASSOC_ARRAY_FAN_MASK;
  709. }
  710. base_seg = ops->get_key_chunk(index_key, level);
  711. base_seg >>= level & ASSOC_ARRAY_KEY_CHUNK_MASK;
  712. edit->segment_cache[ASSOC_ARRAY_FAN_OUT] = base_seg & ASSOC_ARRAY_FAN_MASK;
  713. goto do_split_node;
  714. }
  715. /*
  716. * Handle insertion into the middle of a shortcut.
  717. */
  718. static bool assoc_array_insert_mid_shortcut(struct assoc_array_edit *edit,
  719. const struct assoc_array_ops *ops,
  720. struct assoc_array_walk_result *result)
  721. {
  722. struct assoc_array_shortcut *shortcut, *new_s0, *new_s1;
  723. struct assoc_array_node *node, *new_n0, *side;
  724. unsigned long sc_segments, dissimilarity, blank;
  725. size_t keylen;
  726. int level, sc_level, diff;
  727. int sc_slot;
  728. shortcut = result->wrong_shortcut.shortcut;
  729. level = result->wrong_shortcut.level;
  730. sc_level = result->wrong_shortcut.sc_level;
  731. sc_segments = result->wrong_shortcut.sc_segments;
  732. dissimilarity = result->wrong_shortcut.dissimilarity;
  733. pr_devel("-->%s(ix=%d dis=%lx scix=%d)\n",
  734. __func__, level, dissimilarity, sc_level);
  735. /* We need to split a shortcut and insert a node between the two
  736. * pieces. Zero-length pieces will be dispensed with entirely.
  737. *
  738. * First of all, we need to find out in which level the first
  739. * difference was.
  740. */
  741. diff = __ffs(dissimilarity);
  742. diff &= ~ASSOC_ARRAY_LEVEL_STEP_MASK;
  743. diff += sc_level & ~ASSOC_ARRAY_KEY_CHUNK_MASK;
  744. pr_devel("diff=%d\n", diff);
  745. if (!shortcut->back_pointer) {
  746. edit->set[0].ptr = &edit->array->root;
  747. } else if (assoc_array_ptr_is_node(shortcut->back_pointer)) {
  748. node = assoc_array_ptr_to_node(shortcut->back_pointer);
  749. edit->set[0].ptr = &node->slots[shortcut->parent_slot];
  750. } else {
  751. BUG();
  752. }
  753. edit->excised_meta[0] = assoc_array_shortcut_to_ptr(shortcut);
  754. /* Create a new node now since we're going to need it anyway */
  755. new_n0 = kzalloc(sizeof(struct assoc_array_node), GFP_KERNEL);
  756. if (!new_n0)
  757. return false;
  758. edit->new_meta[0] = assoc_array_node_to_ptr(new_n0);
  759. edit->adjust_count_on = new_n0;
  760. /* Insert a new shortcut before the new node if this segment isn't of
  761. * zero length - otherwise we just connect the new node directly to the
  762. * parent.
  763. */
  764. level += ASSOC_ARRAY_LEVEL_STEP;
  765. if (diff > level) {
  766. pr_devel("pre-shortcut %d...%d\n", level, diff);
  767. keylen = round_up(diff, ASSOC_ARRAY_KEY_CHUNK_SIZE);
  768. keylen >>= ASSOC_ARRAY_KEY_CHUNK_SHIFT;
  769. new_s0 = kzalloc(sizeof(struct assoc_array_shortcut) +
  770. keylen * sizeof(unsigned long), GFP_KERNEL);
  771. if (!new_s0)
  772. return false;
  773. edit->new_meta[1] = assoc_array_shortcut_to_ptr(new_s0);
  774. edit->set[0].to = assoc_array_shortcut_to_ptr(new_s0);
  775. new_s0->back_pointer = shortcut->back_pointer;
  776. new_s0->parent_slot = shortcut->parent_slot;
  777. new_s0->next_node = assoc_array_node_to_ptr(new_n0);
  778. new_s0->skip_to_level = diff;
  779. new_n0->back_pointer = assoc_array_shortcut_to_ptr(new_s0);
  780. new_n0->parent_slot = 0;
  781. memcpy(new_s0->index_key, shortcut->index_key,
  782. keylen * sizeof(unsigned long));
  783. blank = ULONG_MAX << (diff & ASSOC_ARRAY_KEY_CHUNK_MASK);
  784. pr_devel("blank off [%zu] %d: %lx\n", keylen - 1, diff, blank);
  785. new_s0->index_key[keylen - 1] &= ~blank;
  786. } else {
  787. pr_devel("no pre-shortcut\n");
  788. edit->set[0].to = assoc_array_node_to_ptr(new_n0);
  789. new_n0->back_pointer = shortcut->back_pointer;
  790. new_n0->parent_slot = shortcut->parent_slot;
  791. }
  792. side = assoc_array_ptr_to_node(shortcut->next_node);
  793. new_n0->nr_leaves_on_branch = side->nr_leaves_on_branch;
  794. /* We need to know which slot in the new node is going to take a
  795. * metadata pointer.
  796. */
  797. sc_slot = sc_segments >> (diff & ASSOC_ARRAY_KEY_CHUNK_MASK);
  798. sc_slot &= ASSOC_ARRAY_FAN_MASK;
  799. pr_devel("new slot %lx >> %d -> %d\n",
  800. sc_segments, diff & ASSOC_ARRAY_KEY_CHUNK_MASK, sc_slot);
  801. /* Determine whether we need to follow the new node with a replacement
  802. * for the current shortcut. We could in theory reuse the current
  803. * shortcut if its parent slot number doesn't change - but that's a
  804. * 1-in-16 chance so not worth expending the code upon.
  805. */
  806. level = diff + ASSOC_ARRAY_LEVEL_STEP;
  807. if (level < shortcut->skip_to_level) {
  808. pr_devel("post-shortcut %d...%d\n", level, shortcut->skip_to_level);
  809. keylen = round_up(shortcut->skip_to_level, ASSOC_ARRAY_KEY_CHUNK_SIZE);
  810. keylen >>= ASSOC_ARRAY_KEY_CHUNK_SHIFT;
  811. new_s1 = kzalloc(sizeof(struct assoc_array_shortcut) +
  812. keylen * sizeof(unsigned long), GFP_KERNEL);
  813. if (!new_s1)
  814. return false;
  815. edit->new_meta[2] = assoc_array_shortcut_to_ptr(new_s1);
  816. new_s1->back_pointer = assoc_array_node_to_ptr(new_n0);
  817. new_s1->parent_slot = sc_slot;
  818. new_s1->next_node = shortcut->next_node;
  819. new_s1->skip_to_level = shortcut->skip_to_level;
  820. new_n0->slots[sc_slot] = assoc_array_shortcut_to_ptr(new_s1);
  821. memcpy(new_s1->index_key, shortcut->index_key,
  822. keylen * sizeof(unsigned long));
  823. edit->set[1].ptr = &side->back_pointer;
  824. edit->set[1].to = assoc_array_shortcut_to_ptr(new_s1);
  825. } else {
  826. pr_devel("no post-shortcut\n");
  827. /* We don't have to replace the pointed-to node as long as we
  828. * use memory barriers to make sure the parent slot number is
  829. * changed before the back pointer (the parent slot number is
  830. * irrelevant to the old parent shortcut).
  831. */
  832. new_n0->slots[sc_slot] = shortcut->next_node;
  833. edit->set_parent_slot[0].p = &side->parent_slot;
  834. edit->set_parent_slot[0].to = sc_slot;
  835. edit->set[1].ptr = &side->back_pointer;
  836. edit->set[1].to = assoc_array_node_to_ptr(new_n0);
  837. }
  838. /* Install the new leaf in a spare slot in the new node. */
  839. if (sc_slot == 0)
  840. edit->leaf_p = &new_n0->slots[1];
  841. else
  842. edit->leaf_p = &new_n0->slots[0];
  843. pr_devel("<--%s() = ok [split shortcut]\n", __func__);
  844. return edit;
  845. }
  846. /**
  847. * assoc_array_insert - Script insertion of an object into an associative array
  848. * @array: The array to insert into.
  849. * @ops: The operations to use.
  850. * @index_key: The key to insert at.
  851. * @object: The object to insert.
  852. *
  853. * Precalculate and preallocate a script for the insertion or replacement of an
  854. * object in an associative array. This results in an edit script that can
  855. * either be applied or cancelled.
  856. *
  857. * The function returns a pointer to an edit script or -ENOMEM.
  858. *
  859. * The caller should lock against other modifications and must continue to hold
  860. * the lock until assoc_array_apply_edit() has been called.
  861. *
  862. * Accesses to the tree may take place concurrently with this function,
  863. * provided they hold the RCU read lock.
  864. */
  865. struct assoc_array_edit *assoc_array_insert(struct assoc_array *array,
  866. const struct assoc_array_ops *ops,
  867. const void *index_key,
  868. void *object)
  869. {
  870. struct assoc_array_walk_result result;
  871. struct assoc_array_edit *edit;
  872. pr_devel("-->%s()\n", __func__);
  873. /* The leaf pointer we're given must not have the bottom bit set as we
  874. * use those for type-marking the pointer. NULL pointers are also not
  875. * allowed as they indicate an empty slot but we have to allow them
  876. * here as they can be updated later.
  877. */
  878. BUG_ON(assoc_array_ptr_is_meta(object));
  879. edit = kzalloc(sizeof(struct assoc_array_edit), GFP_KERNEL);
  880. if (!edit)
  881. return ERR_PTR(-ENOMEM);
  882. edit->array = array;
  883. edit->ops = ops;
  884. edit->leaf = assoc_array_leaf_to_ptr(object);
  885. edit->adjust_count_by = 1;
  886. switch (assoc_array_walk(array, ops, index_key, &result)) {
  887. case assoc_array_walk_tree_empty:
  888. /* Allocate a root node if there isn't one yet */
  889. if (!assoc_array_insert_in_empty_tree(edit))
  890. goto enomem;
  891. return edit;
  892. case assoc_array_walk_found_terminal_node:
  893. /* We found a node that doesn't have a node/shortcut pointer in
  894. * the slot corresponding to the index key that we have to
  895. * follow.
  896. */
  897. if (!assoc_array_insert_into_terminal_node(edit, ops, index_key,
  898. &result))
  899. goto enomem;
  900. return edit;
  901. case assoc_array_walk_found_wrong_shortcut:
  902. /* We found a shortcut that didn't match our key in a slot we
  903. * needed to follow.
  904. */
  905. if (!assoc_array_insert_mid_shortcut(edit, ops, &result))
  906. goto enomem;
  907. return edit;
  908. }
  909. enomem:
  910. /* Clean up after an out of memory error */
  911. pr_devel("enomem\n");
  912. assoc_array_cancel_edit(edit);
  913. return ERR_PTR(-ENOMEM);
  914. }
  915. /**
  916. * assoc_array_insert_set_object - Set the new object pointer in an edit script
  917. * @edit: The edit script to modify.
  918. * @object: The object pointer to set.
  919. *
  920. * Change the object to be inserted in an edit script. The object pointed to
  921. * by the old object is not freed. This must be done prior to applying the
  922. * script.
  923. */
  924. void assoc_array_insert_set_object(struct assoc_array_edit *edit, void *object)
  925. {
  926. BUG_ON(!object);
  927. edit->leaf = assoc_array_leaf_to_ptr(object);
  928. }
  929. struct assoc_array_delete_collapse_context {
  930. struct assoc_array_node *node;
  931. const void *skip_leaf;
  932. int slot;
  933. };
  934. /*
  935. * Subtree collapse to node iterator.
  936. */
  937. static int assoc_array_delete_collapse_iterator(const void *leaf,
  938. void *iterator_data)
  939. {
  940. struct assoc_array_delete_collapse_context *collapse = iterator_data;
  941. if (leaf == collapse->skip_leaf)
  942. return 0;
  943. BUG_ON(collapse->slot >= ASSOC_ARRAY_FAN_OUT);
  944. collapse->node->slots[collapse->slot++] = assoc_array_leaf_to_ptr(leaf);
  945. return 0;
  946. }
  947. /**
  948. * assoc_array_delete - Script deletion of an object from an associative array
  949. * @array: The array to search.
  950. * @ops: The operations to use.
  951. * @index_key: The key to the object.
  952. *
  953. * Precalculate and preallocate a script for the deletion of an object from an
  954. * associative array. This results in an edit script that can either be
  955. * applied or cancelled.
  956. *
  957. * The function returns a pointer to an edit script if the object was found,
  958. * NULL if the object was not found or -ENOMEM.
  959. *
  960. * The caller should lock against other modifications and must continue to hold
  961. * the lock until assoc_array_apply_edit() has been called.
  962. *
  963. * Accesses to the tree may take place concurrently with this function,
  964. * provided they hold the RCU read lock.
  965. */
  966. struct assoc_array_edit *assoc_array_delete(struct assoc_array *array,
  967. const struct assoc_array_ops *ops,
  968. const void *index_key)
  969. {
  970. struct assoc_array_delete_collapse_context collapse;
  971. struct assoc_array_walk_result result;
  972. struct assoc_array_node *node, *new_n0;
  973. struct assoc_array_edit *edit;
  974. struct assoc_array_ptr *ptr;
  975. bool has_meta;
  976. int slot, i;
  977. pr_devel("-->%s()\n", __func__);
  978. edit = kzalloc(sizeof(struct assoc_array_edit), GFP_KERNEL);
  979. if (!edit)
  980. return ERR_PTR(-ENOMEM);
  981. edit->array = array;
  982. edit->ops = ops;
  983. edit->adjust_count_by = -1;
  984. switch (assoc_array_walk(array, ops, index_key, &result)) {
  985. case assoc_array_walk_found_terminal_node:
  986. /* We found a node that should contain the leaf we've been
  987. * asked to remove - *if* it's in the tree.
  988. */
  989. pr_devel("terminal_node\n");
  990. node = result.terminal_node.node;
  991. for (slot = 0; slot < ASSOC_ARRAY_FAN_OUT; slot++) {
  992. ptr = node->slots[slot];
  993. if (ptr &&
  994. assoc_array_ptr_is_leaf(ptr) &&
  995. ops->compare_object(assoc_array_ptr_to_leaf(ptr),
  996. index_key))
  997. goto found_leaf;
  998. }
  999. case assoc_array_walk_tree_empty:
  1000. case assoc_array_walk_found_wrong_shortcut:
  1001. default:
  1002. assoc_array_cancel_edit(edit);
  1003. pr_devel("not found\n");
  1004. return NULL;
  1005. }
  1006. found_leaf:
  1007. BUG_ON(array->nr_leaves_on_tree <= 0);
  1008. /* In the simplest form of deletion we just clear the slot and release
  1009. * the leaf after a suitable interval.
  1010. */
  1011. edit->dead_leaf = node->slots[slot];
  1012. edit->set[0].ptr = &node->slots[slot];
  1013. edit->set[0].to = NULL;
  1014. edit->adjust_count_on = node;
  1015. /* If that concludes erasure of the last leaf, then delete the entire
  1016. * internal array.
  1017. */
  1018. if (array->nr_leaves_on_tree == 1) {
  1019. edit->set[1].ptr = &array->root;
  1020. edit->set[1].to = NULL;
  1021. edit->adjust_count_on = NULL;
  1022. edit->excised_subtree = array->root;
  1023. pr_devel("all gone\n");
  1024. return edit;
  1025. }
  1026. /* However, we'd also like to clear up some metadata blocks if we
  1027. * possibly can.
  1028. *
  1029. * We go for a simple algorithm of: if this node has FAN_OUT or fewer
  1030. * leaves in it, then attempt to collapse it - and attempt to
  1031. * recursively collapse up the tree.
  1032. *
  1033. * We could also try and collapse in partially filled subtrees to take
  1034. * up space in this node.
  1035. */
  1036. if (node->nr_leaves_on_branch <= ASSOC_ARRAY_FAN_OUT + 1) {
  1037. struct assoc_array_node *parent, *grandparent;
  1038. struct assoc_array_ptr *ptr;
  1039. /* First of all, we need to know if this node has metadata so
  1040. * that we don't try collapsing if all the leaves are already
  1041. * here.
  1042. */
  1043. has_meta = false;
  1044. for (i = 0; i < ASSOC_ARRAY_FAN_OUT; i++) {
  1045. ptr = node->slots[i];
  1046. if (assoc_array_ptr_is_meta(ptr)) {
  1047. has_meta = true;
  1048. break;
  1049. }
  1050. }
  1051. pr_devel("leaves: %ld [m=%d]\n",
  1052. node->nr_leaves_on_branch - 1, has_meta);
  1053. /* Look further up the tree to see if we can collapse this node
  1054. * into a more proximal node too.
  1055. */
  1056. parent = node;
  1057. collapse_up:
  1058. pr_devel("collapse subtree: %ld\n", parent->nr_leaves_on_branch);
  1059. ptr = parent->back_pointer;
  1060. if (!ptr)
  1061. goto do_collapse;
  1062. if (assoc_array_ptr_is_shortcut(ptr)) {
  1063. struct assoc_array_shortcut *s = assoc_array_ptr_to_shortcut(ptr);
  1064. ptr = s->back_pointer;
  1065. if (!ptr)
  1066. goto do_collapse;
  1067. }
  1068. grandparent = assoc_array_ptr_to_node(ptr);
  1069. if (grandparent->nr_leaves_on_branch <= ASSOC_ARRAY_FAN_OUT + 1) {
  1070. parent = grandparent;
  1071. goto collapse_up;
  1072. }
  1073. do_collapse:
  1074. /* There's no point collapsing if the original node has no meta
  1075. * pointers to discard and if we didn't merge into one of that
  1076. * node's ancestry.
  1077. */
  1078. if (has_meta || parent != node) {
  1079. node = parent;
  1080. /* Create a new node to collapse into */
  1081. new_n0 = kzalloc(sizeof(struct assoc_array_node), GFP_KERNEL);
  1082. if (!new_n0)
  1083. goto enomem;
  1084. edit->new_meta[0] = assoc_array_node_to_ptr(new_n0);
  1085. new_n0->back_pointer = node->back_pointer;
  1086. new_n0->parent_slot = node->parent_slot;
  1087. new_n0->nr_leaves_on_branch = node->nr_leaves_on_branch;
  1088. edit->adjust_count_on = new_n0;
  1089. collapse.node = new_n0;
  1090. collapse.skip_leaf = assoc_array_ptr_to_leaf(edit->dead_leaf);
  1091. collapse.slot = 0;
  1092. assoc_array_subtree_iterate(assoc_array_node_to_ptr(node),
  1093. node->back_pointer,
  1094. assoc_array_delete_collapse_iterator,
  1095. &collapse);
  1096. pr_devel("collapsed %d,%lu\n", collapse.slot, new_n0->nr_leaves_on_branch);
  1097. BUG_ON(collapse.slot != new_n0->nr_leaves_on_branch - 1);
  1098. if (!node->back_pointer) {
  1099. edit->set[1].ptr = &array->root;
  1100. } else if (assoc_array_ptr_is_leaf(node->back_pointer)) {
  1101. BUG();
  1102. } else if (assoc_array_ptr_is_node(node->back_pointer)) {
  1103. struct assoc_array_node *p =
  1104. assoc_array_ptr_to_node(node->back_pointer);
  1105. edit->set[1].ptr = &p->slots[node->parent_slot];
  1106. } else if (assoc_array_ptr_is_shortcut(node->back_pointer)) {
  1107. struct assoc_array_shortcut *s =
  1108. assoc_array_ptr_to_shortcut(node->back_pointer);
  1109. edit->set[1].ptr = &s->next_node;
  1110. }
  1111. edit->set[1].to = assoc_array_node_to_ptr(new_n0);
  1112. edit->excised_subtree = assoc_array_node_to_ptr(node);
  1113. }
  1114. }
  1115. return edit;
  1116. enomem:
  1117. /* Clean up after an out of memory error */
  1118. pr_devel("enomem\n");
  1119. assoc_array_cancel_edit(edit);
  1120. return ERR_PTR(-ENOMEM);
  1121. }
  1122. /**
  1123. * assoc_array_clear - Script deletion of all objects from an associative array
  1124. * @array: The array to clear.
  1125. * @ops: The operations to use.
  1126. *
  1127. * Precalculate and preallocate a script for the deletion of all the objects
  1128. * from an associative array. This results in an edit script that can either
  1129. * be applied or cancelled.
  1130. *
  1131. * The function returns a pointer to an edit script if there are objects to be
  1132. * deleted, NULL if there are no objects in the array or -ENOMEM.
  1133. *
  1134. * The caller should lock against other modifications and must continue to hold
  1135. * the lock until assoc_array_apply_edit() has been called.
  1136. *
  1137. * Accesses to the tree may take place concurrently with this function,
  1138. * provided they hold the RCU read lock.
  1139. */
  1140. struct assoc_array_edit *assoc_array_clear(struct assoc_array *array,
  1141. const struct assoc_array_ops *ops)
  1142. {
  1143. struct assoc_array_edit *edit;
  1144. pr_devel("-->%s()\n", __func__);
  1145. if (!array->root)
  1146. return NULL;
  1147. edit = kzalloc(sizeof(struct assoc_array_edit), GFP_KERNEL);
  1148. if (!edit)
  1149. return ERR_PTR(-ENOMEM);
  1150. edit->array = array;
  1151. edit->ops = ops;
  1152. edit->set[1].ptr = &array->root;
  1153. edit->set[1].to = NULL;
  1154. edit->excised_subtree = array->root;
  1155. edit->ops_for_excised_subtree = ops;
  1156. pr_devel("all gone\n");
  1157. return edit;
  1158. }
  1159. /*
  1160. * Handle the deferred destruction after an applied edit.
  1161. */
  1162. static void assoc_array_rcu_cleanup(struct rcu_head *head)
  1163. {
  1164. struct assoc_array_edit *edit =
  1165. container_of(head, struct assoc_array_edit, rcu);
  1166. int i;
  1167. pr_devel("-->%s()\n", __func__);
  1168. if (edit->dead_leaf)
  1169. edit->ops->free_object(assoc_array_ptr_to_leaf(edit->dead_leaf));
  1170. for (i = 0; i < ARRAY_SIZE(edit->excised_meta); i++)
  1171. if (edit->excised_meta[i])
  1172. kfree(assoc_array_ptr_to_node(edit->excised_meta[i]));
  1173. if (edit->excised_subtree) {
  1174. BUG_ON(assoc_array_ptr_is_leaf(edit->excised_subtree));
  1175. if (assoc_array_ptr_is_node(edit->excised_subtree)) {
  1176. struct assoc_array_node *n =
  1177. assoc_array_ptr_to_node(edit->excised_subtree);
  1178. n->back_pointer = NULL;
  1179. } else {
  1180. struct assoc_array_shortcut *s =
  1181. assoc_array_ptr_to_shortcut(edit->excised_subtree);
  1182. s->back_pointer = NULL;
  1183. }
  1184. assoc_array_destroy_subtree(edit->excised_subtree,
  1185. edit->ops_for_excised_subtree);
  1186. }
  1187. kfree(edit);
  1188. }
  1189. /**
  1190. * assoc_array_apply_edit - Apply an edit script to an associative array
  1191. * @edit: The script to apply.
  1192. *
  1193. * Apply an edit script to an associative array to effect an insertion,
  1194. * deletion or clearance. As the edit script includes preallocated memory,
  1195. * this is guaranteed not to fail.
  1196. *
  1197. * The edit script, dead objects and dead metadata will be scheduled for
  1198. * destruction after an RCU grace period to permit those doing read-only
  1199. * accesses on the array to continue to do so under the RCU read lock whilst
  1200. * the edit is taking place.
  1201. */
  1202. void assoc_array_apply_edit(struct assoc_array_edit *edit)
  1203. {
  1204. struct assoc_array_shortcut *shortcut;
  1205. struct assoc_array_node *node;
  1206. struct assoc_array_ptr *ptr;
  1207. int i;
  1208. pr_devel("-->%s()\n", __func__);
  1209. smp_wmb();
  1210. if (edit->leaf_p)
  1211. *edit->leaf_p = edit->leaf;
  1212. smp_wmb();
  1213. for (i = 0; i < ARRAY_SIZE(edit->set_parent_slot); i++)
  1214. if (edit->set_parent_slot[i].p)
  1215. *edit->set_parent_slot[i].p = edit->set_parent_slot[i].to;
  1216. smp_wmb();
  1217. for (i = 0; i < ARRAY_SIZE(edit->set_backpointers); i++)
  1218. if (edit->set_backpointers[i])
  1219. *edit->set_backpointers[i] = edit->set_backpointers_to;
  1220. smp_wmb();
  1221. for (i = 0; i < ARRAY_SIZE(edit->set); i++)
  1222. if (edit->set[i].ptr)
  1223. *edit->set[i].ptr = edit->set[i].to;
  1224. if (edit->array->root == NULL) {
  1225. edit->array->nr_leaves_on_tree = 0;
  1226. } else if (edit->adjust_count_on) {
  1227. node = edit->adjust_count_on;
  1228. for (;;) {
  1229. node->nr_leaves_on_branch += edit->adjust_count_by;
  1230. ptr = node->back_pointer;
  1231. if (!ptr)
  1232. break;
  1233. if (assoc_array_ptr_is_shortcut(ptr)) {
  1234. shortcut = assoc_array_ptr_to_shortcut(ptr);
  1235. ptr = shortcut->back_pointer;
  1236. if (!ptr)
  1237. break;
  1238. }
  1239. BUG_ON(!assoc_array_ptr_is_node(ptr));
  1240. node = assoc_array_ptr_to_node(ptr);
  1241. }
  1242. edit->array->nr_leaves_on_tree += edit->adjust_count_by;
  1243. }
  1244. call_rcu(&edit->rcu, assoc_array_rcu_cleanup);
  1245. }
  1246. /**
  1247. * assoc_array_cancel_edit - Discard an edit script.
  1248. * @edit: The script to discard.
  1249. *
  1250. * Free an edit script and all the preallocated data it holds without making
  1251. * any changes to the associative array it was intended for.
  1252. *
  1253. * NOTE! In the case of an insertion script, this does _not_ release the leaf
  1254. * that was to be inserted. That is left to the caller.
  1255. */
  1256. void assoc_array_cancel_edit(struct assoc_array_edit *edit)
  1257. {
  1258. struct assoc_array_ptr *ptr;
  1259. int i;
  1260. pr_devel("-->%s()\n", __func__);
  1261. /* Clean up after an out of memory error */
  1262. for (i = 0; i < ARRAY_SIZE(edit->new_meta); i++) {
  1263. ptr = edit->new_meta[i];
  1264. if (ptr) {
  1265. if (assoc_array_ptr_is_node(ptr))
  1266. kfree(assoc_array_ptr_to_node(ptr));
  1267. else
  1268. kfree(assoc_array_ptr_to_shortcut(ptr));
  1269. }
  1270. }
  1271. kfree(edit);
  1272. }
  1273. /**
  1274. * assoc_array_gc - Garbage collect an associative array.
  1275. * @array: The array to clean.
  1276. * @ops: The operations to use.
  1277. * @iterator: A callback function to pass judgement on each object.
  1278. * @iterator_data: Private data for the callback function.
  1279. *
  1280. * Collect garbage from an associative array and pack down the internal tree to
  1281. * save memory.
  1282. *
  1283. * The iterator function is asked to pass judgement upon each object in the
  1284. * array. If it returns false, the object is discard and if it returns true,
  1285. * the object is kept. If it returns true, it must increment the object's
  1286. * usage count (or whatever it needs to do to retain it) before returning.
  1287. *
  1288. * This function returns 0 if successful or -ENOMEM if out of memory. In the
  1289. * latter case, the array is not changed.
  1290. *
  1291. * The caller should lock against other modifications and must continue to hold
  1292. * the lock until assoc_array_apply_edit() has been called.
  1293. *
  1294. * Accesses to the tree may take place concurrently with this function,
  1295. * provided they hold the RCU read lock.
  1296. */
  1297. int assoc_array_gc(struct assoc_array *array,
  1298. const struct assoc_array_ops *ops,
  1299. bool (*iterator)(void *object, void *iterator_data),
  1300. void *iterator_data)
  1301. {
  1302. struct assoc_array_shortcut *shortcut, *new_s;
  1303. struct assoc_array_node *node, *new_n;
  1304. struct assoc_array_edit *edit;
  1305. struct assoc_array_ptr *cursor, *ptr;
  1306. struct assoc_array_ptr *new_root, *new_parent, **new_ptr_pp;
  1307. unsigned long nr_leaves_on_tree;
  1308. int keylen, slot, nr_free, next_slot, i;
  1309. pr_devel("-->%s()\n", __func__);
  1310. if (!array->root)
  1311. return 0;
  1312. edit = kzalloc(sizeof(struct assoc_array_edit), GFP_KERNEL);
  1313. if (!edit)
  1314. return -ENOMEM;
  1315. edit->array = array;
  1316. edit->ops = ops;
  1317. edit->ops_for_excised_subtree = ops;
  1318. edit->set[0].ptr = &array->root;
  1319. edit->excised_subtree = array->root;
  1320. new_root = new_parent = NULL;
  1321. new_ptr_pp = &new_root;
  1322. cursor = array->root;
  1323. descend:
  1324. /* If this point is a shortcut, then we need to duplicate it and
  1325. * advance the target cursor.
  1326. */
  1327. if (assoc_array_ptr_is_shortcut(cursor)) {
  1328. shortcut = assoc_array_ptr_to_shortcut(cursor);
  1329. keylen = round_up(shortcut->skip_to_level, ASSOC_ARRAY_KEY_CHUNK_SIZE);
  1330. keylen >>= ASSOC_ARRAY_KEY_CHUNK_SHIFT;
  1331. new_s = kmalloc(sizeof(struct assoc_array_shortcut) +
  1332. keylen * sizeof(unsigned long), GFP_KERNEL);
  1333. if (!new_s)
  1334. goto enomem;
  1335. pr_devel("dup shortcut %p -> %p\n", shortcut, new_s);
  1336. memcpy(new_s, shortcut, (sizeof(struct assoc_array_shortcut) +
  1337. keylen * sizeof(unsigned long)));
  1338. new_s->back_pointer = new_parent;
  1339. new_s->parent_slot = shortcut->parent_slot;
  1340. *new_ptr_pp = new_parent = assoc_array_shortcut_to_ptr(new_s);
  1341. new_ptr_pp = &new_s->next_node;
  1342. cursor = shortcut->next_node;
  1343. }
  1344. /* Duplicate the node at this position */
  1345. node = assoc_array_ptr_to_node(cursor);
  1346. new_n = kzalloc(sizeof(struct assoc_array_node), GFP_KERNEL);
  1347. if (!new_n)
  1348. goto enomem;
  1349. pr_devel("dup node %p -> %p\n", node, new_n);
  1350. new_n->back_pointer = new_parent;
  1351. new_n->parent_slot = node->parent_slot;
  1352. *new_ptr_pp = new_parent = assoc_array_node_to_ptr(new_n);
  1353. new_ptr_pp = NULL;
  1354. slot = 0;
  1355. continue_node:
  1356. /* Filter across any leaves and gc any subtrees */
  1357. for (; slot < ASSOC_ARRAY_FAN_OUT; slot++) {
  1358. ptr = node->slots[slot];
  1359. if (!ptr)
  1360. continue;
  1361. if (assoc_array_ptr_is_leaf(ptr)) {
  1362. if (iterator(assoc_array_ptr_to_leaf(ptr),
  1363. iterator_data))
  1364. /* The iterator will have done any reference
  1365. * counting on the object for us.
  1366. */
  1367. new_n->slots[slot] = ptr;
  1368. continue;
  1369. }
  1370. new_ptr_pp = &new_n->slots[slot];
  1371. cursor = ptr;
  1372. goto descend;
  1373. }
  1374. pr_devel("-- compress node %p --\n", new_n);
  1375. /* Count up the number of empty slots in this node and work out the
  1376. * subtree leaf count.
  1377. */
  1378. new_n->nr_leaves_on_branch = 0;
  1379. nr_free = 0;
  1380. for (slot = 0; slot < ASSOC_ARRAY_FAN_OUT; slot++) {
  1381. ptr = new_n->slots[slot];
  1382. if (!ptr)
  1383. nr_free++;
  1384. else if (assoc_array_ptr_is_leaf(ptr))
  1385. new_n->nr_leaves_on_branch++;
  1386. }
  1387. pr_devel("free=%d, leaves=%lu\n", nr_free, new_n->nr_leaves_on_branch);
  1388. /* See what we can fold in */
  1389. next_slot = 0;
  1390. for (slot = 0; slot < ASSOC_ARRAY_FAN_OUT; slot++) {
  1391. struct assoc_array_shortcut *s;
  1392. struct assoc_array_node *child;
  1393. ptr = new_n->slots[slot];
  1394. if (!ptr || assoc_array_ptr_is_leaf(ptr))
  1395. continue;
  1396. s = NULL;
  1397. if (assoc_array_ptr_is_shortcut(ptr)) {
  1398. s = assoc_array_ptr_to_shortcut(ptr);
  1399. ptr = s->next_node;
  1400. }
  1401. child = assoc_array_ptr_to_node(ptr);
  1402. new_n->nr_leaves_on_branch += child->nr_leaves_on_branch;
  1403. if (child->nr_leaves_on_branch <= nr_free + 1) {
  1404. /* Fold the child node into this one */
  1405. pr_devel("[%d] fold node %lu/%d [nx %d]\n",
  1406. slot, child->nr_leaves_on_branch, nr_free + 1,
  1407. next_slot);
  1408. /* We would already have reaped an intervening shortcut
  1409. * on the way back up the tree.
  1410. */
  1411. BUG_ON(s);
  1412. new_n->slots[slot] = NULL;
  1413. nr_free++;
  1414. if (slot < next_slot)
  1415. next_slot = slot;
  1416. for (i = 0; i < ASSOC_ARRAY_FAN_OUT; i++) {
  1417. struct assoc_array_ptr *p = child->slots[i];
  1418. if (!p)
  1419. continue;
  1420. BUG_ON(assoc_array_ptr_is_meta(p));
  1421. while (new_n->slots[next_slot])
  1422. next_slot++;
  1423. BUG_ON(next_slot >= ASSOC_ARRAY_FAN_OUT);
  1424. new_n->slots[next_slot++] = p;
  1425. nr_free--;
  1426. }
  1427. kfree(child);
  1428. } else {
  1429. pr_devel("[%d] retain node %lu/%d [nx %d]\n",
  1430. slot, child->nr_leaves_on_branch, nr_free + 1,
  1431. next_slot);
  1432. }
  1433. }
  1434. pr_devel("after: %lu\n", new_n->nr_leaves_on_branch);
  1435. nr_leaves_on_tree = new_n->nr_leaves_on_branch;
  1436. /* Excise this node if it is singly occupied by a shortcut */
  1437. if (nr_free == ASSOC_ARRAY_FAN_OUT - 1) {
  1438. for (slot = 0; slot < ASSOC_ARRAY_FAN_OUT; slot++)
  1439. if ((ptr = new_n->slots[slot]))
  1440. break;
  1441. if (assoc_array_ptr_is_meta(ptr) &&
  1442. assoc_array_ptr_is_shortcut(ptr)) {
  1443. pr_devel("excise node %p with 1 shortcut\n", new_n);
  1444. new_s = assoc_array_ptr_to_shortcut(ptr);
  1445. new_parent = new_n->back_pointer;
  1446. slot = new_n->parent_slot;
  1447. kfree(new_n);
  1448. if (!new_parent) {
  1449. new_s->back_pointer = NULL;
  1450. new_s->parent_slot = 0;
  1451. new_root = ptr;
  1452. goto gc_complete;
  1453. }
  1454. if (assoc_array_ptr_is_shortcut(new_parent)) {
  1455. /* We can discard any preceding shortcut also */
  1456. struct assoc_array_shortcut *s =
  1457. assoc_array_ptr_to_shortcut(new_parent);
  1458. pr_devel("excise preceding shortcut\n");
  1459. new_parent = new_s->back_pointer = s->back_pointer;
  1460. slot = new_s->parent_slot = s->parent_slot;
  1461. kfree(s);
  1462. if (!new_parent) {
  1463. new_s->back_pointer = NULL;
  1464. new_s->parent_slot = 0;
  1465. new_root = ptr;
  1466. goto gc_complete;
  1467. }
  1468. }
  1469. new_s->back_pointer = new_parent;
  1470. new_s->parent_slot = slot;
  1471. new_n = assoc_array_ptr_to_node(new_parent);
  1472. new_n->slots[slot] = ptr;
  1473. goto ascend_old_tree;
  1474. }
  1475. }
  1476. /* Excise any shortcuts we might encounter that point to nodes that
  1477. * only contain leaves.
  1478. */
  1479. ptr = new_n->back_pointer;
  1480. if (!ptr)
  1481. goto gc_complete;
  1482. if (assoc_array_ptr_is_shortcut(ptr)) {
  1483. new_s = assoc_array_ptr_to_shortcut(ptr);
  1484. new_parent = new_s->back_pointer;
  1485. slot = new_s->parent_slot;
  1486. if (new_n->nr_leaves_on_branch <= ASSOC_ARRAY_FAN_OUT) {
  1487. struct assoc_array_node *n;
  1488. pr_devel("excise shortcut\n");
  1489. new_n->back_pointer = new_parent;
  1490. new_n->parent_slot = slot;
  1491. kfree(new_s);
  1492. if (!new_parent) {
  1493. new_root = assoc_array_node_to_ptr(new_n);
  1494. goto gc_complete;
  1495. }
  1496. n = assoc_array_ptr_to_node(new_parent);
  1497. n->slots[slot] = assoc_array_node_to_ptr(new_n);
  1498. }
  1499. } else {
  1500. new_parent = ptr;
  1501. }
  1502. new_n = assoc_array_ptr_to_node(new_parent);
  1503. ascend_old_tree:
  1504. ptr = node->back_pointer;
  1505. if (assoc_array_ptr_is_shortcut(ptr)) {
  1506. shortcut = assoc_array_ptr_to_shortcut(ptr);
  1507. slot = shortcut->parent_slot;
  1508. cursor = shortcut->back_pointer;
  1509. if (!cursor)
  1510. goto gc_complete;
  1511. } else {
  1512. slot = node->parent_slot;
  1513. cursor = ptr;
  1514. }
  1515. BUG_ON(!cursor);
  1516. node = assoc_array_ptr_to_node(cursor);
  1517. slot++;
  1518. goto continue_node;
  1519. gc_complete:
  1520. edit->set[0].to = new_root;
  1521. assoc_array_apply_edit(edit);
  1522. array->nr_leaves_on_tree = nr_leaves_on_tree;
  1523. return 0;
  1524. enomem:
  1525. pr_devel("enomem\n");
  1526. assoc_array_destroy_subtree(new_root, edit->ops);
  1527. kfree(edit);
  1528. return -ENOMEM;
  1529. }