assoc_array.c 53 KB

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