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