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