assoc_array.c 52 KB

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