+ /* Not enough space in node, need to recompact. */
+ ret = ja_node_recompact_add(ja, type_index, type, node,
+ shadow_node, node_flag, n, child_node_flag);
+ }
+ return ret;
+}
+
+struct cds_hlist_head *cds_ja_lookup(struct cds_ja *ja, uint64_t key)
+{
+ unsigned int tree_depth, i;
+ struct cds_ja_inode_flag *node_flag;
+
+ if (caa_unlikely(key > ja->key_max))
+ return NULL;
+ tree_depth = ja->tree_depth;
+ node_flag = rcu_dereference(ja->root);
+
+ /* level 0: root node */
+ if (!ja_node_ptr(node_flag))
+ return NULL;
+
+ for (i = 1; i < tree_depth; i++) {
+ node_flag = ja_node_get_nth(node_flag, NULL,
+ (unsigned char) key);
+ if (!ja_node_ptr(node_flag))
+ return NULL;
+ key >>= JA_BITS_PER_BYTE;
+ }
+
+ /* Last level lookup succeded. We got an actual match. */
+ return (struct cds_hlist_head *) node_flag;
+}
+
+/*
+ * We reached an unpopulated node. Create it and the children we need,
+ * and then attach the entire branch to the current node. This may
+ * trigger recompaction of the current node. Locks needed: node lock
+ * (for add), and, possibly, parent node lock (to update pointer due to
+ * node recompaction).
+ *
+ * First take node lock, check if recompaction is needed, then take
+ * parent lock (if needed). Then we can proceed to create the new
+ * branch. Publish the new branch, and release locks.
+ * TODO: we currently always take the parent lock even when not needed.
+ */
+static
+int ja_attach_node(struct cds_ja *ja,
+ struct cds_ja_inode_flag **node_flag_ptr,
+ struct cds_ja_inode_flag *node_flag,
+ struct cds_ja_inode_flag *parent_node_flag,
+ uint64_t key,
+ unsigned int depth,
+ struct cds_ja_node *child_node)
+{
+ struct cds_ja_shadow_node *shadow_node = NULL,
+ *parent_shadow_node = NULL;
+ struct cds_ja_inode *node = ja_node_ptr(node_flag);
+ struct cds_ja_inode *parent_node = ja_node_ptr(parent_node_flag);
+ struct cds_hlist_head head;
+ struct cds_ja_inode_flag *iter_node_flag, *iter_dest_node_flag;
+ int ret, i;
+ struct cds_ja_inode_flag *created_nodes[JA_MAX_INTERNAL_DEPTH];
+ int nr_created_nodes = 0;
+
+ assert(node);
+ shadow_node = rcuja_shadow_lookup_lock(ja->ht, node);
+ if (!shadow_node) {
+ ret = -ENOENT;
+ goto end;
+ }
+ if (parent_node) {
+ parent_shadow_node = rcuja_shadow_lookup_lock(ja->ht,
+ parent_node);
+ if (!parent_shadow_node) {
+ ret = -ENOENT;
+ goto unlock_shadow;
+ }
+ }
+
+ CDS_INIT_HLIST_HEAD(&head);
+ cds_hlist_add_head_rcu(&child_node->list, &head);
+
+ iter_dest_node_flag = NULL;
+ ret = ja_node_set_nth(ja, &iter_dest_node_flag,
+ key >> (JA_BITS_PER_BYTE * (ja->tree_depth - 2)),
+ (struct cds_ja_inode_flag *) head.next,
+ NULL);
+ if (ret)
+ goto unlock_parent;
+ created_nodes[nr_created_nodes++] = iter_dest_node_flag;
+ iter_node_flag = iter_dest_node_flag;
+
+ /* Create new branch, starting from bottom */
+ for (i = ja->tree_depth - 2; i >= (int) depth; i--) {
+ iter_dest_node_flag = NULL;
+ ret = ja_node_set_nth(ja, &iter_dest_node_flag,
+ key >> (JA_BITS_PER_BYTE * (i - 1)),
+ iter_node_flag,
+ NULL);
+ if (ret)
+ goto check_error;
+ created_nodes[nr_created_nodes++] = iter_dest_node_flag;
+ iter_node_flag = iter_dest_node_flag;
+ }
+
+ /* Publish new branch */
+ rcu_assign_pointer(*node_flag_ptr, iter_node_flag);
+
+ /* Success */
+ ret = 0;
+
+check_error:
+ if (ret) {
+ for (i = 0; i < nr_created_nodes; i++) {
+ int tmpret;
+ tmpret = rcuja_shadow_clear(ja->ht,
+ ja_node_ptr(created_nodes[i]),
+ RCUJA_SHADOW_CLEAR_FREE_NODE
+ | RCUJA_SHADOW_CLEAR_FREE_LOCK);
+ assert(!tmpret);
+ }