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