assoc_array.c 53 KB

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