assoc_array.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735
  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. blank = ULONG_MAX << (level & ASSOC_ARRAY_KEY_CHUNK_MASK);
  696. pr_devel("blank off [%zu] %d: %lx\n", keylen - 1, level, blank);
  697. new_s0->index_key[keylen - 1] &= ~blank;
  698. /* This now reduces to a node splitting exercise for which we'll need
  699. * to regenerate the disparity table.
  700. */
  701. for (i = 0; i < ASSOC_ARRAY_FAN_OUT; i++) {
  702. ptr = node->slots[i];
  703. base_seg = ops->get_object_key_chunk(assoc_array_ptr_to_leaf(ptr),
  704. level);
  705. base_seg >>= level & ASSOC_ARRAY_KEY_CHUNK_MASK;
  706. edit->segment_cache[i] = base_seg & ASSOC_ARRAY_FAN_MASK;
  707. }
  708. base_seg = ops->get_key_chunk(index_key, level);
  709. base_seg >>= level & ASSOC_ARRAY_KEY_CHUNK_MASK;
  710. edit->segment_cache[ASSOC_ARRAY_FAN_OUT] = base_seg & ASSOC_ARRAY_FAN_MASK;
  711. goto do_split_node;
  712. }
  713. /*
  714. * Handle insertion into the middle of a shortcut.
  715. */
  716. static bool assoc_array_insert_mid_shortcut(struct assoc_array_edit *edit,
  717. const struct assoc_array_ops *ops,
  718. struct assoc_array_walk_result *result)
  719. {
  720. struct assoc_array_shortcut *shortcut, *new_s0, *new_s1;
  721. struct assoc_array_node *node, *new_n0, *side;
  722. unsigned long sc_segments, dissimilarity, blank;
  723. size_t keylen;
  724. int level, sc_level, diff;
  725. int sc_slot;
  726. shortcut = result->wrong_shortcut.shortcut;
  727. level = result->wrong_shortcut.level;
  728. sc_level = result->wrong_shortcut.sc_level;
  729. sc_segments = result->wrong_shortcut.sc_segments;
  730. dissimilarity = result->wrong_shortcut.dissimilarity;
  731. pr_devel("-->%s(ix=%d dis=%lx scix=%d)\n",
  732. __func__, level, dissimilarity, sc_level);
  733. /* We need to split a shortcut and insert a node between the two
  734. * pieces. Zero-length pieces will be dispensed with entirely.
  735. *
  736. * First of all, we need to find out in which level the first
  737. * difference was.
  738. */
  739. diff = __ffs(dissimilarity);
  740. diff &= ~ASSOC_ARRAY_LEVEL_STEP_MASK;
  741. diff += sc_level & ~ASSOC_ARRAY_KEY_CHUNK_MASK;
  742. pr_devel("diff=%d\n", diff);
  743. if (!shortcut->back_pointer) {
  744. edit->set[0].ptr = &edit->array->root;
  745. } else if (assoc_array_ptr_is_node(shortcut->back_pointer)) {
  746. node = assoc_array_ptr_to_node(shortcut->back_pointer);
  747. edit->set[0].ptr = &node->slots[shortcut->parent_slot];
  748. } else {
  749. BUG();
  750. }
  751. edit->excised_meta[0] = assoc_array_shortcut_to_ptr(shortcut);
  752. /* Create a new node now since we're going to need it anyway */
  753. new_n0 = kzalloc(sizeof(struct assoc_array_node), GFP_KERNEL);
  754. if (!new_n0)
  755. return false;
  756. edit->new_meta[0] = assoc_array_node_to_ptr(new_n0);
  757. edit->adjust_count_on = new_n0;
  758. /* Insert a new shortcut before the new node if this segment isn't of
  759. * zero length - otherwise we just connect the new node directly to the
  760. * parent.
  761. */
  762. level += ASSOC_ARRAY_LEVEL_STEP;
  763. if (diff > level) {
  764. pr_devel("pre-shortcut %d...%d\n", level, diff);
  765. keylen = round_up(diff, ASSOC_ARRAY_KEY_CHUNK_SIZE);
  766. keylen >>= ASSOC_ARRAY_KEY_CHUNK_SHIFT;
  767. new_s0 = kzalloc(sizeof(struct assoc_array_shortcut) +
  768. keylen * sizeof(unsigned long), GFP_KERNEL);
  769. if (!new_s0)
  770. return false;
  771. edit->new_meta[1] = assoc_array_shortcut_to_ptr(new_s0);
  772. edit->set[0].to = assoc_array_shortcut_to_ptr(new_s0);
  773. new_s0->back_pointer = shortcut->back_pointer;
  774. new_s0->parent_slot = shortcut->parent_slot;
  775. new_s0->next_node = assoc_array_node_to_ptr(new_n0);
  776. new_s0->skip_to_level = diff;
  777. new_n0->back_pointer = assoc_array_shortcut_to_ptr(new_s0);
  778. new_n0->parent_slot = 0;
  779. memcpy(new_s0->index_key, shortcut->index_key,
  780. keylen * sizeof(unsigned long));
  781. blank = ULONG_MAX << (diff & ASSOC_ARRAY_KEY_CHUNK_MASK);
  782. pr_devel("blank off [%zu] %d: %lx\n", keylen - 1, diff, blank);
  783. new_s0->index_key[keylen - 1] &= ~blank;
  784. } else {
  785. pr_devel("no pre-shortcut\n");
  786. edit->set[0].to = assoc_array_node_to_ptr(new_n0);
  787. new_n0->back_pointer = shortcut->back_pointer;
  788. new_n0->parent_slot = shortcut->parent_slot;
  789. }
  790. side = assoc_array_ptr_to_node(shortcut->next_node);
  791. new_n0->nr_leaves_on_branch = side->nr_leaves_on_branch;
  792. /* We need to know which slot in the new node is going to take a
  793. * metadata pointer.
  794. */
  795. sc_slot = sc_segments >> (diff & ASSOC_ARRAY_KEY_CHUNK_MASK);
  796. sc_slot &= ASSOC_ARRAY_FAN_MASK;
  797. pr_devel("new slot %lx >> %d -> %d\n",
  798. sc_segments, diff & ASSOC_ARRAY_KEY_CHUNK_MASK, sc_slot);
  799. /* Determine whether we need to follow the new node with a replacement
  800. * for the current shortcut. We could in theory reuse the current
  801. * shortcut if its parent slot number doesn't change - but that's a
  802. * 1-in-16 chance so not worth expending the code upon.
  803. */
  804. level = diff + ASSOC_ARRAY_LEVEL_STEP;
  805. if (level < shortcut->skip_to_level) {
  806. pr_devel("post-shortcut %d...%d\n", level, shortcut->skip_to_level);
  807. keylen = round_up(shortcut->skip_to_level, ASSOC_ARRAY_KEY_CHUNK_SIZE);
  808. keylen >>= ASSOC_ARRAY_KEY_CHUNK_SHIFT;
  809. new_s1 = kzalloc(sizeof(struct assoc_array_shortcut) +
  810. keylen * sizeof(unsigned long), GFP_KERNEL);
  811. if (!new_s1)
  812. return false;
  813. edit->new_meta[2] = assoc_array_shortcut_to_ptr(new_s1);
  814. new_s1->back_pointer = assoc_array_node_to_ptr(new_n0);
  815. new_s1->parent_slot = sc_slot;
  816. new_s1->next_node = shortcut->next_node;
  817. new_s1->skip_to_level = shortcut->skip_to_level;
  818. new_n0->slots[sc_slot] = assoc_array_shortcut_to_ptr(new_s1);
  819. memcpy(new_s1->index_key, shortcut->index_key,
  820. keylen * sizeof(unsigned long));
  821. edit->set[1].ptr = &side->back_pointer;
  822. edit->set[1].to = assoc_array_shortcut_to_ptr(new_s1);
  823. } else {
  824. pr_devel("no post-shortcut\n");
  825. /* We don't have to replace the pointed-to node as long as we
  826. * use memory barriers to make sure the parent slot number is
  827. * changed before the back pointer (the parent slot number is
  828. * irrelevant to the old parent shortcut).
  829. */
  830. new_n0->slots[sc_slot] = shortcut->next_node;
  831. edit->set_parent_slot[0].p = &side->parent_slot;
  832. edit->set_parent_slot[0].to = sc_slot;
  833. edit->set[1].ptr = &side->back_pointer;
  834. edit->set[1].to = assoc_array_node_to_ptr(new_n0);
  835. }
  836. /* Install the new leaf in a spare slot in the new node. */
  837. if (sc_slot == 0)
  838. edit->leaf_p = &new_n0->slots[1];
  839. else
  840. edit->leaf_p = &new_n0->slots[0];
  841. pr_devel("<--%s() = ok [split shortcut]\n", __func__);
  842. return edit;
  843. }
  844. /**
  845. * assoc_array_insert - Script insertion of an object into an associative array
  846. * @array: The array to insert into.
  847. * @ops: The operations to use.
  848. * @index_key: The key to insert at.
  849. * @object: The object to insert.
  850. *
  851. * Precalculate and preallocate a script for the insertion or replacement of an
  852. * object in an associative array. This results in an edit script that can
  853. * either be applied or cancelled.
  854. *
  855. * The function returns a pointer to an edit script or -ENOMEM.
  856. *
  857. * The caller should lock against other modifications and must continue to hold
  858. * the lock until assoc_array_apply_edit() has been called.
  859. *
  860. * Accesses to the tree may take place concurrently with this function,
  861. * provided they hold the RCU read lock.
  862. */
  863. struct assoc_array_edit *assoc_array_insert(struct assoc_array *array,
  864. const struct assoc_array_ops *ops,
  865. const void *index_key,
  866. void *object)
  867. {
  868. struct assoc_array_walk_result result;
  869. struct assoc_array_edit *edit;
  870. pr_devel("-->%s()\n", __func__);
  871. /* The leaf pointer we're given must not have the bottom bit set as we
  872. * use those for type-marking the pointer. NULL pointers are also not
  873. * allowed as they indicate an empty slot but we have to allow them
  874. * here as they can be updated later.
  875. */
  876. BUG_ON(assoc_array_ptr_is_meta(object));
  877. edit = kzalloc(sizeof(struct assoc_array_edit), GFP_KERNEL);
  878. if (!edit)
  879. return ERR_PTR(-ENOMEM);
  880. edit->array = array;
  881. edit->ops = ops;
  882. edit->leaf = assoc_array_leaf_to_ptr(object);
  883. edit->adjust_count_by = 1;
  884. switch (assoc_array_walk(array, ops, index_key, &result)) {
  885. case assoc_array_walk_tree_empty:
  886. /* Allocate a root node if there isn't one yet */
  887. if (!assoc_array_insert_in_empty_tree(edit))
  888. goto enomem;
  889. return edit;
  890. case assoc_array_walk_found_terminal_node:
  891. /* We found a node that doesn't have a node/shortcut pointer in
  892. * the slot corresponding to the index key that we have to
  893. * follow.
  894. */
  895. if (!assoc_array_insert_into_terminal_node(edit, ops, index_key,
  896. &result))
  897. goto enomem;
  898. return edit;
  899. case assoc_array_walk_found_wrong_shortcut:
  900. /* We found a shortcut that didn't match our key in a slot we
  901. * needed to follow.
  902. */
  903. if (!assoc_array_insert_mid_shortcut(edit, ops, &result))
  904. goto enomem;
  905. return edit;
  906. }
  907. enomem:
  908. /* Clean up after an out of memory error */
  909. pr_devel("enomem\n");
  910. assoc_array_cancel_edit(edit);
  911. return ERR_PTR(-ENOMEM);
  912. }
  913. /**
  914. * assoc_array_insert_set_object - Set the new object pointer in an edit script
  915. * @edit: The edit script to modify.
  916. * @object: The object pointer to set.
  917. *
  918. * Change the object to be inserted in an edit script. The object pointed to
  919. * by the old object is not freed. This must be done prior to applying the
  920. * script.
  921. */
  922. void assoc_array_insert_set_object(struct assoc_array_edit *edit, void *object)
  923. {
  924. BUG_ON(!object);
  925. edit->leaf = assoc_array_leaf_to_ptr(object);
  926. }
  927. struct assoc_array_delete_collapse_context {
  928. struct assoc_array_node *node;
  929. const void *skip_leaf;
  930. int slot;
  931. };
  932. /*
  933. * Subtree collapse to node iterator.
  934. */
  935. static int assoc_array_delete_collapse_iterator(const void *leaf,
  936. void *iterator_data)
  937. {
  938. struct assoc_array_delete_collapse_context *collapse = iterator_data;
  939. if (leaf == collapse->skip_leaf)
  940. return 0;
  941. BUG_ON(collapse->slot >= ASSOC_ARRAY_FAN_OUT);
  942. collapse->node->slots[collapse->slot++] = assoc_array_leaf_to_ptr(leaf);
  943. return 0;
  944. }
  945. /**
  946. * assoc_array_delete - Script deletion of an object from an associative array
  947. * @array: The array to search.
  948. * @ops: The operations to use.
  949. * @index_key: The key to the object.
  950. *
  951. * Precalculate and preallocate a script for the deletion of an object from an
  952. * associative array. This results in an edit script that can either be
  953. * applied or cancelled.
  954. *
  955. * The function returns a pointer to an edit script if the object was found,
  956. * NULL if the object was not found or -ENOMEM.
  957. *
  958. * The caller should lock against other modifications and must continue to hold
  959. * the lock until assoc_array_apply_edit() has been called.
  960. *
  961. * Accesses to the tree may take place concurrently with this function,
  962. * provided they hold the RCU read lock.
  963. */
  964. struct assoc_array_edit *assoc_array_delete(struct assoc_array *array,
  965. const struct assoc_array_ops *ops,
  966. const void *index_key)
  967. {
  968. struct assoc_array_delete_collapse_context collapse;
  969. struct assoc_array_walk_result result;
  970. struct assoc_array_node *node, *new_n0;
  971. struct assoc_array_edit *edit;
  972. struct assoc_array_ptr *ptr;
  973. bool has_meta;
  974. int slot, i;
  975. pr_devel("-->%s()\n", __func__);
  976. edit = kzalloc(sizeof(struct assoc_array_edit), GFP_KERNEL);
  977. if (!edit)
  978. return ERR_PTR(-ENOMEM);
  979. edit->array = array;
  980. edit->ops = ops;
  981. edit->adjust_count_by = -1;
  982. switch (assoc_array_walk(array, ops, index_key, &result)) {
  983. case assoc_array_walk_found_terminal_node:
  984. /* We found a node that should contain the leaf we've been
  985. * asked to remove - *if* it's in the tree.
  986. */
  987. pr_devel("terminal_node\n");
  988. node = result.terminal_node.node;
  989. for (slot = 0; slot < ASSOC_ARRAY_FAN_OUT; slot++) {
  990. ptr = node->slots[slot];
  991. if (ptr &&
  992. assoc_array_ptr_is_leaf(ptr) &&
  993. ops->compare_object(assoc_array_ptr_to_leaf(ptr),
  994. index_key))
  995. goto found_leaf;
  996. }
  997. case assoc_array_walk_tree_empty:
  998. case assoc_array_walk_found_wrong_shortcut:
  999. default:
  1000. assoc_array_cancel_edit(edit);
  1001. pr_devel("not found\n");
  1002. return NULL;
  1003. }
  1004. found_leaf:
  1005. BUG_ON(array->nr_leaves_on_tree <= 0);
  1006. /* In the simplest form of deletion we just clear the slot and release
  1007. * the leaf after a suitable interval.
  1008. */
  1009. edit->dead_leaf = node->slots[slot];
  1010. edit->set[0].ptr = &node->slots[slot];
  1011. edit->set[0].to = NULL;
  1012. edit->adjust_count_on = node;
  1013. /* If that concludes erasure of the last leaf, then delete the entire
  1014. * internal array.
  1015. */
  1016. if (array->nr_leaves_on_tree == 1) {
  1017. edit->set[1].ptr = &array->root;
  1018. edit->set[1].to = NULL;
  1019. edit->adjust_count_on = NULL;
  1020. edit->excised_subtree = array->root;
  1021. pr_devel("all gone\n");
  1022. return edit;
  1023. }
  1024. /* However, we'd also like to clear up some metadata blocks if we
  1025. * possibly can.
  1026. *
  1027. * We go for a simple algorithm of: if this node has FAN_OUT or fewer
  1028. * leaves in it, then attempt to collapse it - and attempt to
  1029. * recursively collapse up the tree.
  1030. *
  1031. * We could also try and collapse in partially filled subtrees to take
  1032. * up space in this node.
  1033. */
  1034. if (node->nr_leaves_on_branch <= ASSOC_ARRAY_FAN_OUT + 1) {
  1035. struct assoc_array_node *parent, *grandparent;
  1036. struct assoc_array_ptr *ptr;
  1037. /* First of all, we need to know if this node has metadata so
  1038. * that we don't try collapsing if all the leaves are already
  1039. * here.
  1040. */
  1041. has_meta = false;
  1042. for (i = 0; i < ASSOC_ARRAY_FAN_OUT; i++) {
  1043. ptr = node->slots[i];
  1044. if (assoc_array_ptr_is_meta(ptr)) {
  1045. has_meta = true;
  1046. break;
  1047. }
  1048. }
  1049. pr_devel("leaves: %ld [m=%d]\n",
  1050. node->nr_leaves_on_branch - 1, has_meta);
  1051. /* Look further up the tree to see if we can collapse this node
  1052. * into a more proximal node too.
  1053. */
  1054. parent = node;
  1055. collapse_up:
  1056. pr_devel("collapse subtree: %ld\n", parent->nr_leaves_on_branch);
  1057. ptr = parent->back_pointer;
  1058. if (!ptr)
  1059. goto do_collapse;
  1060. if (assoc_array_ptr_is_shortcut(ptr)) {
  1061. struct assoc_array_shortcut *s = assoc_array_ptr_to_shortcut(ptr);
  1062. ptr = s->back_pointer;
  1063. if (!ptr)
  1064. goto do_collapse;
  1065. }
  1066. grandparent = assoc_array_ptr_to_node(ptr);
  1067. if (grandparent->nr_leaves_on_branch <= ASSOC_ARRAY_FAN_OUT + 1) {
  1068. parent = grandparent;
  1069. goto collapse_up;
  1070. }
  1071. do_collapse:
  1072. /* There's no point collapsing if the original node has no meta
  1073. * pointers to discard and if we didn't merge into one of that
  1074. * node's ancestry.
  1075. */
  1076. if (has_meta || parent != node) {
  1077. node = parent;
  1078. /* Create a new node to collapse into */
  1079. new_n0 = kzalloc(sizeof(struct assoc_array_node), GFP_KERNEL);
  1080. if (!new_n0)
  1081. goto enomem;
  1082. edit->new_meta[0] = assoc_array_node_to_ptr(new_n0);
  1083. new_n0->back_pointer = node->back_pointer;
  1084. new_n0->parent_slot = node->parent_slot;
  1085. new_n0->nr_leaves_on_branch = node->nr_leaves_on_branch;
  1086. edit->adjust_count_on = new_n0;
  1087. collapse.node = new_n0;
  1088. collapse.skip_leaf = assoc_array_ptr_to_leaf(edit->dead_leaf);
  1089. collapse.slot = 0;
  1090. assoc_array_subtree_iterate(assoc_array_node_to_ptr(node),
  1091. node->back_pointer,
  1092. assoc_array_delete_collapse_iterator,
  1093. &collapse);
  1094. pr_devel("collapsed %d,%lu\n", collapse.slot, new_n0->nr_leaves_on_branch);
  1095. BUG_ON(collapse.slot != new_n0->nr_leaves_on_branch - 1);
  1096. if (!node->back_pointer) {
  1097. edit->set[1].ptr = &array->root;
  1098. } else if (assoc_array_ptr_is_leaf(node->back_pointer)) {
  1099. BUG();
  1100. } else if (assoc_array_ptr_is_node(node->back_pointer)) {
  1101. struct assoc_array_node *p =
  1102. assoc_array_ptr_to_node(node->back_pointer);
  1103. edit->set[1].ptr = &p->slots[node->parent_slot];
  1104. } else if (assoc_array_ptr_is_shortcut(node->back_pointer)) {
  1105. struct assoc_array_shortcut *s =
  1106. assoc_array_ptr_to_shortcut(node->back_pointer);
  1107. edit->set[1].ptr = &s->next_node;
  1108. }
  1109. edit->set[1].to = assoc_array_node_to_ptr(new_n0);
  1110. edit->excised_subtree = assoc_array_node_to_ptr(node);
  1111. }
  1112. }
  1113. return edit;
  1114. enomem:
  1115. /* Clean up after an out of memory error */
  1116. pr_devel("enomem\n");
  1117. assoc_array_cancel_edit(edit);
  1118. return ERR_PTR(-ENOMEM);
  1119. }
  1120. /**
  1121. * assoc_array_clear - Script deletion of all objects from an associative array
  1122. * @array: The array to clear.
  1123. * @ops: The operations to use.
  1124. *
  1125. * Precalculate and preallocate a script for the deletion of all the objects
  1126. * from an associative array. This results in an edit script that can either
  1127. * be applied or cancelled.
  1128. *
  1129. * The function returns a pointer to an edit script if there are objects to be
  1130. * deleted, NULL if there are no objects in the array or -ENOMEM.
  1131. *
  1132. * The caller should lock against other modifications and must continue to hold
  1133. * the lock until assoc_array_apply_edit() has been called.
  1134. *
  1135. * Accesses to the tree may take place concurrently with this function,
  1136. * provided they hold the RCU read lock.
  1137. */
  1138. struct assoc_array_edit *assoc_array_clear(struct assoc_array *array,
  1139. const struct assoc_array_ops *ops)
  1140. {
  1141. struct assoc_array_edit *edit;
  1142. pr_devel("-->%s()\n", __func__);
  1143. if (!array->root)
  1144. return NULL;
  1145. edit = kzalloc(sizeof(struct assoc_array_edit), GFP_KERNEL);
  1146. if (!edit)
  1147. return ERR_PTR(-ENOMEM);
  1148. edit->array = array;
  1149. edit->ops = ops;
  1150. edit->set[1].ptr = &array->root;
  1151. edit->set[1].to = NULL;
  1152. edit->excised_subtree = array->root;
  1153. edit->ops_for_excised_subtree = ops;
  1154. pr_devel("all gone\n");
  1155. return edit;
  1156. }
  1157. /*
  1158. * Handle the deferred destruction after an applied edit.
  1159. */
  1160. static void assoc_array_rcu_cleanup(struct rcu_head *head)
  1161. {
  1162. struct assoc_array_edit *edit =
  1163. container_of(head, struct assoc_array_edit, rcu);
  1164. int i;
  1165. pr_devel("-->%s()\n", __func__);
  1166. if (edit->dead_leaf)
  1167. edit->ops->free_object(assoc_array_ptr_to_leaf(edit->dead_leaf));
  1168. for (i = 0; i < ARRAY_SIZE(edit->excised_meta); i++)
  1169. if (edit->excised_meta[i])
  1170. kfree(assoc_array_ptr_to_node(edit->excised_meta[i]));
  1171. if (edit->excised_subtree) {
  1172. BUG_ON(assoc_array_ptr_is_leaf(edit->excised_subtree));
  1173. if (assoc_array_ptr_is_node(edit->excised_subtree)) {
  1174. struct assoc_array_node *n =
  1175. assoc_array_ptr_to_node(edit->excised_subtree);
  1176. n->back_pointer = NULL;
  1177. } else {
  1178. struct assoc_array_shortcut *s =
  1179. assoc_array_ptr_to_shortcut(edit->excised_subtree);
  1180. s->back_pointer = NULL;
  1181. }
  1182. assoc_array_destroy_subtree(edit->excised_subtree,
  1183. edit->ops_for_excised_subtree);
  1184. }
  1185. kfree(edit);
  1186. }
  1187. /**
  1188. * assoc_array_apply_edit - Apply an edit script to an associative array
  1189. * @edit: The script to apply.
  1190. *
  1191. * Apply an edit script to an associative array to effect an insertion,
  1192. * deletion or clearance. As the edit script includes preallocated memory,
  1193. * this is guaranteed not to fail.
  1194. *
  1195. * The edit script, dead objects and dead metadata will be scheduled for
  1196. * destruction after an RCU grace period to permit those doing read-only
  1197. * accesses on the array to continue to do so under the RCU read lock whilst
  1198. * the edit is taking place.
  1199. */
  1200. void assoc_array_apply_edit(struct assoc_array_edit *edit)
  1201. {
  1202. struct assoc_array_shortcut *shortcut;
  1203. struct assoc_array_node *node;
  1204. struct assoc_array_ptr *ptr;
  1205. int i;
  1206. pr_devel("-->%s()\n", __func__);
  1207. smp_wmb();
  1208. if (edit->leaf_p)
  1209. *edit->leaf_p = edit->leaf;
  1210. smp_wmb();
  1211. for (i = 0; i < ARRAY_SIZE(edit->set_parent_slot); i++)
  1212. if (edit->set_parent_slot[i].p)
  1213. *edit->set_parent_slot[i].p = edit->set_parent_slot[i].to;
  1214. smp_wmb();
  1215. for (i = 0; i < ARRAY_SIZE(edit->set_backpointers); i++)
  1216. if (edit->set_backpointers[i])
  1217. *edit->set_backpointers[i] = edit->set_backpointers_to;
  1218. smp_wmb();
  1219. for (i = 0; i < ARRAY_SIZE(edit->set); i++)
  1220. if (edit->set[i].ptr)
  1221. *edit->set[i].ptr = edit->set[i].to;
  1222. if (edit->array->root == NULL) {
  1223. edit->array->nr_leaves_on_tree = 0;
  1224. } else if (edit->adjust_count_on) {
  1225. node = edit->adjust_count_on;
  1226. for (;;) {
  1227. node->nr_leaves_on_branch += edit->adjust_count_by;
  1228. ptr = node->back_pointer;
  1229. if (!ptr)
  1230. break;
  1231. if (assoc_array_ptr_is_shortcut(ptr)) {
  1232. shortcut = assoc_array_ptr_to_shortcut(ptr);
  1233. ptr = shortcut->back_pointer;
  1234. if (!ptr)
  1235. break;
  1236. }
  1237. BUG_ON(!assoc_array_ptr_is_node(ptr));
  1238. node = assoc_array_ptr_to_node(ptr);
  1239. }
  1240. edit->array->nr_leaves_on_tree += edit->adjust_count_by;
  1241. }
  1242. call_rcu(&edit->rcu, assoc_array_rcu_cleanup);
  1243. }
  1244. /**
  1245. * assoc_array_cancel_edit - Discard an edit script.
  1246. * @edit: The script to discard.
  1247. *
  1248. * Free an edit script and all the preallocated data it holds without making
  1249. * any changes to the associative array it was intended for.
  1250. *
  1251. * NOTE! In the case of an insertion script, this does _not_ release the leaf
  1252. * that was to be inserted. That is left to the caller.
  1253. */
  1254. void assoc_array_cancel_edit(struct assoc_array_edit *edit)
  1255. {
  1256. struct assoc_array_ptr *ptr;
  1257. int i;
  1258. pr_devel("-->%s()\n", __func__);
  1259. /* Clean up after an out of memory error */
  1260. for (i = 0; i < ARRAY_SIZE(edit->new_meta); i++) {
  1261. ptr = edit->new_meta[i];
  1262. if (ptr) {
  1263. if (assoc_array_ptr_is_node(ptr))
  1264. kfree(assoc_array_ptr_to_node(ptr));
  1265. else
  1266. kfree(assoc_array_ptr_to_shortcut(ptr));
  1267. }
  1268. }
  1269. kfree(edit);
  1270. }
  1271. /**
  1272. * assoc_array_gc - Garbage collect an associative array.
  1273. * @array: The array to clean.
  1274. * @ops: The operations to use.
  1275. * @iterator: A callback function to pass judgement on each object.
  1276. * @iterator_data: Private data for the callback function.
  1277. *
  1278. * Collect garbage from an associative array and pack down the internal tree to
  1279. * save memory.
  1280. *
  1281. * The iterator function is asked to pass judgement upon each object in the
  1282. * array. If it returns false, the object is discard and if it returns true,
  1283. * the object is kept. If it returns true, it must increment the object's
  1284. * usage count (or whatever it needs to do to retain it) before returning.
  1285. *
  1286. * This function returns 0 if successful or -ENOMEM if out of memory. In the
  1287. * latter case, the array is not changed.
  1288. *
  1289. * The caller should lock against other modifications and must continue to hold
  1290. * the lock until assoc_array_apply_edit() has been called.
  1291. *
  1292. * Accesses to the tree may take place concurrently with this function,
  1293. * provided they hold the RCU read lock.
  1294. */
  1295. int assoc_array_gc(struct assoc_array *array,
  1296. const struct assoc_array_ops *ops,
  1297. bool (*iterator)(void *object, void *iterator_data),
  1298. void *iterator_data)
  1299. {
  1300. struct assoc_array_shortcut *shortcut, *new_s;
  1301. struct assoc_array_node *node, *new_n;
  1302. struct assoc_array_edit *edit;
  1303. struct assoc_array_ptr *cursor, *ptr;
  1304. struct assoc_array_ptr *new_root, *new_parent, **new_ptr_pp;
  1305. unsigned long nr_leaves_on_tree;
  1306. int keylen, slot, nr_free, next_slot, i;
  1307. pr_devel("-->%s()\n", __func__);
  1308. if (!array->root)
  1309. return 0;
  1310. edit = kzalloc(sizeof(struct assoc_array_edit), GFP_KERNEL);
  1311. if (!edit)
  1312. return -ENOMEM;
  1313. edit->array = array;
  1314. edit->ops = ops;
  1315. edit->ops_for_excised_subtree = ops;
  1316. edit->set[0].ptr = &array->root;
  1317. edit->excised_subtree = array->root;
  1318. new_root = new_parent = NULL;
  1319. new_ptr_pp = &new_root;
  1320. cursor = array->root;
  1321. descend:
  1322. /* If this point is a shortcut, then we need to duplicate it and
  1323. * advance the target cursor.
  1324. */
  1325. if (assoc_array_ptr_is_shortcut(cursor)) {
  1326. shortcut = assoc_array_ptr_to_shortcut(cursor);
  1327. keylen = round_up(shortcut->skip_to_level, ASSOC_ARRAY_KEY_CHUNK_SIZE);
  1328. keylen >>= ASSOC_ARRAY_KEY_CHUNK_SHIFT;
  1329. new_s = kmalloc(sizeof(struct assoc_array_shortcut) +
  1330. keylen * sizeof(unsigned long), GFP_KERNEL);
  1331. if (!new_s)
  1332. goto enomem;
  1333. pr_devel("dup shortcut %p -> %p\n", shortcut, new_s);
  1334. memcpy(new_s, shortcut, (sizeof(struct assoc_array_shortcut) +
  1335. keylen * sizeof(unsigned long)));
  1336. new_s->back_pointer = new_parent;
  1337. new_s->parent_slot = shortcut->parent_slot;
  1338. *new_ptr_pp = new_parent = assoc_array_shortcut_to_ptr(new_s);
  1339. new_ptr_pp = &new_s->next_node;
  1340. cursor = shortcut->next_node;
  1341. }
  1342. /* Duplicate the node at this position */
  1343. node = assoc_array_ptr_to_node(cursor);
  1344. new_n = kzalloc(sizeof(struct assoc_array_node), GFP_KERNEL);
  1345. if (!new_n)
  1346. goto enomem;
  1347. pr_devel("dup node %p -> %p\n", node, new_n);
  1348. new_n->back_pointer = new_parent;
  1349. new_n->parent_slot = node->parent_slot;
  1350. *new_ptr_pp = new_parent = assoc_array_node_to_ptr(new_n);
  1351. new_ptr_pp = NULL;
  1352. slot = 0;
  1353. continue_node:
  1354. /* Filter across any leaves and gc any subtrees */
  1355. for (; slot < ASSOC_ARRAY_FAN_OUT; slot++) {
  1356. ptr = node->slots[slot];
  1357. if (!ptr)
  1358. continue;
  1359. if (assoc_array_ptr_is_leaf(ptr)) {
  1360. if (iterator(assoc_array_ptr_to_leaf(ptr),
  1361. iterator_data))
  1362. /* The iterator will have done any reference
  1363. * counting on the object for us.
  1364. */
  1365. new_n->slots[slot] = ptr;
  1366. continue;
  1367. }
  1368. new_ptr_pp = &new_n->slots[slot];
  1369. cursor = ptr;
  1370. goto descend;
  1371. }
  1372. pr_devel("-- compress node %p --\n", new_n);
  1373. /* Count up the number of empty slots in this node and work out the
  1374. * subtree leaf count.
  1375. */
  1376. new_n->nr_leaves_on_branch = 0;
  1377. nr_free = 0;
  1378. for (slot = 0; slot < ASSOC_ARRAY_FAN_OUT; slot++) {
  1379. ptr = new_n->slots[slot];
  1380. if (!ptr)
  1381. nr_free++;
  1382. else if (assoc_array_ptr_is_leaf(ptr))
  1383. new_n->nr_leaves_on_branch++;
  1384. }
  1385. pr_devel("free=%d, leaves=%lu\n", nr_free, new_n->nr_leaves_on_branch);
  1386. /* See what we can fold in */
  1387. next_slot = 0;
  1388. for (slot = 0; slot < ASSOC_ARRAY_FAN_OUT; slot++) {
  1389. struct assoc_array_shortcut *s;
  1390. struct assoc_array_node *child;
  1391. ptr = new_n->slots[slot];
  1392. if (!ptr || assoc_array_ptr_is_leaf(ptr))
  1393. continue;
  1394. s = NULL;
  1395. if (assoc_array_ptr_is_shortcut(ptr)) {
  1396. s = assoc_array_ptr_to_shortcut(ptr);
  1397. ptr = s->next_node;
  1398. }
  1399. child = assoc_array_ptr_to_node(ptr);
  1400. new_n->nr_leaves_on_branch += child->nr_leaves_on_branch;
  1401. if (child->nr_leaves_on_branch <= nr_free + 1) {
  1402. /* Fold the child node into this one */
  1403. pr_devel("[%d] fold node %lu/%d [nx %d]\n",
  1404. slot, child->nr_leaves_on_branch, nr_free + 1,
  1405. next_slot);
  1406. /* We would already have reaped an intervening shortcut
  1407. * on the way back up the tree.
  1408. */
  1409. BUG_ON(s);
  1410. new_n->slots[slot] = NULL;
  1411. nr_free++;
  1412. if (slot < next_slot)
  1413. next_slot = slot;
  1414. for (i = 0; i < ASSOC_ARRAY_FAN_OUT; i++) {
  1415. struct assoc_array_ptr *p = child->slots[i];
  1416. if (!p)
  1417. continue;
  1418. BUG_ON(assoc_array_ptr_is_meta(p));
  1419. while (new_n->slots[next_slot])
  1420. next_slot++;
  1421. BUG_ON(next_slot >= ASSOC_ARRAY_FAN_OUT);
  1422. new_n->slots[next_slot++] = p;
  1423. nr_free--;
  1424. }
  1425. kfree(child);
  1426. } else {
  1427. pr_devel("[%d] retain node %lu/%d [nx %d]\n",
  1428. slot, child->nr_leaves_on_branch, nr_free + 1,
  1429. next_slot);
  1430. }
  1431. }
  1432. pr_devel("after: %lu\n", new_n->nr_leaves_on_branch);
  1433. nr_leaves_on_tree = new_n->nr_leaves_on_branch;
  1434. /* Excise this node if it is singly occupied by a shortcut */
  1435. if (nr_free == ASSOC_ARRAY_FAN_OUT - 1) {
  1436. for (slot = 0; slot < ASSOC_ARRAY_FAN_OUT; slot++)
  1437. if ((ptr = new_n->slots[slot]))
  1438. break;
  1439. if (assoc_array_ptr_is_meta(ptr) &&
  1440. assoc_array_ptr_is_shortcut(ptr)) {
  1441. pr_devel("excise node %p with 1 shortcut\n", new_n);
  1442. new_s = assoc_array_ptr_to_shortcut(ptr);
  1443. new_parent = new_n->back_pointer;
  1444. slot = new_n->parent_slot;
  1445. kfree(new_n);
  1446. if (!new_parent) {
  1447. new_s->back_pointer = NULL;
  1448. new_s->parent_slot = 0;
  1449. new_root = ptr;
  1450. goto gc_complete;
  1451. }
  1452. if (assoc_array_ptr_is_shortcut(new_parent)) {
  1453. /* We can discard any preceding shortcut also */
  1454. struct assoc_array_shortcut *s =
  1455. assoc_array_ptr_to_shortcut(new_parent);
  1456. pr_devel("excise preceding shortcut\n");
  1457. new_parent = new_s->back_pointer = s->back_pointer;
  1458. slot = new_s->parent_slot = s->parent_slot;
  1459. kfree(s);
  1460. if (!new_parent) {
  1461. new_s->back_pointer = NULL;
  1462. new_s->parent_slot = 0;
  1463. new_root = ptr;
  1464. goto gc_complete;
  1465. }
  1466. }
  1467. new_s->back_pointer = new_parent;
  1468. new_s->parent_slot = slot;
  1469. new_n = assoc_array_ptr_to_node(new_parent);
  1470. new_n->slots[slot] = ptr;
  1471. goto ascend_old_tree;
  1472. }
  1473. }
  1474. /* Excise any shortcuts we might encounter that point to nodes that
  1475. * only contain leaves.
  1476. */
  1477. ptr = new_n->back_pointer;
  1478. if (!ptr)
  1479. goto gc_complete;
  1480. if (assoc_array_ptr_is_shortcut(ptr)) {
  1481. new_s = assoc_array_ptr_to_shortcut(ptr);
  1482. new_parent = new_s->back_pointer;
  1483. slot = new_s->parent_slot;
  1484. if (new_n->nr_leaves_on_branch <= ASSOC_ARRAY_FAN_OUT) {
  1485. struct assoc_array_node *n;
  1486. pr_devel("excise shortcut\n");
  1487. new_n->back_pointer = new_parent;
  1488. new_n->parent_slot = slot;
  1489. kfree(new_s);
  1490. if (!new_parent) {
  1491. new_root = assoc_array_node_to_ptr(new_n);
  1492. goto gc_complete;
  1493. }
  1494. n = assoc_array_ptr_to_node(new_parent);
  1495. n->slots[slot] = assoc_array_node_to_ptr(new_n);
  1496. }
  1497. } else {
  1498. new_parent = ptr;
  1499. }
  1500. new_n = assoc_array_ptr_to_node(new_parent);
  1501. ascend_old_tree:
  1502. ptr = node->back_pointer;
  1503. if (assoc_array_ptr_is_shortcut(ptr)) {
  1504. shortcut = assoc_array_ptr_to_shortcut(ptr);
  1505. slot = shortcut->parent_slot;
  1506. cursor = shortcut->back_pointer;
  1507. if (!cursor)
  1508. goto gc_complete;
  1509. } else {
  1510. slot = node->parent_slot;
  1511. cursor = ptr;
  1512. }
  1513. BUG_ON(!cursor);
  1514. node = assoc_array_ptr_to_node(cursor);
  1515. slot++;
  1516. goto continue_node;
  1517. gc_complete:
  1518. edit->set[0].to = new_root;
  1519. assoc_array_apply_edit(edit);
  1520. array->nr_leaves_on_tree = nr_leaves_on_tree;
  1521. return 0;
  1522. enomem:
  1523. pr_devel("enomem\n");
  1524. assoc_array_destroy_subtree(new_root, edit->ops);
  1525. kfree(edit);
  1526. return -ENOMEM;
  1527. }