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