1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/rcupdate.h>
15#include <linux/slab.h>
16#include <linux/err.h>
17#include <linux/assoc_array_priv.h>
18
19
20
21
22
23static int assoc_array_subtree_iterate(const struct assoc_array_ptr *root,
24 const struct assoc_array_ptr *stop,
25 int (*iterator)(const void *leaf,
26 void *iterator_data),
27 void *iterator_data)
28{
29 const struct assoc_array_shortcut *shortcut;
30 const struct assoc_array_node *node;
31 const struct assoc_array_ptr *cursor, *ptr, *parent;
32 unsigned long has_meta;
33 int slot, ret;
34
35 cursor = root;
36
37begin_node:
38 if (assoc_array_ptr_is_shortcut(cursor)) {
39
40 shortcut = assoc_array_ptr_to_shortcut(cursor);
41 cursor = READ_ONCE(shortcut->next_node);
42 }
43
44 node = assoc_array_ptr_to_node(cursor);
45 slot = 0;
46
47
48
49
50
51
52
53
54 has_meta = 0;
55 for (; slot < ASSOC_ARRAY_FAN_OUT; slot++) {
56 ptr = READ_ONCE(node->slots[slot]);
57 has_meta |= (unsigned long)ptr;
58 if (ptr && assoc_array_ptr_is_leaf(ptr)) {
59
60
61
62
63 ret = iterator(assoc_array_ptr_to_leaf(ptr),
64 iterator_data);
65 if (ret)
66 return ret;
67 }
68 }
69
70
71
72
73
74
75
76
77
78 if (!(has_meta & ASSOC_ARRAY_PTR_META_TYPE))
79 goto finished_node;
80 slot = 0;
81
82continue_node:
83 node = assoc_array_ptr_to_node(cursor);
84 for (; slot < ASSOC_ARRAY_FAN_OUT; slot++) {
85 ptr = READ_ONCE(node->slots[slot]);
86 if (assoc_array_ptr_is_meta(ptr)) {
87 cursor = ptr;
88 goto begin_node;
89 }
90 }
91
92finished_node:
93
94 parent = READ_ONCE(node->back_pointer);
95 slot = node->parent_slot;
96 if (parent == stop)
97 return 0;
98
99 if (assoc_array_ptr_is_shortcut(parent)) {
100 shortcut = assoc_array_ptr_to_shortcut(parent);
101 cursor = parent;
102 parent = READ_ONCE(shortcut->back_pointer);
103 slot = shortcut->parent_slot;
104 if (parent == stop)
105 return 0;
106 }
107
108
109 cursor = parent;
110 slot++;
111 goto continue_node;
112}
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137int assoc_array_iterate(const struct assoc_array *array,
138 int (*iterator)(const void *object,
139 void *iterator_data),
140 void *iterator_data)
141{
142 struct assoc_array_ptr *root = READ_ONCE(array->root);
143
144 if (!root)
145 return 0;
146 return assoc_array_subtree_iterate(root, NULL, iterator, iterator_data);
147}
148
149enum assoc_array_walk_status {
150 assoc_array_walk_tree_empty,
151 assoc_array_walk_found_terminal_node,
152 assoc_array_walk_found_wrong_shortcut,
153};
154
155struct assoc_array_walk_result {
156 struct {
157 struct assoc_array_node *node;
158 int level;
159 int slot;
160 } terminal_node;
161 struct {
162 struct assoc_array_shortcut *shortcut;
163 int level;
164 int sc_level;
165 unsigned long sc_segments;
166 unsigned long dissimilarity;
167 } wrong_shortcut;
168};
169
170
171
172
173static enum assoc_array_walk_status
174assoc_array_walk(const struct assoc_array *array,
175 const struct assoc_array_ops *ops,
176 const void *index_key,
177 struct assoc_array_walk_result *result)
178{
179 struct assoc_array_shortcut *shortcut;
180 struct assoc_array_node *node;
181 struct assoc_array_ptr *cursor, *ptr;
182 unsigned long sc_segments, dissimilarity;
183 unsigned long segments;
184 int level, sc_level, next_sc_level;
185 int slot;
186
187 pr_devel("-->%s()\n", __func__);
188
189 cursor = READ_ONCE(array->root);
190 if (!cursor)
191 return assoc_array_walk_tree_empty;
192
193 level = 0;
194
195
196
197
198
199
200
201
202jumped:
203 segments = ops->get_key_chunk(index_key, level);
204 pr_devel("segments[%d]: %lx\n", level, segments);
205
206 if (assoc_array_ptr_is_shortcut(cursor))
207 goto follow_shortcut;
208
209consider_node:
210 node = assoc_array_ptr_to_node(cursor);
211 slot = segments >> (level & ASSOC_ARRAY_KEY_CHUNK_MASK);
212 slot &= ASSOC_ARRAY_FAN_MASK;
213 ptr = READ_ONCE(node->slots[slot]);
214
215 pr_devel("consider slot %x [ix=%d type=%lu]\n",
216 slot, level, (unsigned long)ptr & 3);
217
218 if (!assoc_array_ptr_is_meta(ptr)) {
219
220
221
222 result->terminal_node.node = node;
223 result->terminal_node.level = level;
224 result->terminal_node.slot = slot;
225 pr_devel("<--%s() = terminal_node\n", __func__);
226 return assoc_array_walk_found_terminal_node;
227 }
228
229 if (assoc_array_ptr_is_node(ptr)) {
230
231
232
233 cursor = ptr;
234 level += ASSOC_ARRAY_LEVEL_STEP;
235 if ((level & ASSOC_ARRAY_KEY_CHUNK_MASK) != 0)
236 goto consider_node;
237 goto jumped;
238 }
239
240
241
242
243
244 cursor = ptr;
245follow_shortcut:
246 shortcut = assoc_array_ptr_to_shortcut(cursor);
247 pr_devel("shortcut to %d\n", shortcut->skip_to_level);
248 sc_level = level + ASSOC_ARRAY_LEVEL_STEP;
249 BUG_ON(sc_level > shortcut->skip_to_level);
250
251 do {
252
253
254
255
256 if ((sc_level & ASSOC_ARRAY_KEY_CHUNK_MASK) == 0)
257 segments = ops->get_key_chunk(index_key, sc_level);
258
259 sc_segments = shortcut->index_key[sc_level >> ASSOC_ARRAY_KEY_CHUNK_SHIFT];
260 dissimilarity = segments ^ sc_segments;
261
262 if (round_up(sc_level, ASSOC_ARRAY_KEY_CHUNK_SIZE) > shortcut->skip_to_level) {
263
264 int shift = shortcut->skip_to_level & ASSOC_ARRAY_KEY_CHUNK_MASK;
265 dissimilarity &= ~(ULONG_MAX << shift);
266 next_sc_level = shortcut->skip_to_level;
267 } else {
268 next_sc_level = sc_level + ASSOC_ARRAY_KEY_CHUNK_SIZE;
269 next_sc_level = round_down(next_sc_level, ASSOC_ARRAY_KEY_CHUNK_SIZE);
270 }
271
272 if (dissimilarity != 0) {
273
274 result->wrong_shortcut.shortcut = shortcut;
275 result->wrong_shortcut.level = level;
276 result->wrong_shortcut.sc_level = sc_level;
277 result->wrong_shortcut.sc_segments = sc_segments;
278 result->wrong_shortcut.dissimilarity = dissimilarity;
279 return assoc_array_walk_found_wrong_shortcut;
280 }
281
282 sc_level = next_sc_level;
283 } while (sc_level < shortcut->skip_to_level);
284
285
286 cursor = READ_ONCE(shortcut->next_node);
287 if (((level ^ sc_level) & ~ASSOC_ARRAY_KEY_CHUNK_MASK) != 0) {
288 level = sc_level;
289 goto jumped;
290 } else {
291 level = sc_level;
292 goto consider_node;
293 }
294}
295
296
297
298
299
300
301
302
303
304
305
306
307
308void *assoc_array_find(const struct assoc_array *array,
309 const struct assoc_array_ops *ops,
310 const void *index_key)
311{
312 struct assoc_array_walk_result result;
313 const struct assoc_array_node *node;
314 const struct assoc_array_ptr *ptr;
315 const void *leaf;
316 int slot;
317
318 if (assoc_array_walk(array, ops, index_key, &result) !=
319 assoc_array_walk_found_terminal_node)
320 return NULL;
321
322 node = result.terminal_node.node;
323
324
325
326
327 for (slot = 0; slot < ASSOC_ARRAY_FAN_OUT; slot++) {
328 ptr = READ_ONCE(node->slots[slot]);
329 if (ptr && assoc_array_ptr_is_leaf(ptr)) {
330
331
332
333
334 leaf = assoc_array_ptr_to_leaf(ptr);
335 if (ops->compare_object(leaf, index_key))
336 return (void *)leaf;
337 }
338 }
339
340 return NULL;
341}
342
343
344
345
346
347static void assoc_array_destroy_subtree(struct assoc_array_ptr *root,
348 const struct assoc_array_ops *ops)
349{
350 struct assoc_array_shortcut *shortcut;
351 struct assoc_array_node *node;
352 struct assoc_array_ptr *cursor, *parent = NULL;
353 int slot = -1;
354
355 pr_devel("-->%s()\n", __func__);
356
357 cursor = root;
358 if (!cursor) {
359 pr_devel("empty\n");
360 return;
361 }
362
363move_to_meta:
364 if (assoc_array_ptr_is_shortcut(cursor)) {
365
366 pr_devel("[%d] shortcut\n", slot);
367 BUG_ON(!assoc_array_ptr_is_shortcut(cursor));
368 shortcut = assoc_array_ptr_to_shortcut(cursor);
369 BUG_ON(shortcut->back_pointer != parent);
370 BUG_ON(slot != -1 && shortcut->parent_slot != slot);
371 parent = cursor;
372 cursor = shortcut->next_node;
373 slot = -1;
374 BUG_ON(!assoc_array_ptr_is_node(cursor));
375 }
376
377 pr_devel("[%d] node\n", slot);
378 node = assoc_array_ptr_to_node(cursor);
379 BUG_ON(node->back_pointer != parent);
380 BUG_ON(slot != -1 && node->parent_slot != slot);
381 slot = 0;
382
383continue_node:
384 pr_devel("Node %p [back=%p]\n", node, node->back_pointer);
385 for (; slot < ASSOC_ARRAY_FAN_OUT; slot++) {
386 struct assoc_array_ptr *ptr = node->slots[slot];
387 if (!ptr)
388 continue;
389 if (assoc_array_ptr_is_meta(ptr)) {
390 parent = cursor;
391 cursor = ptr;
392 goto move_to_meta;
393 }
394
395 if (ops) {
396 pr_devel("[%d] free leaf\n", slot);
397 ops->free_object(assoc_array_ptr_to_leaf(ptr));
398 }
399 }
400
401 parent = node->back_pointer;
402 slot = node->parent_slot;
403 pr_devel("free node\n");
404 kfree(node);
405 if (!parent)
406 return;
407
408
409
410 if (assoc_array_ptr_is_shortcut(parent)) {
411 shortcut = assoc_array_ptr_to_shortcut(parent);
412 BUG_ON(shortcut->next_node != cursor);
413 cursor = parent;
414 parent = shortcut->back_pointer;
415 slot = shortcut->parent_slot;
416 pr_devel("free shortcut\n");
417 kfree(shortcut);
418 if (!parent)
419 return;
420
421 BUG_ON(!assoc_array_ptr_is_node(parent));
422 }
423
424
425 pr_devel("ascend to %p[%d]\n", parent, slot);
426 cursor = parent;
427 node = assoc_array_ptr_to_node(cursor);
428 slot++;
429 goto continue_node;
430}
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445void assoc_array_destroy(struct assoc_array *array,
446 const struct assoc_array_ops *ops)
447{
448 assoc_array_destroy_subtree(array->root, ops);
449 array->root = NULL;
450}
451
452
453
454
455static bool assoc_array_insert_in_empty_tree(struct assoc_array_edit *edit)
456{
457 struct assoc_array_node *new_n0;
458
459 pr_devel("-->%s()\n", __func__);
460
461 new_n0 = kzalloc(sizeof(struct assoc_array_node), GFP_KERNEL);
462 if (!new_n0)
463 return false;
464
465 edit->new_meta[0] = assoc_array_node_to_ptr(new_n0);
466 edit->leaf_p = &new_n0->slots[0];
467 edit->adjust_count_on = new_n0;
468 edit->set[0].ptr = &edit->array->root;
469 edit->set[0].to = assoc_array_node_to_ptr(new_n0);
470
471 pr_devel("<--%s() = ok [no root]\n", __func__);
472 return true;
473}
474
475
476
477
478static bool assoc_array_insert_into_terminal_node(struct assoc_array_edit *edit,
479 const struct assoc_array_ops *ops,
480 const void *index_key,
481 struct assoc_array_walk_result *result)
482{
483 struct assoc_array_shortcut *shortcut, *new_s0;
484 struct assoc_array_node *node, *new_n0, *new_n1, *side;
485 struct assoc_array_ptr *ptr;
486 unsigned long dissimilarity, base_seg, blank;
487 size_t keylen;
488 bool have_meta;
489 int level, diff;
490 int slot, next_slot, free_slot, i, j;
491
492 node = result->terminal_node.node;
493 level = result->terminal_node.level;
494 edit->segment_cache[ASSOC_ARRAY_FAN_OUT] = result->terminal_node.slot;
495
496 pr_devel("-->%s()\n", __func__);
497
498
499
500
501
502
503 free_slot = -1;
504
505
506
507
508 for (i = 0; i < ASSOC_ARRAY_FAN_OUT; i++) {
509 ptr = node->slots[i];
510 if (!ptr) {
511 free_slot = i;
512 continue;
513 }
514 if (assoc_array_ptr_is_leaf(ptr) &&
515 ops->compare_object(assoc_array_ptr_to_leaf(ptr),
516 index_key)) {
517 pr_devel("replace in slot %d\n", i);
518 edit->leaf_p = &node->slots[i];
519 edit->dead_leaf = node->slots[i];
520 pr_devel("<--%s() = ok [replace]\n", __func__);
521 return true;
522 }
523 }
524
525
526
527
528 if (free_slot >= 0) {
529 pr_devel("insert in free slot %d\n", free_slot);
530 edit->leaf_p = &node->slots[free_slot];
531 edit->adjust_count_on = node;
532 pr_devel("<--%s() = ok [insert]\n", __func__);
533 return true;
534 }
535
536
537
538
539
540
541
542
543 new_n0 = kzalloc(sizeof(struct assoc_array_node), GFP_KERNEL);
544 if (!new_n0)
545 return false;
546 edit->new_meta[0] = assoc_array_node_to_ptr(new_n0);
547 new_n1 = kzalloc(sizeof(struct assoc_array_node), GFP_KERNEL);
548 if (!new_n1)
549 return false;
550 edit->new_meta[1] = assoc_array_node_to_ptr(new_n1);
551
552
553 pr_devel("no spare slots\n");
554 have_meta = false;
555 for (i = 0; i < ASSOC_ARRAY_FAN_OUT; i++) {
556 ptr = node->slots[i];
557 if (assoc_array_ptr_is_meta(ptr)) {
558 edit->segment_cache[i] = 0xff;
559 have_meta = true;
560 continue;
561 }
562 base_seg = ops->get_object_key_chunk(
563 assoc_array_ptr_to_leaf(ptr), level);
564 base_seg >>= level & ASSOC_ARRAY_KEY_CHUNK_MASK;
565 edit->segment_cache[i] = base_seg & ASSOC_ARRAY_FAN_MASK;
566 }
567
568 if (have_meta) {
569 pr_devel("have meta\n");
570 goto split_node;
571 }
572
573
574 dissimilarity = 0;
575 base_seg = edit->segment_cache[0];
576 for (i = 1; i < ASSOC_ARRAY_FAN_OUT; i++)
577 dissimilarity |= edit->segment_cache[i] ^ base_seg;
578
579 pr_devel("only leaves; dissimilarity=%lx\n", dissimilarity);
580
581 if ((dissimilarity & ASSOC_ARRAY_FAN_MASK) == 0) {
582
583
584
585 if ((edit->segment_cache[ASSOC_ARRAY_FAN_OUT] ^ base_seg) == 0)
586 goto all_leaves_cluster_together;
587
588
589
590
591
592
593
594
595
596 pr_devel("present leaves cluster but not new leaf\n");
597 }
598
599split_node:
600 pr_devel("split node\n");
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617 edit->set[0].to = assoc_array_node_to_ptr(new_n0);
618 new_n0->back_pointer = node->back_pointer;
619 new_n0->parent_slot = node->parent_slot;
620 new_n1->back_pointer = assoc_array_node_to_ptr(new_n0);
621 new_n1->parent_slot = -1;
622
623do_split_node:
624 pr_devel("do_split_node\n");
625
626 new_n0->nr_leaves_on_branch = node->nr_leaves_on_branch;
627 new_n1->nr_leaves_on_branch = 0;
628
629
630
631
632
633
634
635 for (i = 0; i < ASSOC_ARRAY_FAN_OUT; i++) {
636 slot = edit->segment_cache[i];
637 if (slot != 0xff)
638 for (j = i + 1; j < ASSOC_ARRAY_FAN_OUT + 1; j++)
639 if (edit->segment_cache[j] == slot)
640 goto found_slot_for_multiple_occupancy;
641 }
642found_slot_for_multiple_occupancy:
643 pr_devel("same slot: %x %x [%02x]\n", i, j, slot);
644 BUG_ON(i >= ASSOC_ARRAY_FAN_OUT);
645 BUG_ON(j >= ASSOC_ARRAY_FAN_OUT + 1);
646 BUG_ON(slot >= ASSOC_ARRAY_FAN_OUT);
647
648 new_n1->parent_slot = slot;
649
650
651 for (i = 0; i < ASSOC_ARRAY_FAN_OUT; i++)
652 if (assoc_array_ptr_is_meta(node->slots[i]))
653 new_n0->slots[i] = node->slots[i];
654 else
655 new_n0->slots[i] = NULL;
656 BUG_ON(new_n0->slots[slot] != NULL);
657 new_n0->slots[slot] = assoc_array_node_to_ptr(new_n1);
658
659
660 free_slot = -1;
661 next_slot = 0;
662 for (i = 0; i < ASSOC_ARRAY_FAN_OUT; i++) {
663 if (assoc_array_ptr_is_meta(node->slots[i]))
664 continue;
665 if (edit->segment_cache[i] == slot) {
666 new_n1->slots[next_slot++] = node->slots[i];
667 new_n1->nr_leaves_on_branch++;
668 } else {
669 do {
670 free_slot++;
671 } while (new_n0->slots[free_slot] != NULL);
672 new_n0->slots[free_slot] = node->slots[i];
673 }
674 }
675
676 pr_devel("filtered: f=%x n=%x\n", free_slot, next_slot);
677
678 if (edit->segment_cache[ASSOC_ARRAY_FAN_OUT] != slot) {
679 do {
680 free_slot++;
681 } while (new_n0->slots[free_slot] != NULL);
682 edit->leaf_p = &new_n0->slots[free_slot];
683 edit->adjust_count_on = new_n0;
684 } else {
685 edit->leaf_p = &new_n1->slots[next_slot++];
686 edit->adjust_count_on = new_n1;
687 }
688
689 BUG_ON(next_slot <= 1);
690
691 edit->set_backpointers_to = assoc_array_node_to_ptr(new_n0);
692 for (i = 0; i < ASSOC_ARRAY_FAN_OUT; i++) {
693 if (edit->segment_cache[i] == 0xff) {
694 ptr = node->slots[i];
695 BUG_ON(assoc_array_ptr_is_leaf(ptr));
696 if (assoc_array_ptr_is_node(ptr)) {
697 side = assoc_array_ptr_to_node(ptr);
698 edit->set_backpointers[i] = &side->back_pointer;
699 } else {
700 shortcut = assoc_array_ptr_to_shortcut(ptr);
701 edit->set_backpointers[i] = &shortcut->back_pointer;
702 }
703 }
704 }
705
706 ptr = node->back_pointer;
707 if (!ptr)
708 edit->set[0].ptr = &edit->array->root;
709 else if (assoc_array_ptr_is_node(ptr))
710 edit->set[0].ptr = &assoc_array_ptr_to_node(ptr)->slots[node->parent_slot];
711 else
712 edit->set[0].ptr = &assoc_array_ptr_to_shortcut(ptr)->next_node;
713 edit->excised_meta[0] = assoc_array_node_to_ptr(node);
714 pr_devel("<--%s() = ok [split node]\n", __func__);
715 return true;
716
717all_leaves_cluster_together:
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732 pr_devel("all leaves cluster together\n");
733 diff = INT_MAX;
734 for (i = 0; i < ASSOC_ARRAY_FAN_OUT; i++) {
735 int x = ops->diff_objects(assoc_array_ptr_to_leaf(node->slots[i]),
736 index_key);
737 if (x < diff) {
738 BUG_ON(x < 0);
739 diff = x;
740 }
741 }
742 BUG_ON(diff == INT_MAX);
743 BUG_ON(diff < level + ASSOC_ARRAY_LEVEL_STEP);
744
745 keylen = round_up(diff, ASSOC_ARRAY_KEY_CHUNK_SIZE);
746 keylen >>= ASSOC_ARRAY_KEY_CHUNK_SHIFT;
747
748 new_s0 = kzalloc(sizeof(struct assoc_array_shortcut) +
749 keylen * sizeof(unsigned long), GFP_KERNEL);
750 if (!new_s0)
751 return false;
752 edit->new_meta[2] = assoc_array_shortcut_to_ptr(new_s0);
753
754 edit->set[0].to = assoc_array_shortcut_to_ptr(new_s0);
755 new_s0->back_pointer = node->back_pointer;
756 new_s0->parent_slot = node->parent_slot;
757 new_s0->next_node = assoc_array_node_to_ptr(new_n0);
758 new_n0->back_pointer = assoc_array_shortcut_to_ptr(new_s0);
759 new_n0->parent_slot = 0;
760 new_n1->back_pointer = assoc_array_node_to_ptr(new_n0);
761 new_n1->parent_slot = -1;
762
763 new_s0->skip_to_level = level = diff & ~ASSOC_ARRAY_LEVEL_STEP_MASK;
764 pr_devel("skip_to_level = %d [diff %d]\n", level, diff);
765 BUG_ON(level <= 0);
766
767 for (i = 0; i < keylen; i++)
768 new_s0->index_key[i] =
769 ops->get_key_chunk(index_key, i * ASSOC_ARRAY_KEY_CHUNK_SIZE);
770
771 blank = ULONG_MAX << (level & ASSOC_ARRAY_KEY_CHUNK_MASK);
772 pr_devel("blank off [%zu] %d: %lx\n", keylen - 1, level, blank);
773 new_s0->index_key[keylen - 1] &= ~blank;
774
775
776
777
778 for (i = 0; i < ASSOC_ARRAY_FAN_OUT; i++) {
779 ptr = node->slots[i];
780 base_seg = ops->get_object_key_chunk(assoc_array_ptr_to_leaf(ptr),
781 level);
782 base_seg >>= level & ASSOC_ARRAY_KEY_CHUNK_MASK;
783 edit->segment_cache[i] = base_seg & ASSOC_ARRAY_FAN_MASK;
784 }
785
786 base_seg = ops->get_key_chunk(index_key, level);
787 base_seg >>= level & ASSOC_ARRAY_KEY_CHUNK_MASK;
788 edit->segment_cache[ASSOC_ARRAY_FAN_OUT] = base_seg & ASSOC_ARRAY_FAN_MASK;
789 goto do_split_node;
790}
791
792
793
794
795static bool assoc_array_insert_mid_shortcut(struct assoc_array_edit *edit,
796 const struct assoc_array_ops *ops,
797 struct assoc_array_walk_result *result)
798{
799 struct assoc_array_shortcut *shortcut, *new_s0, *new_s1;
800 struct assoc_array_node *node, *new_n0, *side;
801 unsigned long sc_segments, dissimilarity, blank;
802 size_t keylen;
803 int level, sc_level, diff;
804 int sc_slot;
805
806 shortcut = result->wrong_shortcut.shortcut;
807 level = result->wrong_shortcut.level;
808 sc_level = result->wrong_shortcut.sc_level;
809 sc_segments = result->wrong_shortcut.sc_segments;
810 dissimilarity = result->wrong_shortcut.dissimilarity;
811
812 pr_devel("-->%s(ix=%d dis=%lx scix=%d)\n",
813 __func__, level, dissimilarity, sc_level);
814
815
816
817
818
819
820
821 diff = __ffs(dissimilarity);
822 diff &= ~ASSOC_ARRAY_LEVEL_STEP_MASK;
823 diff += sc_level & ~ASSOC_ARRAY_KEY_CHUNK_MASK;
824 pr_devel("diff=%d\n", diff);
825
826 if (!shortcut->back_pointer) {
827 edit->set[0].ptr = &edit->array->root;
828 } else if (assoc_array_ptr_is_node(shortcut->back_pointer)) {
829 node = assoc_array_ptr_to_node(shortcut->back_pointer);
830 edit->set[0].ptr = &node->slots[shortcut->parent_slot];
831 } else {
832 BUG();
833 }
834
835 edit->excised_meta[0] = assoc_array_shortcut_to_ptr(shortcut);
836
837
838 new_n0 = kzalloc(sizeof(struct assoc_array_node), GFP_KERNEL);
839 if (!new_n0)
840 return false;
841 edit->new_meta[0] = assoc_array_node_to_ptr(new_n0);
842 edit->adjust_count_on = new_n0;
843
844
845
846
847
848 level += ASSOC_ARRAY_LEVEL_STEP;
849 if (diff > level) {
850 pr_devel("pre-shortcut %d...%d\n", level, diff);
851 keylen = round_up(diff, ASSOC_ARRAY_KEY_CHUNK_SIZE);
852 keylen >>= ASSOC_ARRAY_KEY_CHUNK_SHIFT;
853
854 new_s0 = kzalloc(sizeof(struct assoc_array_shortcut) +
855 keylen * sizeof(unsigned long), GFP_KERNEL);
856 if (!new_s0)
857 return false;
858 edit->new_meta[1] = assoc_array_shortcut_to_ptr(new_s0);
859 edit->set[0].to = assoc_array_shortcut_to_ptr(new_s0);
860 new_s0->back_pointer = shortcut->back_pointer;
861 new_s0->parent_slot = shortcut->parent_slot;
862 new_s0->next_node = assoc_array_node_to_ptr(new_n0);
863 new_s0->skip_to_level = diff;
864
865 new_n0->back_pointer = assoc_array_shortcut_to_ptr(new_s0);
866 new_n0->parent_slot = 0;
867
868 memcpy(new_s0->index_key, shortcut->index_key,
869 keylen * sizeof(unsigned long));
870
871 blank = ULONG_MAX << (diff & ASSOC_ARRAY_KEY_CHUNK_MASK);
872 pr_devel("blank off [%zu] %d: %lx\n", keylen - 1, diff, blank);
873 new_s0->index_key[keylen - 1] &= ~blank;
874 } else {
875 pr_devel("no pre-shortcut\n");
876 edit->set[0].to = assoc_array_node_to_ptr(new_n0);
877 new_n0->back_pointer = shortcut->back_pointer;
878 new_n0->parent_slot = shortcut->parent_slot;
879 }
880
881 side = assoc_array_ptr_to_node(shortcut->next_node);
882 new_n0->nr_leaves_on_branch = side->nr_leaves_on_branch;
883
884
885
886
887 sc_slot = sc_segments >> (diff & ASSOC_ARRAY_KEY_CHUNK_MASK);
888 sc_slot &= ASSOC_ARRAY_FAN_MASK;
889
890 pr_devel("new slot %lx >> %d -> %d\n",
891 sc_segments, diff & ASSOC_ARRAY_KEY_CHUNK_MASK, sc_slot);
892
893
894
895
896
897
898 level = diff + ASSOC_ARRAY_LEVEL_STEP;
899 if (level < shortcut->skip_to_level) {
900 pr_devel("post-shortcut %d...%d\n", level, shortcut->skip_to_level);
901 keylen = round_up(shortcut->skip_to_level, ASSOC_ARRAY_KEY_CHUNK_SIZE);
902 keylen >>= ASSOC_ARRAY_KEY_CHUNK_SHIFT;
903
904 new_s1 = kzalloc(sizeof(struct assoc_array_shortcut) +
905 keylen * sizeof(unsigned long), GFP_KERNEL);
906 if (!new_s1)
907 return false;
908 edit->new_meta[2] = assoc_array_shortcut_to_ptr(new_s1);
909
910 new_s1->back_pointer = assoc_array_node_to_ptr(new_n0);
911 new_s1->parent_slot = sc_slot;
912 new_s1->next_node = shortcut->next_node;
913 new_s1->skip_to_level = shortcut->skip_to_level;
914
915 new_n0->slots[sc_slot] = assoc_array_shortcut_to_ptr(new_s1);
916
917 memcpy(new_s1->index_key, shortcut->index_key,
918 keylen * sizeof(unsigned long));
919
920 edit->set[1].ptr = &side->back_pointer;
921 edit->set[1].to = assoc_array_shortcut_to_ptr(new_s1);
922 } else {
923 pr_devel("no post-shortcut\n");
924
925
926
927
928
929
930 new_n0->slots[sc_slot] = shortcut->next_node;
931 edit->set_parent_slot[0].p = &side->parent_slot;
932 edit->set_parent_slot[0].to = sc_slot;
933 edit->set[1].ptr = &side->back_pointer;
934 edit->set[1].to = assoc_array_node_to_ptr(new_n0);
935 }
936
937
938 if (sc_slot == 0)
939 edit->leaf_p = &new_n0->slots[1];
940 else
941 edit->leaf_p = &new_n0->slots[0];
942
943 pr_devel("<--%s() = ok [split shortcut]\n", __func__);
944 return edit;
945}
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966struct assoc_array_edit *assoc_array_insert(struct assoc_array *array,
967 const struct assoc_array_ops *ops,
968 const void *index_key,
969 void *object)
970{
971 struct assoc_array_walk_result result;
972 struct assoc_array_edit *edit;
973
974 pr_devel("-->%s()\n", __func__);
975
976
977
978
979
980
981 BUG_ON(assoc_array_ptr_is_meta(object));
982
983 edit = kzalloc(sizeof(struct assoc_array_edit), GFP_KERNEL);
984 if (!edit)
985 return ERR_PTR(-ENOMEM);
986 edit->array = array;
987 edit->ops = ops;
988 edit->leaf = assoc_array_leaf_to_ptr(object);
989 edit->adjust_count_by = 1;
990
991 switch (assoc_array_walk(array, ops, index_key, &result)) {
992 case assoc_array_walk_tree_empty:
993
994 if (!assoc_array_insert_in_empty_tree(edit))
995 goto enomem;
996 return edit;
997
998 case assoc_array_walk_found_terminal_node:
999
1000
1001
1002
1003 if (!assoc_array_insert_into_terminal_node(edit, ops, index_key,
1004 &result))
1005 goto enomem;
1006 return edit;
1007
1008 case assoc_array_walk_found_wrong_shortcut:
1009
1010
1011
1012 if (!assoc_array_insert_mid_shortcut(edit, ops, &result))
1013 goto enomem;
1014 return edit;
1015 }
1016
1017enomem:
1018
1019 pr_devel("enomem\n");
1020 assoc_array_cancel_edit(edit);
1021 return ERR_PTR(-ENOMEM);
1022}
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033void assoc_array_insert_set_object(struct assoc_array_edit *edit, void *object)
1034{
1035 BUG_ON(!object);
1036 edit->leaf = assoc_array_leaf_to_ptr(object);
1037}
1038
1039struct assoc_array_delete_collapse_context {
1040 struct assoc_array_node *node;
1041 const void *skip_leaf;
1042 int slot;
1043};
1044
1045
1046
1047
1048static int assoc_array_delete_collapse_iterator(const void *leaf,
1049 void *iterator_data)
1050{
1051 struct assoc_array_delete_collapse_context *collapse = iterator_data;
1052
1053 if (leaf == collapse->skip_leaf)
1054 return 0;
1055
1056 BUG_ON(collapse->slot >= ASSOC_ARRAY_FAN_OUT);
1057
1058 collapse->node->slots[collapse->slot++] = assoc_array_leaf_to_ptr(leaf);
1059 return 0;
1060}
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081struct assoc_array_edit *assoc_array_delete(struct assoc_array *array,
1082 const struct assoc_array_ops *ops,
1083 const void *index_key)
1084{
1085 struct assoc_array_delete_collapse_context collapse;
1086 struct assoc_array_walk_result result;
1087 struct assoc_array_node *node, *new_n0;
1088 struct assoc_array_edit *edit;
1089 struct assoc_array_ptr *ptr;
1090 bool has_meta;
1091 int slot, i;
1092
1093 pr_devel("-->%s()\n", __func__);
1094
1095 edit = kzalloc(sizeof(struct assoc_array_edit), GFP_KERNEL);
1096 if (!edit)
1097 return ERR_PTR(-ENOMEM);
1098 edit->array = array;
1099 edit->ops = ops;
1100 edit->adjust_count_by = -1;
1101
1102 switch (assoc_array_walk(array, ops, index_key, &result)) {
1103 case assoc_array_walk_found_terminal_node:
1104
1105
1106
1107 pr_devel("terminal_node\n");
1108 node = result.terminal_node.node;
1109
1110 for (slot = 0; slot < ASSOC_ARRAY_FAN_OUT; slot++) {
1111 ptr = node->slots[slot];
1112 if (ptr &&
1113 assoc_array_ptr_is_leaf(ptr) &&
1114 ops->compare_object(assoc_array_ptr_to_leaf(ptr),
1115 index_key))
1116 goto found_leaf;
1117 }
1118 case assoc_array_walk_tree_empty:
1119 case assoc_array_walk_found_wrong_shortcut:
1120 default:
1121 assoc_array_cancel_edit(edit);
1122 pr_devel("not found\n");
1123 return NULL;
1124 }
1125
1126found_leaf:
1127 BUG_ON(array->nr_leaves_on_tree <= 0);
1128
1129
1130
1131
1132 edit->dead_leaf = node->slots[slot];
1133 edit->set[0].ptr = &node->slots[slot];
1134 edit->set[0].to = NULL;
1135 edit->adjust_count_on = node;
1136
1137
1138
1139
1140 if (array->nr_leaves_on_tree == 1) {
1141 edit->set[1].ptr = &array->root;
1142 edit->set[1].to = NULL;
1143 edit->adjust_count_on = NULL;
1144 edit->excised_subtree = array->root;
1145 pr_devel("all gone\n");
1146 return edit;
1147 }
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159 if (node->nr_leaves_on_branch <= ASSOC_ARRAY_FAN_OUT + 1) {
1160 struct assoc_array_node *parent, *grandparent;
1161 struct assoc_array_ptr *ptr;
1162
1163
1164
1165
1166
1167 has_meta = false;
1168 for (i = 0; i < ASSOC_ARRAY_FAN_OUT; i++) {
1169 ptr = node->slots[i];
1170 if (assoc_array_ptr_is_meta(ptr)) {
1171 has_meta = true;
1172 break;
1173 }
1174 }
1175
1176 pr_devel("leaves: %ld [m=%d]\n",
1177 node->nr_leaves_on_branch - 1, has_meta);
1178
1179
1180
1181
1182 parent = node;
1183 collapse_up:
1184 pr_devel("collapse subtree: %ld\n", parent->nr_leaves_on_branch);
1185
1186 ptr = parent->back_pointer;
1187 if (!ptr)
1188 goto do_collapse;
1189 if (assoc_array_ptr_is_shortcut(ptr)) {
1190 struct assoc_array_shortcut *s = assoc_array_ptr_to_shortcut(ptr);
1191 ptr = s->back_pointer;
1192 if (!ptr)
1193 goto do_collapse;
1194 }
1195
1196 grandparent = assoc_array_ptr_to_node(ptr);
1197 if (grandparent->nr_leaves_on_branch <= ASSOC_ARRAY_FAN_OUT + 1) {
1198 parent = grandparent;
1199 goto collapse_up;
1200 }
1201
1202 do_collapse:
1203
1204
1205
1206
1207 if (has_meta || parent != node) {
1208 node = parent;
1209
1210
1211 new_n0 = kzalloc(sizeof(struct assoc_array_node), GFP_KERNEL);
1212 if (!new_n0)
1213 goto enomem;
1214 edit->new_meta[0] = assoc_array_node_to_ptr(new_n0);
1215
1216 new_n0->back_pointer = node->back_pointer;
1217 new_n0->parent_slot = node->parent_slot;
1218 new_n0->nr_leaves_on_branch = node->nr_leaves_on_branch;
1219 edit->adjust_count_on = new_n0;
1220
1221 collapse.node = new_n0;
1222 collapse.skip_leaf = assoc_array_ptr_to_leaf(edit->dead_leaf);
1223 collapse.slot = 0;
1224 assoc_array_subtree_iterate(assoc_array_node_to_ptr(node),
1225 node->back_pointer,
1226 assoc_array_delete_collapse_iterator,
1227 &collapse);
1228 pr_devel("collapsed %d,%lu\n", collapse.slot, new_n0->nr_leaves_on_branch);
1229 BUG_ON(collapse.slot != new_n0->nr_leaves_on_branch - 1);
1230
1231 if (!node->back_pointer) {
1232 edit->set[1].ptr = &array->root;
1233 } else if (assoc_array_ptr_is_leaf(node->back_pointer)) {
1234 BUG();
1235 } else if (assoc_array_ptr_is_node(node->back_pointer)) {
1236 struct assoc_array_node *p =
1237 assoc_array_ptr_to_node(node->back_pointer);
1238 edit->set[1].ptr = &p->slots[node->parent_slot];
1239 } else if (assoc_array_ptr_is_shortcut(node->back_pointer)) {
1240 struct assoc_array_shortcut *s =
1241 assoc_array_ptr_to_shortcut(node->back_pointer);
1242 edit->set[1].ptr = &s->next_node;
1243 }
1244 edit->set[1].to = assoc_array_node_to_ptr(new_n0);
1245 edit->excised_subtree = assoc_array_node_to_ptr(node);
1246 }
1247 }
1248
1249 return edit;
1250
1251enomem:
1252
1253 pr_devel("enomem\n");
1254 assoc_array_cancel_edit(edit);
1255 return ERR_PTR(-ENOMEM);
1256}
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276struct assoc_array_edit *assoc_array_clear(struct assoc_array *array,
1277 const struct assoc_array_ops *ops)
1278{
1279 struct assoc_array_edit *edit;
1280
1281 pr_devel("-->%s()\n", __func__);
1282
1283 if (!array->root)
1284 return NULL;
1285
1286 edit = kzalloc(sizeof(struct assoc_array_edit), GFP_KERNEL);
1287 if (!edit)
1288 return ERR_PTR(-ENOMEM);
1289 edit->array = array;
1290 edit->ops = ops;
1291 edit->set[1].ptr = &array->root;
1292 edit->set[1].to = NULL;
1293 edit->excised_subtree = array->root;
1294 edit->ops_for_excised_subtree = ops;
1295 pr_devel("all gone\n");
1296 return edit;
1297}
1298
1299
1300
1301
1302static void assoc_array_rcu_cleanup(struct rcu_head *head)
1303{
1304 struct assoc_array_edit *edit =
1305 container_of(head, struct assoc_array_edit, rcu);
1306 int i;
1307
1308 pr_devel("-->%s()\n", __func__);
1309
1310 if (edit->dead_leaf)
1311 edit->ops->free_object(assoc_array_ptr_to_leaf(edit->dead_leaf));
1312 for (i = 0; i < ARRAY_SIZE(edit->excised_meta); i++)
1313 if (edit->excised_meta[i])
1314 kfree(assoc_array_ptr_to_node(edit->excised_meta[i]));
1315
1316 if (edit->excised_subtree) {
1317 BUG_ON(assoc_array_ptr_is_leaf(edit->excised_subtree));
1318 if (assoc_array_ptr_is_node(edit->excised_subtree)) {
1319 struct assoc_array_node *n =
1320 assoc_array_ptr_to_node(edit->excised_subtree);
1321 n->back_pointer = NULL;
1322 } else {
1323 struct assoc_array_shortcut *s =
1324 assoc_array_ptr_to_shortcut(edit->excised_subtree);
1325 s->back_pointer = NULL;
1326 }
1327 assoc_array_destroy_subtree(edit->excised_subtree,
1328 edit->ops_for_excised_subtree);
1329 }
1330
1331 kfree(edit);
1332}
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347void assoc_array_apply_edit(struct assoc_array_edit *edit)
1348{
1349 struct assoc_array_shortcut *shortcut;
1350 struct assoc_array_node *node;
1351 struct assoc_array_ptr *ptr;
1352 int i;
1353
1354 pr_devel("-->%s()\n", __func__);
1355
1356 smp_wmb();
1357 if (edit->leaf_p)
1358 *edit->leaf_p = edit->leaf;
1359
1360 smp_wmb();
1361 for (i = 0; i < ARRAY_SIZE(edit->set_parent_slot); i++)
1362 if (edit->set_parent_slot[i].p)
1363 *edit->set_parent_slot[i].p = edit->set_parent_slot[i].to;
1364
1365 smp_wmb();
1366 for (i = 0; i < ARRAY_SIZE(edit->set_backpointers); i++)
1367 if (edit->set_backpointers[i])
1368 *edit->set_backpointers[i] = edit->set_backpointers_to;
1369
1370 smp_wmb();
1371 for (i = 0; i < ARRAY_SIZE(edit->set); i++)
1372 if (edit->set[i].ptr)
1373 *edit->set[i].ptr = edit->set[i].to;
1374
1375 if (edit->array->root == NULL) {
1376 edit->array->nr_leaves_on_tree = 0;
1377 } else if (edit->adjust_count_on) {
1378 node = edit->adjust_count_on;
1379 for (;;) {
1380 node->nr_leaves_on_branch += edit->adjust_count_by;
1381
1382 ptr = node->back_pointer;
1383 if (!ptr)
1384 break;
1385 if (assoc_array_ptr_is_shortcut(ptr)) {
1386 shortcut = assoc_array_ptr_to_shortcut(ptr);
1387 ptr = shortcut->back_pointer;
1388 if (!ptr)
1389 break;
1390 }
1391 BUG_ON(!assoc_array_ptr_is_node(ptr));
1392 node = assoc_array_ptr_to_node(ptr);
1393 }
1394
1395 edit->array->nr_leaves_on_tree += edit->adjust_count_by;
1396 }
1397
1398 call_rcu(&edit->rcu, assoc_array_rcu_cleanup);
1399}
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411void assoc_array_cancel_edit(struct assoc_array_edit *edit)
1412{
1413 struct assoc_array_ptr *ptr;
1414 int i;
1415
1416 pr_devel("-->%s()\n", __func__);
1417
1418
1419 for (i = 0; i < ARRAY_SIZE(edit->new_meta); i++) {
1420 ptr = edit->new_meta[i];
1421 if (ptr) {
1422 if (assoc_array_ptr_is_node(ptr))
1423 kfree(assoc_array_ptr_to_node(ptr));
1424 else
1425 kfree(assoc_array_ptr_to_shortcut(ptr));
1426 }
1427 }
1428 kfree(edit);
1429}
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455int assoc_array_gc(struct assoc_array *array,
1456 const struct assoc_array_ops *ops,
1457 bool (*iterator)(void *object, void *iterator_data),
1458 void *iterator_data)
1459{
1460 struct assoc_array_shortcut *shortcut, *new_s;
1461 struct assoc_array_node *node, *new_n;
1462 struct assoc_array_edit *edit;
1463 struct assoc_array_ptr *cursor, *ptr;
1464 struct assoc_array_ptr *new_root, *new_parent, **new_ptr_pp;
1465 unsigned long nr_leaves_on_tree;
1466 int keylen, slot, nr_free, next_slot, i;
1467
1468 pr_devel("-->%s()\n", __func__);
1469
1470 if (!array->root)
1471 return 0;
1472
1473 edit = kzalloc(sizeof(struct assoc_array_edit), GFP_KERNEL);
1474 if (!edit)
1475 return -ENOMEM;
1476 edit->array = array;
1477 edit->ops = ops;
1478 edit->ops_for_excised_subtree = ops;
1479 edit->set[0].ptr = &array->root;
1480 edit->excised_subtree = array->root;
1481
1482 new_root = new_parent = NULL;
1483 new_ptr_pp = &new_root;
1484 cursor = array->root;
1485
1486descend:
1487
1488
1489
1490 if (assoc_array_ptr_is_shortcut(cursor)) {
1491 shortcut = assoc_array_ptr_to_shortcut(cursor);
1492 keylen = round_up(shortcut->skip_to_level, ASSOC_ARRAY_KEY_CHUNK_SIZE);
1493 keylen >>= ASSOC_ARRAY_KEY_CHUNK_SHIFT;
1494 new_s = kmalloc(sizeof(struct assoc_array_shortcut) +
1495 keylen * sizeof(unsigned long), GFP_KERNEL);
1496 if (!new_s)
1497 goto enomem;
1498 pr_devel("dup shortcut %p -> %p\n", shortcut, new_s);
1499 memcpy(new_s, shortcut, (sizeof(struct assoc_array_shortcut) +
1500 keylen * sizeof(unsigned long)));
1501 new_s->back_pointer = new_parent;
1502 new_s->parent_slot = shortcut->parent_slot;
1503 *new_ptr_pp = new_parent = assoc_array_shortcut_to_ptr(new_s);
1504 new_ptr_pp = &new_s->next_node;
1505 cursor = shortcut->next_node;
1506 }
1507
1508
1509 node = assoc_array_ptr_to_node(cursor);
1510 new_n = kzalloc(sizeof(struct assoc_array_node), GFP_KERNEL);
1511 if (!new_n)
1512 goto enomem;
1513 pr_devel("dup node %p -> %p\n", node, new_n);
1514 new_n->back_pointer = new_parent;
1515 new_n->parent_slot = node->parent_slot;
1516 *new_ptr_pp = new_parent = assoc_array_node_to_ptr(new_n);
1517 new_ptr_pp = NULL;
1518 slot = 0;
1519
1520continue_node:
1521
1522 for (; slot < ASSOC_ARRAY_FAN_OUT; slot++) {
1523 ptr = node->slots[slot];
1524 if (!ptr)
1525 continue;
1526
1527 if (assoc_array_ptr_is_leaf(ptr)) {
1528 if (iterator(assoc_array_ptr_to_leaf(ptr),
1529 iterator_data))
1530
1531
1532
1533 new_n->slots[slot] = ptr;
1534 continue;
1535 }
1536
1537 new_ptr_pp = &new_n->slots[slot];
1538 cursor = ptr;
1539 goto descend;
1540 }
1541
1542 pr_devel("-- compress node %p --\n", new_n);
1543
1544
1545
1546
1547 new_n->nr_leaves_on_branch = 0;
1548 nr_free = 0;
1549 for (slot = 0; slot < ASSOC_ARRAY_FAN_OUT; slot++) {
1550 ptr = new_n->slots[slot];
1551 if (!ptr)
1552 nr_free++;
1553 else if (assoc_array_ptr_is_leaf(ptr))
1554 new_n->nr_leaves_on_branch++;
1555 }
1556 pr_devel("free=%d, leaves=%lu\n", nr_free, new_n->nr_leaves_on_branch);
1557
1558
1559 next_slot = 0;
1560 for (slot = 0; slot < ASSOC_ARRAY_FAN_OUT; slot++) {
1561 struct assoc_array_shortcut *s;
1562 struct assoc_array_node *child;
1563
1564 ptr = new_n->slots[slot];
1565 if (!ptr || assoc_array_ptr_is_leaf(ptr))
1566 continue;
1567
1568 s = NULL;
1569 if (assoc_array_ptr_is_shortcut(ptr)) {
1570 s = assoc_array_ptr_to_shortcut(ptr);
1571 ptr = s->next_node;
1572 }
1573
1574 child = assoc_array_ptr_to_node(ptr);
1575 new_n->nr_leaves_on_branch += child->nr_leaves_on_branch;
1576
1577 if (child->nr_leaves_on_branch <= nr_free + 1) {
1578
1579 pr_devel("[%d] fold node %lu/%d [nx %d]\n",
1580 slot, child->nr_leaves_on_branch, nr_free + 1,
1581 next_slot);
1582
1583
1584
1585
1586 BUG_ON(s);
1587
1588 new_n->slots[slot] = NULL;
1589 nr_free++;
1590 if (slot < next_slot)
1591 next_slot = slot;
1592 for (i = 0; i < ASSOC_ARRAY_FAN_OUT; i++) {
1593 struct assoc_array_ptr *p = child->slots[i];
1594 if (!p)
1595 continue;
1596 BUG_ON(assoc_array_ptr_is_meta(p));
1597 while (new_n->slots[next_slot])
1598 next_slot++;
1599 BUG_ON(next_slot >= ASSOC_ARRAY_FAN_OUT);
1600 new_n->slots[next_slot++] = p;
1601 nr_free--;
1602 }
1603 kfree(child);
1604 } else {
1605 pr_devel("[%d] retain node %lu/%d [nx %d]\n",
1606 slot, child->nr_leaves_on_branch, nr_free + 1,
1607 next_slot);
1608 }
1609 }
1610
1611 pr_devel("after: %lu\n", new_n->nr_leaves_on_branch);
1612
1613 nr_leaves_on_tree = new_n->nr_leaves_on_branch;
1614
1615
1616 if (nr_free == ASSOC_ARRAY_FAN_OUT - 1) {
1617 for (slot = 0; slot < ASSOC_ARRAY_FAN_OUT; slot++)
1618 if ((ptr = new_n->slots[slot]))
1619 break;
1620
1621 if (assoc_array_ptr_is_meta(ptr) &&
1622 assoc_array_ptr_is_shortcut(ptr)) {
1623 pr_devel("excise node %p with 1 shortcut\n", new_n);
1624 new_s = assoc_array_ptr_to_shortcut(ptr);
1625 new_parent = new_n->back_pointer;
1626 slot = new_n->parent_slot;
1627 kfree(new_n);
1628 if (!new_parent) {
1629 new_s->back_pointer = NULL;
1630 new_s->parent_slot = 0;
1631 new_root = ptr;
1632 goto gc_complete;
1633 }
1634
1635 if (assoc_array_ptr_is_shortcut(new_parent)) {
1636
1637 struct assoc_array_shortcut *s =
1638 assoc_array_ptr_to_shortcut(new_parent);
1639
1640 pr_devel("excise preceding shortcut\n");
1641
1642 new_parent = new_s->back_pointer = s->back_pointer;
1643 slot = new_s->parent_slot = s->parent_slot;
1644 kfree(s);
1645 if (!new_parent) {
1646 new_s->back_pointer = NULL;
1647 new_s->parent_slot = 0;
1648 new_root = ptr;
1649 goto gc_complete;
1650 }
1651 }
1652
1653 new_s->back_pointer = new_parent;
1654 new_s->parent_slot = slot;
1655 new_n = assoc_array_ptr_to_node(new_parent);
1656 new_n->slots[slot] = ptr;
1657 goto ascend_old_tree;
1658 }
1659 }
1660
1661
1662
1663
1664 ptr = new_n->back_pointer;
1665 if (!ptr)
1666 goto gc_complete;
1667
1668 if (assoc_array_ptr_is_shortcut(ptr)) {
1669 new_s = assoc_array_ptr_to_shortcut(ptr);
1670 new_parent = new_s->back_pointer;
1671 slot = new_s->parent_slot;
1672
1673 if (new_n->nr_leaves_on_branch <= ASSOC_ARRAY_FAN_OUT) {
1674 struct assoc_array_node *n;
1675
1676 pr_devel("excise shortcut\n");
1677 new_n->back_pointer = new_parent;
1678 new_n->parent_slot = slot;
1679 kfree(new_s);
1680 if (!new_parent) {
1681 new_root = assoc_array_node_to_ptr(new_n);
1682 goto gc_complete;
1683 }
1684
1685 n = assoc_array_ptr_to_node(new_parent);
1686 n->slots[slot] = assoc_array_node_to_ptr(new_n);
1687 }
1688 } else {
1689 new_parent = ptr;
1690 }
1691 new_n = assoc_array_ptr_to_node(new_parent);
1692
1693ascend_old_tree:
1694 ptr = node->back_pointer;
1695 if (assoc_array_ptr_is_shortcut(ptr)) {
1696 shortcut = assoc_array_ptr_to_shortcut(ptr);
1697 slot = shortcut->parent_slot;
1698 cursor = shortcut->back_pointer;
1699 if (!cursor)
1700 goto gc_complete;
1701 } else {
1702 slot = node->parent_slot;
1703 cursor = ptr;
1704 }
1705 BUG_ON(!cursor);
1706 node = assoc_array_ptr_to_node(cursor);
1707 slot++;
1708 goto continue_node;
1709
1710gc_complete:
1711 edit->set[0].to = new_root;
1712 assoc_array_apply_edit(edit);
1713 array->nr_leaves_on_tree = nr_leaves_on_tree;
1714 return 0;
1715
1716enomem:
1717 pr_devel("enomem\n");
1718 assoc_array_destroy_subtree(new_root, edit->ops);
1719 kfree(edit);
1720 return -ENOMEM;
1721}
1722