SSDFS: flash-friendly LFS file system for ZNS SSD
RFC PATCH 48/76 ssdfs: add/delete b-tree node
In reply to Viacheslav DubeykoB-tree can be imagined like a hierarchy of b-tree nodes. This patch implements the logic of adding, inserting, or deletion the node to/from b-tree. The logic is reused by all specialized b-trees implementation.
--- fs/ssdfs/btree.c | 3054 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 3054 insertions(+)
fs/ssdfs/btree.c +3054 -0
@@ -1018,3 +1018,3057 @@ int ssdfs_btree_flush(struct ssdfs_btree *tree)return err; } + +/* +* ssdfs_btree_destroy_node_range() - destroy nodes from radix tree +* @tree: btree object +* @hash: starting hash for nodes destruction +* +* This method tries to flush and destroy +* some nodes from radix tree +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +*/ +int ssdfs_btree_destroy_node_range (struct ssdfs_btree *tree, +u64 hash) +{ +int tree_state; +struct radix_tree_iter iter; +void **slot; +struct ssdfs_btree_node *node; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree); +#endif /* CONFIG_SSDFS_DEBUG */ + + tree_state =atomic_read (&tree->state); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("tree %p, type %#x, state %#x\n" , + tree, tree->type, tree_state); +#endif /* CONFIG_SSDFS_DEBUG */ + +switch (tree_state) { +case SSDFS_BTREE_CREATED : +case SSDFS_BTREE_DIRTY : +/* expected state */ +break ; + +default : +SSDFS_WARN ("invalid tree state %#x\n" , + tree_state); +return -ERANGE ; + } + +down_write (&tree->lock); + +rcu_read_lock (); + +spin_lock (&tree->nodes_lock); +radix_tree_for_each_slot (slot, &tree->nodes, &iter, +SSDFS_BTREE_ROOT_NODE_ID ) { + + node = (struct ssdfs_btree_node *)radix_tree_deref_slot (slot); +if (unlikely (!node)) { +SSDFS_WARN ("empty node ptr: node_id %llu\n" , + (u64 )iter.index); +continue ; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node_id %u\n" , node->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (is_ssdfs_btree_node_pre_deleted (node)) { +if (is_ssdfs_node_shared (node)) { +atomic_set (&node->state, +SSDFS_BTREE_NODE_INVALID ); +continue ; + } + } + +spin_unlock (&tree->nodes_lock); +rcu_read_unlock (); + +if (is_ssdfs_btree_node_pre_deleted (node)) { +clear_ssdfs_btree_node_pre_deleted (node); + +ssdfs_btree_radix_tree_delete (tree, + node->node_id); + +if (tree->btree_ops && + tree->btree_ops->delete_node) { + err = tree->btree_ops->delete_node (node); +if (unlikely (err)) { +SSDFS_ERR ("delete node failure: " +"err %d\n" , err); + } + } + +if (tree->btree_ops && + tree->btree_ops->destroy_node) + tree->btree_ops->destroy_node (node); + +ssdfs_btree_node_destroy (node); + } + +rcu_read_lock (); +spin_lock (&tree->nodes_lock); + } +spin_unlock (&tree->nodes_lock); + +rcu_read_unlock (); + +up_write (&tree->lock); + +return err; +} + +/* +* ssdfs_check_leaf_node_absence() - check that node is absent in the tree +* @tree: btree object +* @search: search object +* +* This method tries to detect that node is really absent before +* starting to add a new node. The tree should be exclusively locked +* for this operation in caller method. +* +* RETURN: +* [success] - tree hasn't requested node. +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +* %-EFAULT - tree is corrupted. +* %-EEXIST - node exists in the tree. +*/ +static +int ssdfs_check_leaf_node_absence (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search) +{ +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree || !search); +BUG_ON (!rwsem_is_locked (&tree->lock)); + +SSDFS_DBG ("node_id %u, height %u\n" , + search->node.id, search->node.height); +#endif /* CONFIG_SSDFS_DEBUG */ + +switch (search->node.state) { +case SSDFS_BTREE_SEARCH_ROOT_NODE_DESC : +case SSDFS_BTREE_SEARCH_FOUND_INDEX_NODE_DESC : +case SSDFS_BTREE_SEARCH_FOUND_LEAF_NODE_DESC : +/* expected state */ +break ; + +default : +SSDFS_ERR ("invalid search object state: " +"search->node.state %#x\n" , + search->node.state); +return -ERANGE ; + } + +if (!search->node.parent) { +SSDFS_ERR ("parent node is NULL\n" ); +return -ERANGE ; + } + + err =__ssdfs_btree_find_item (tree, search); +if (err == -ENODATA ) { +switch (search->result.state) { +case SSDFS_BTREE_SEARCH_PLEASE_ADD_NODE : +/* +* node doesn't exist in the tree +*/ + err =0 ; +break ; + +default : +/* +* existing node has free space +*/ + err = -EEXIST ; +break ; + } + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find index: " +"start_hash %llx, err %d\n" , + search->request.start.hash, + err); + }else + err = -EEXIST ; + +return err; +} + +/* +* ssdfs_btree_define_new_node_type() - define the type of creating node +* @tree: btree object +* @search: search object +* +* This method tries to define the type of creating node. +* +* RETURN: +* [success] - type of creating node. +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +* %-EFAULT - tree is corrupted. +*/ +static +int ssdfs_btree_define_new_node_type (struct ssdfs_btree *tree, +struct ssdfs_btree_node *parent) +{ +int tree_height; +int parent_height; +int parent_type; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree); +BUG_ON (!rwsem_is_locked (&tree->lock)); + +SSDFS_DBG ("tree %p, parent %p\n" , + tree, parent); +#endif /* CONFIG_SSDFS_DEBUG */ + + tree_height =atomic_read (&tree->height); + +if (tree_height <=SSDFS_BTREE_PARENT2LEAF_HEIGHT ) { +/* btree contains root node only */ +return SSDFS_BTREE_LEAF_NODE ; + } + +if (!parent) { +SSDFS_ERR ("parent node is NULL\n" ); +return -ERANGE ; + } + + parent_height =atomic_read (&parent->height); + +if (parent_height ==0 ) { +SSDFS_ERR ("invalid parent height %u\n" , + parent_height); +return -ERANGE ; + } + + parent_type =atomic_read (&parent->type); +switch (parent_type) { +case SSDFS_BTREE_ROOT_NODE : +switch (parent_height) { +case SSDFS_BTREE_LEAF_NODE_HEIGHT : +case SSDFS_BTREE_PARENT2LEAF_HEIGHT : +if (can_add_new_index (parent)) +return SSDFS_BTREE_LEAF_NODE ; +else +return SSDFS_BTREE_HYBRID_NODE ; + +case SSDFS_BTREE_PARENT2HYBRID_HEIGHT : +if (can_add_new_index (parent)) +return SSDFS_BTREE_HYBRID_NODE ; +else +return SSDFS_BTREE_INDEX_NODE ; + +default : +return SSDFS_BTREE_INDEX_NODE ; + } + +case SSDFS_BTREE_INDEX_NODE : +switch (parent_height) { +case SSDFS_BTREE_PARENT2HYBRID_HEIGHT : +if (can_add_new_index (parent)) +return SSDFS_BTREE_HYBRID_NODE ; +else +return SSDFS_BTREE_INDEX_NODE ; + +case SSDFS_BTREE_PARENT2LEAF_HEIGHT : +return SSDFS_BTREE_LEAF_NODE ; + +default : +return SSDFS_BTREE_INDEX_NODE ; + } + +case SSDFS_BTREE_HYBRID_NODE : +return SSDFS_BTREE_LEAF_NODE ; + } + +SSDFS_ERR ("invalid btree node's type %#x\n" , + parent_type); +return -ERANGE ; +} + +/* +* ssdfs_current_segment_pre_allocate_node() - pre-allocate the node +* @node_type: type of the node +* @node: node object +* +* This method tries to pre-allocate the node +* in the current segment. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +* %-EFAULT - tree is corrupted. +* %-ENOSPC - volume hasn't free space. +*/ +static +int ssdfs_current_segment_pre_allocate_node (int node_type, +struct ssdfs_btree_node *node) +{ +struct ssdfs_fs_info *fsi; +struct ssdfs_segment_request *req; +struct ssdfs_segment_info *si; +u64 ino; +u64 logical_offset; +u64 seg_id; +int seg_type; +struct ssdfs_blk2off_range extent; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node_type %#x\n" , node_type); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!node) { +SSDFS_ERR ("node is NULL\n" ); +return -ERANGE ; + } + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node->tree); +#endif /* CONFIG_SSDFS_DEBUG */ + + fsi = node->tree->fsi; + + req =ssdfs_request_alloc (); +if (IS_ERR_OR_NULL (req)) { + err = (req ==NULL ? -ENOMEM :PTR_ERR (req)); +SSDFS_ERR ("fail to allocate segment request: err %d\n" , + err); +return err; + } + +ssdfs_request_init (req); +ssdfs_get_request (req); + + ino = node->tree->owner_ino; + logical_offset = (u64 )node->node_id * node->node_size; +ssdfs_request_prepare_logical_extent (ino, + logical_offset, + node->node_size, +0 ,0 , req); + +switch (node_type) { +case SSDFS_BTREE_INDEX_NODE : + err =ssdfs_segment_pre_alloc_index_node_extent_async (fsi, req, + &seg_id, + &extent); +break ; + +case SSDFS_BTREE_HYBRID_NODE : + err =ssdfs_segment_pre_alloc_hybrid_node_extent_async (fsi, + req, + &seg_id, + &extent); +break ; + +case SSDFS_BTREE_LEAF_NODE : + err =ssdfs_segment_pre_alloc_leaf_node_extent_async (fsi, req, + &seg_id, + &extent); +break ; + +default : + err = -ERANGE ; +SSDFS_ERR ("invalid node_type %#x\n" , node_type); + goto finish_pre_allocate_node; + } + +if (err == -ENOSPC ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to pre-allocate node: " +"free space is absent\n" ); +#endif /* CONFIG_SSDFS_DEBUG */ + goto free_segment_request; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to pre-allocate node: err %d\n" , + err); + goto free_segment_request; + } + +if (node->pages_per_node != extent.len) { + err = -ERANGE ; +SSDFS_ERR ("invalid request result: " +"pages_per_node %u != len %u\n" , + node->pages_per_node, + extent.len); + goto finish_pre_allocate_node; + } + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (seg_id ==U64_MAX ); +#endif /* CONFIG_SSDFS_DEBUG */ + + seg_type =NODE2SEG_TYPE (node_type); + + si =ssdfs_grab_segment (fsi, seg_type, seg_id,U64_MAX ); +if (IS_ERR_OR_NULL (si)) { + err = (si ==NULL ? -ERANGE :PTR_ERR (si)); +SSDFS_ERR ("fail to grab segment object: " +"err %d\n" , + err); + goto finish_pre_allocate_node; + } + +spin_lock (&node->descriptor_lock); + node->seg = si; + node->extent.seg_id =cpu_to_le64 (seg_id); + node->extent.logical_blk =cpu_to_le32 (extent.start_lblk); + node->extent.len =cpu_to_le32 (extent.len); +ssdfs_memcpy (&node->node_index.index.extent, +0 ,sizeof (struct ssdfs_raw_extent ), + &node->extent, +0 ,sizeof (struct ssdfs_raw_extent ), +sizeof (struct ssdfs_raw_extent )); +spin_unlock (&node->descriptor_lock); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("tree_type %#x, node_id %u, node_type %#x, " +"seg_id %llu, logical_blk %u, len %u\n" , + node->tree->type, node->node_id, node_type, + seg_id, extent.start_lblk, extent.len); +#endif /* CONFIG_SSDFS_DEBUG */ + +return 0 ; + +free_segment_request: +ssdfs_put_request (req); +ssdfs_request_free (req); + +finish_pre_allocate_node: +return err; +} + +/* +* ssdfs_check_leaf_node_state() - check the leaf node's state +* @search: search object +* +* This method checks the leaf node's state. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +* %-EEXIST - node exists. +*/ +static +int ssdfs_check_leaf_node_state (struct ssdfs_btree_search *search) +{ +struct ssdfs_btree_node *node; +int state; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!search); + +SSDFS_DBG ("node_id %u, height %u\n" , + search->node.id, search->node.height); +#endif /* CONFIG_SSDFS_DEBUG */ + + state = search->node.state; +if (state !=SSDFS_BTREE_SEARCH_FOUND_LEAF_NODE_DESC ) { +SSDFS_ERR ("invalid node state %#x\n" , state); +return -ERANGE ; + } + +if (!search->node.child) { +SSDFS_ERR ("child node is NULL\n" ); +return -ERANGE ; + } + +check_leaf_node_state: +switch (atomic_read (&search->node.child->state)) { +case SSDFS_BTREE_NODE_INITIALIZED : +case SSDFS_BTREE_NODE_DIRTY : + err = -EEXIST ; +break ; + +case SSDFS_BTREE_NODE_CREATED : +case SSDFS_BTREE_NODE_CONTENT_PREPARED : + node = search->node.child; + err =SSDFS_WAIT_COMPLETION (&node->init_end); +if (unlikely (err)) { +SSDFS_ERR ("node init failed: " +"err %d\n" , err); + }else { + err = -EEXIST ; + goto check_leaf_node_state; + } +break ; + +default : +BUG (); + } + +return err; +} + +/* +* ssdfs_prepare_empty_btree_for_add() - prepare empty btree for adding +* @tree: btree object +* @search: search object +* @hierarchy: hierarchy object +* +* This method prepares empty btree for adding a new node. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +*/ +#ifdef CONFIG_SSDFS_UNDER_DEVELOPMENT_FUNC +static +int ssdfs_prepare_empty_btree_for_add (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search, +struct ssdfs_btree_hierarchy *hierarchy) +{ +struct ssdfs_btree_level *level; +struct ssdfs_btree_node *parent_node; +int cur_height, tree_height; +u64 start_hash, end_hash; +int parent_node_type; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree || !search || !hierarchy); +BUG_ON (!rwsem_is_locked (&tree->lock)); + +SSDFS_DBG ("tree %p, search %p, hierarchy %p\n" , + tree, search, hierarchy); +#endif /* CONFIG_SSDFS_DEBUG */ + + tree_height =atomic_read (&tree->height); +if (tree_height <=0 ) { +SSDFS_ERR ("invalid tree_height %u\n" , + tree_height); +return -ERANGE ; + } + + parent_node = search->node.parent; + +if (!parent_node) { +SSDFS_ERR ("parent is NULL\n" ); +return -ERANGE ; + } + + cur_height = search->node.height; +if (cur_height >= tree_height) { +SSDFS_ERR ("cur_height %u >= tree_height %u\n" , + cur_height, tree_height); +return -ERANGE ; + } + + start_hash = search->request.start.hash; + end_hash = search->request.end.hash; + + parent_node_type =atomic_read (&parent_node->type); + +if (parent_node_type !=SSDFS_BTREE_ROOT_NODE ) { +SSDFS_ERR ("corrupted hierarchy: " +"expected parent root node\n" ); +return -ERANGE ; + } + +if ((tree_height +1 ) != hierarchy->desc.height) { +SSDFS_ERR ("corrupted hierarchy: " +"tree_height %u, " +"hierarchy->desc.height %u\n" , + tree_height, + hierarchy->desc.height); +return -ERANGE ; + } + +if (!can_add_new_index (parent_node)) { +SSDFS_ERR ("unable add index into the root\n" ); +return -ERANGE ; + } + + level = hierarchy->array_ptr[cur_height]; +ssdfs_btree_prepare_add_node (tree,SSDFS_BTREE_LEAF_NODE , + start_hash, end_hash, + level,NULL ); + + level = hierarchy->array_ptr[cur_height +1 ]; + err =ssdfs_btree_prepare_add_index (level, + start_hash, + end_hash, + parent_node); +if (unlikely (err)) { +SSDFS_ERR ("fail to prepare level: " +"node_id %u, height %u\n" , + parent_node->node_id, +atomic_read (&parent_node->height)); +return err; + } + +return 0 ; +} +#endif /* CONFIG_SSDFS_UNDER_DEVELOPMENT_FUNC */ + +/* +* __ssdfs_btree_read_node() - create and initialize the node +* @tree: btree object +* @parent: parent node +* @node_index: index key of preparing node +* @node_type: type of the node +* @node_id: node ID +* +* This method tries to read the node's content from the disk. +* +* RETURN: +* [success] - pointer on created node object. +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +* %-EEXIST - node exists already. +*/ +struct ssdfs_btree_node * +__ssdfs_btree_read_node (struct ssdfs_btree *tree, +struct ssdfs_btree_node *parent, +struct ssdfs_btree_index_key *node_index, +u8 node_type,u32 node_id) +{ +struct ssdfs_btree_node *ptr, *node; +int height; +u64 start_hash; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("tree %p, parent %p, " +"node_index %p, node_type %#x, node_id %llu\n" , + tree, parent, node_index, + node_type, (u64 )node_id); + +BUG_ON (!tree || !parent || !node_index); +BUG_ON (!rwsem_is_locked (&tree->lock)); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (node_type <=SSDFS_BTREE_NODE_UNKNOWN_TYPE || + node_type >=SSDFS_BTREE_NODE_TYPE_MAX ) { +SSDFS_WARN ("invalid node type %#x\n" , + node_type); +return ERR_PTR (-ERANGE ); + } + + height =atomic_read (&parent->height); +if (height <=0 ) { +SSDFS_ERR ("invalid height %u, node_id %u\n" , + height, parent->node_id); +return ERR_PTR (-ERANGE ); + }else + height -=1 ; + + start_hash =le64_to_cpu (node_index->index.hash); + ptr =ssdfs_btree_node_create (tree, node_id, parent, + height, node_type, start_hash); +if (unlikely (IS_ERR_OR_NULL (ptr))) { + err = !ptr ? -ENOMEM :PTR_ERR (ptr); +SSDFS_ERR ("fail to create node: err %d\n" , + err); +return ptr; + } + +if (tree->btree_ops && tree->btree_ops->create_node) { + err = tree->btree_ops->create_node (ptr); +if (unlikely (err)) { +SSDFS_ERR ("fail to create the node: " +"err %d\n" , err); +ssdfs_btree_node_destroy (ptr); +return ERR_PTR (err); + } + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("NODE_INDEX: node_id %u, node_type %#x, " +"height %u, flags %#x, hash %llx, " +"seg_id %llu, logical_blk %u, len %u\n" , +le32_to_cpu (node_index->node_id), + node_index->node_type, + node_index->height, +le16_to_cpu (node_index->flags), +le64_to_cpu (node_index->index.hash), +le64_to_cpu (node_index->index.extent.seg_id), +le32_to_cpu (node_index->index.extent.logical_blk), +le32_to_cpu (node_index->index.extent.len)); +#endif /* CONFIG_SSDFS_DEBUG */ + +spin_lock (&ptr->descriptor_lock); +ssdfs_memcpy (&ptr->node_index, +0 ,sizeof (struct ssdfs_btree_index_key ), + node_index, +0 ,sizeof (struct ssdfs_btree_index_key ), +sizeof (struct ssdfs_btree_index_key )); +spin_unlock (&ptr->descriptor_lock); + +try_find_node: +spin_lock (&tree->nodes_lock); + node =radix_tree_lookup (&tree->nodes, node_id); +spin_unlock (&tree->nodes_lock); + +if (!node) { + err =radix_tree_preload (GFP_NOFS ); +if (unlikely (err)) { +SSDFS_ERR ("fail to preload radix tree: err %d\n" , + err); + goto finish_insert_node; + } + +spin_lock (&tree->nodes_lock); + err =radix_tree_insert (&tree->nodes, node_id, ptr); +spin_unlock (&tree->nodes_lock); + +radix_tree_preload_end (); + +if (err == -EEXIST ) + goto try_find_node; +else if (unlikely (err)) { +SSDFS_ERR ("fail to add node into radix tree: " +"node_id %llu, node %p, err %d\n" , + (u64 )node_id, ptr, err); + goto finish_insert_node; + } + }else { +switch (atomic_read (&node->state)) { +case SSDFS_BTREE_NODE_CREATED : + err = -EAGAIN ; + goto finish_insert_node; + +case SSDFS_BTREE_NODE_INITIALIZED : +case SSDFS_BTREE_NODE_DIRTY : + err = -EEXIST ; +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node %u has been found\n" , + node_id); +#endif /* CONFIG_SSDFS_DEBUG */ + goto finish_insert_node; + +default : + err = -ERANGE ; +SSDFS_WARN ("invalid node state %#x\n" , +atomic_read (&node->state)); + goto finish_insert_node; + } + } + +finish_insert_node: +if (err == -EAGAIN ) { + err =SSDFS_WAIT_COMPLETION (&node->init_end); +if (unlikely (err)) { +SSDFS_ERR ("node init failed: " +"err %d\n" , err); +ssdfs_btree_node_destroy (ptr); +return ERR_PTR (err); + }else + goto try_find_node; + }else if (err == -EEXIST ) { +ssdfs_btree_node_destroy (ptr); +return node; + }else if (unlikely (err)) { +ssdfs_btree_node_destroy (ptr); +return ERR_PTR (err); + } + + err =ssdfs_btree_node_prepare_content (ptr, node_index); +if (err == -EINTR ) { +/* +* Ignore this error. +*/ + goto fail_read_node; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to prepare btree node's content: " +"err %d\n" , err); + goto fail_read_node; + } + +if (tree->btree_ops && tree->btree_ops->init_node) { + err = tree->btree_ops->init_node (ptr); +if (unlikely (err)) { +SSDFS_ERR ("fail to init btree node: " +"err %d\n" , err); + goto fail_read_node; + } + } + +atomic_set (&ptr->state,SSDFS_BTREE_NODE_INITIALIZED ); +complete_all (&ptr->init_end); +return ptr; + +fail_read_node: +complete_all (&ptr->init_end); +ssdfs_btree_radix_tree_delete (tree, node_id); +if (tree->btree_ops && tree->btree_ops->delete_node) + tree->btree_ops->delete_node (ptr); +if (tree->btree_ops && tree->btree_ops->destroy_node) + tree->btree_ops->destroy_node (ptr); +ssdfs_btree_node_destroy (ptr); +return ERR_PTR (err); +} + +/* +* ssdfs_btree_read_node() - create and initialize the node +* @tree: btree object +* @search: search object [in|out] +* +* This method tries to read the node's content from the disk. +* +* RETURN: +* [success] - pointer on created node object. +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +*/ +static +struct ssdfs_btree_node * +ssdfs_btree_read_node (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search) +{ +u8 node_type; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree || !search); +BUG_ON (!rwsem_is_locked (&tree->lock)); + +SSDFS_DBG ("tree %p, id %u, node_id %u, " +"hash %llx, " +"extent (seg %u, logical_blk %u, len %u)\n" , + tree, + search->node.id, +le32_to_cpu (search->node.found_index.node_id), +le64_to_cpu (search->node.found_index.index.hash), +le32_to_cpu (search->node.found_index.index.extent.seg_id), +le32_to_cpu (search->node.found_index.index.extent.logical_blk), +le32_to_cpu (search->node.found_index.index.extent.len)); +#endif /* CONFIG_SSDFS_DEBUG */ + + node_type = search->node.found_index.node_type; +return __ssdfs_btree_read_node (tree, search->node.parent, + &search->node.found_index, + node_type, search->node.id); +} + +/* +* ssdfs_btree_get_child_node_for_hash() - get child node for hash +* @tree: btree object +* @parent: parent node +* @upper_hash: upper value of the hash +* +* This method tries to extract child node for the hash value. +* +* RETURN: +* [success] - pointer on the child node. +* [failure] - error code: +* +* %-ERANGE - internal error. +* %-EACCES - node is under initialization. +* %-ENOENT - index area is absent. +*/ +struct ssdfs_btree_node * +ssdfs_btree_get_child_node_for_hash (struct ssdfs_btree *tree, +struct ssdfs_btree_node *parent, +u64 upper_hash) +{ +struct ssdfs_btree_node *child =ERR_PTR (-ERANGE ); +struct ssdfs_btree_node_index_area area; +struct ssdfs_btree_index_key index_key; +int parent_type; +u16 found_index =U16_MAX ; +u32 node_id; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree || !parent); +BUG_ON (upper_hash >=U64_MAX ); + +SSDFS_DBG ("node_id %u, upper_hash %llx\n" , + parent->node_id, upper_hash); +#endif /* CONFIG_SSDFS_DEBUG */ + +switch (atomic_read (&parent->state)) { +case SSDFS_BTREE_NODE_CREATED : + err = -EACCES ; +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node %u is under initialization\n" , + parent->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ +return ERR_PTR (err); + +case SSDFS_BTREE_NODE_INITIALIZED : +case SSDFS_BTREE_NODE_DIRTY : +/* expected state */ +break ; + +default : + err = -ERANGE ; +SSDFS_ERR ("invalid node state %#x\n" , +atomic_read (&parent->state)); +return ERR_PTR (err); + } + +if (!is_ssdfs_btree_node_index_area_exist (parent)) { + err = -ENOENT ; +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node %u hasn't index area\n" , + parent->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ +return ERR_PTR (err); + } + +down_read (&parent->full_lock); + + parent_type =atomic_read (&parent->type); +if (parent_type <=SSDFS_BTREE_NODE_UNKNOWN_TYPE || + parent_type >=SSDFS_BTREE_NODE_TYPE_MAX ) { + child =ERR_PTR (-ERANGE ); +SSDFS_ERR ("invalid node type %#x\n" , + parent_type); + goto finish_child_search; + } + +down_read (&parent->header_lock); +ssdfs_memcpy (&area, +0 ,sizeof (struct ssdfs_btree_node_index_area ), + &parent->index_area, +0 ,sizeof (struct ssdfs_btree_node_index_area ), +sizeof (struct ssdfs_btree_node_index_area )); + err =ssdfs_find_index_by_hash (parent, &area, upper_hash, + &found_index); +up_read (&parent->header_lock); + +if (err == -EEXIST ) { +/* hash == found hash */ + err =0 ; + }else if (err == -ENODATA ) { + child =ERR_PTR (err); +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to find an index: " +"node_id %u, hash %llx\n" , + parent->node_id, upper_hash); +#endif /* CONFIG_SSDFS_DEBUG */ + goto finish_child_search; + }else if (unlikely (err)) { + child =ERR_PTR (err); +SSDFS_ERR ("fail to find an index: " +"node_id %u, hash %llx, err %d\n" , + parent->node_id, upper_hash, + err); + goto finish_child_search; + } + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (found_index ==U16_MAX ); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (parent_type ==SSDFS_BTREE_ROOT_NODE ) { + err =__ssdfs_btree_root_node_extract_index (parent, + found_index, + &index_key); + }else { + err =__ssdfs_btree_common_node_extract_index (parent, &area, + found_index, + &index_key); + } + +if (unlikely (err)) { + child =ERR_PTR (err); +SSDFS_ERR ("fail to extract index: " +"node_id %u, node_type %#x, " +"found_index %u, err %d\n" , + parent->node_id, parent_type, + found_index, err); + goto finish_child_search; + } + + node_id =le32_to_cpu (index_key.node_id); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node_id %u, node_type %#x\n" , + node_id, index_key.node_type); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_btree_radix_tree_find (tree, node_id, &child); +if (err == -ENOENT ) { + err =0 ; + child =__ssdfs_btree_read_node (tree, parent, + &index_key, + index_key.node_type, + node_id); +if (unlikely (IS_ERR_OR_NULL (child))) { + err = !child ? -ENOMEM :PTR_ERR (child); +SSDFS_ERR ("fail to read: " +"node %llu, err %d\n" , + (u64 )node_id, err); + goto finish_child_search; + } + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find node in radix tree: " +"node_id %llu, err %d\n" , + (u64 )node_id, err); + goto finish_child_search; + }else if (!child) { + child =ERR_PTR (-ERANGE ); +SSDFS_WARN ("empty node pointer\n" ); + goto finish_child_search; + } + +finish_child_search: +up_read (&parent->full_lock); + +if (err) { +SSDFS_ERR ("node_id %u, upper_hash %llx\n" , + parent->node_id, upper_hash); +SSDFS_ERR ("index_area: index_count %u, index_capacity %u, " +"start_hash %llx, end_hash %llx\n" , + area.index_count, area.index_capacity, + area.start_hash, area.end_hash); +ssdfs_debug_show_btree_node_indexes (parent->tree, parent); + } + +return child; +} + +/* +* ssdfs_btree_generate_node_id() - generate new node ID +* @tree: btree object +* +* It is possible to use the simple technique. The upper node ID will +* be the latest allocated ID number. Generating the node ID means +* simply increasing the upper node ID value. In the case of node deletion +* it needs to leave the empty node till the whole branch of tree will +* be deleted. The goal is to keep the upper node ID in valid state. +* And the upper node ID can be decreased if the whold branch of empty +* nodes will be deleted. +* +* <Currently node deletion is simple operation. Any node can be deleted. +* The implementation should be changed if u32 will be not enough for +* the node ID representation.> +* +* RETURN: +* [success] - new node ID +* [failure] - U32_MAX +*/ +u32 ssdfs_btree_generate_node_id (struct ssdfs_btree *tree) +{ +struct ssdfs_btree_node *node; +u32 node_id =U32_MAX ; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree); + +SSDFS_DBG ("tree %p\n" , tree); +#endif /* CONFIG_SSDFS_DEBUG */ + +spin_lock (&tree->nodes_lock); + node_id = tree->upper_node_id; +if (node_id <U32_MAX ) { + node_id++; + tree->upper_node_id = node_id; + } +spin_unlock (&tree->nodes_lock); + +if (node_id ==U32_MAX ) { +SSDFS_DBG ("node IDs are completely used\n" ); +return node_id; + } + + err =ssdfs_btree_radix_tree_find (tree, +SSDFS_BTREE_ROOT_NODE_ID , + &node); +if (unlikely (err)) { +SSDFS_ERR ("fail to find root node in radix tree: " +"err %d\n" , err); +return U32_MAX ; + }else if (!node) { +SSDFS_WARN ("empty node pointer\n" ); +return U32_MAX ; + } + +set_ssdfs_btree_node_dirty (node); + +return node_id; +} + +/* +* ssdfs_btree_destroy_empty_node() - destroy the empty node. +* @tree: btree object +* @node: node object +* +* This method tries to destroy the empty node. +*/ +static inline +void ssdfs_btree_destroy_empty_node (struct ssdfs_btree *tree, +struct ssdfs_btree_node *node) +{ +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!node) +return ; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node_id %u, height %u\n" , + node->node_id, +atomic_read (&node->height)); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (tree->btree_ops && tree->btree_ops->destroy_node) + tree->btree_ops->destroy_node (node); + +ssdfs_btree_node_destroy (node); +} + +/* +* ssdfs_btree_create_empty_node() - create empty node. +* @tree: btree object +* @cur_height: height for node creation +* @hierarchy: hierarchy object +* +* This method tries to create the empty node. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +*/ +static +int ssdfs_btree_create_empty_node (struct ssdfs_btree *tree, +int cur_height, +struct ssdfs_btree_hierarchy *hierarchy) +{ +struct ssdfs_btree_level *level; +struct ssdfs_btree_node *parent =NULL , *ptr =NULL ; +u32 node_id; +int node_type; +int tree_height; +u16 flags; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree || !hierarchy); +BUG_ON (!rwsem_is_locked (&tree->lock)); + +SSDFS_DBG ("tree %p, cur_height %d\n" , + tree, cur_height); +#endif /* CONFIG_SSDFS_DEBUG */ + + tree_height =atomic_read (&tree->height); + +if (cur_height > tree_height) { +SSDFS_ERR ("cur_height %d > tree_height %d\n" , + cur_height, tree_height); +return -ERANGE ; + } + + level = hierarchy->array_ptr[cur_height]; + +if (!(level->flags &SSDFS_BTREE_LEVEL_ADD_NODE )) +return 0 ; + + node_id =ssdfs_btree_generate_node_id (tree); +if (node_id ==SSDFS_BTREE_NODE_INVALID_ID ) { +SSDFS_ERR ("fail to generate node_id: err %d\n" , + err); +return -ERANGE ; + } + + level = hierarchy->array_ptr[cur_height +1 ]; +if (level->flags &SSDFS_BTREE_LEVEL_ADD_NODE ) + parent = level->nodes.new_node.ptr; +else if (level->nodes.new_node.type ==SSDFS_BTREE_ROOT_NODE ) + parent = level->nodes.new_node.ptr; +else + parent = level->nodes.old_node.ptr; + +if (!parent) { +SSDFS_ERR ("parent is NULL\n" ); +return -ERANGE ; + } + + node_type =ssdfs_btree_define_new_node_type (tree, parent); +switch (node_type) { +case SSDFS_BTREE_INDEX_NODE : +case SSDFS_BTREE_HYBRID_NODE : +case SSDFS_BTREE_LEAF_NODE : +/* expected state */ +break ; + +default : +if (node_type <0 ) { +SSDFS_ERR ("fail to define the new node type: " +"err %d\n" , err); + }else { +SSDFS_ERR ("invalid node type %#x\n" , + node_type); + } +return node_type <0 ? node_type : -ERANGE ; + } + + level = hierarchy->array_ptr[cur_height]; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("level: flags %#x, move.direction %#x\n" , + level->flags, + level->index_area.move.direction); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (level->flags &SSDFS_BTREE_INDEX_AREA_NEED_MOVE ) { +switch (level->index_area.move.direction) { +case SSDFS_BTREE_MOVE_TO_RIGHT : +case SSDFS_BTREE_MOVE_TO_LEFT : +SSDFS_DBG ("correct node type\n" ); +/* correct node type */ + node_type =SSDFS_BTREE_INDEX_NODE ; +break ; + +default : +/* do nothing */ +break ; + } + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node_id %u, node_type %#x\n" , + node_id, node_type); +#endif /* CONFIG_SSDFS_DEBUG */ + + level = hierarchy->array_ptr[cur_height]; + ptr =ssdfs_btree_node_create (tree, node_id, parent, cur_height, + node_type, + level->items_area.hash.start); +if (unlikely (IS_ERR_OR_NULL (ptr))) { + err = !ptr ? -ENOMEM :PTR_ERR (ptr); +SSDFS_ERR ("fail to create node: err %d\n" , + err); +return err; + } + +if (tree->btree_ops && tree->btree_ops->create_node) { + err = tree->btree_ops->create_node (ptr); +if (unlikely (err)) { +SSDFS_ERR ("fail to create the node: " +"err %d\n" , err); + goto finish_create_node; + } + } + + err =ssdfs_current_segment_pre_allocate_node (node_type, ptr); +if (err == -ENOSPC ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to preallocate node: id %u\n" , + node_id); +#endif /* CONFIG_SSDFS_DEBUG */ + goto finish_create_node; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to preallocate node: id %u, err %d\n" , + node_id, err); + goto finish_create_node; + } + +atomic_or (SSDFS_BTREE_NODE_PRE_ALLOCATED , + &ptr->flags); + + flags =le16_to_cpu (ptr->node_index.flags); + flags |=SSDFS_BTREE_INDEX_SHOW_PREALLOCATED_CHILD ; + ptr->node_index.flags =cpu_to_le16 (flags); + + level->nodes.new_node.type = node_type; + level->nodes.new_node.ptr = ptr; +return 0 ; + +finish_create_node: +ssdfs_btree_destroy_empty_node (tree, ptr); +return err; +} + +/* +* ssdfs_btree_update_parent_node_pointer() - update child's parent's pointer +* @tree: btree object +* @parent: parent node +* +* This method tries to update parent pointer in child nodes. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +*/ +int ssdfs_btree_update_parent_node_pointer (struct ssdfs_btree *tree, +struct ssdfs_btree_node *parent) +{ +struct ssdfs_btree_node *child =NULL ; +struct ssdfs_btree_node_index_area area; +struct ssdfs_btree_index_key index_key; +int type; +u32 node_id; +spinlock_t *lock; +int i; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!parent); + +SSDFS_DBG ("node_id %u, height %u\n" , + parent->node_id, +atomic_read (&parent->height)); +#endif /* CONFIG_SSDFS_DEBUG */ + + type =atomic_read (&parent->type); + +switch (type) { +case SSDFS_BTREE_ROOT_NODE : +case SSDFS_BTREE_INDEX_NODE : +case SSDFS_BTREE_HYBRID_NODE : +if (!is_ssdfs_btree_node_index_area_exist (parent)) { +SSDFS_ERR ("corrupted node %u\n" , + parent->node_id); +return -ERANGE ; + } +break ; + +case SSDFS_BTREE_LEAF_NODE : +/* do nothing */ +return 0 ; + +default : +BUG (); + } + +if (is_ssdfs_btree_node_index_area_empty (parent)) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node %u has empty index area\n" , + parent->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ +return 0 ; + } + +down_read (&parent->full_lock); + +down_read (&parent->header_lock); +ssdfs_memcpy (&area, +0 ,sizeof (struct ssdfs_btree_node_index_area ), + &parent->index_area, +0 ,sizeof (struct ssdfs_btree_node_index_area ), +sizeof (struct ssdfs_btree_node_index_area )); +up_read (&parent->header_lock); + +for (i =0 ; i < area.index_count; i++) { +if (type ==SSDFS_BTREE_ROOT_NODE ) { + err =__ssdfs_btree_root_node_extract_index (parent, + i, + &index_key); + }else { + err =__ssdfs_btree_common_node_extract_index (parent, + &area, i, + &index_key); + } + +if (unlikely (err)) { +SSDFS_ERR ("fail to extract index key: " +"index_position %d, err %d\n" , + i, err); + goto finish_index_processing; + } + + node_id =le32_to_cpu (index_key.node_id); + +if (node_id == parent->node_id) { +/* +* Hybrid node contains index on itself +* in the index area. Ignore this node_id. +*/ +if (type != index_key.node_type) { +SSDFS_WARN ("type %#x != node_type %#x\n" , + type, index_key.node_type); + } +continue ; + } + +up_read (&parent->full_lock); + + err =ssdfs_btree_radix_tree_find (tree, node_id, &child); + +if (!child) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("empty node pointer: " +"node_id %u\n" , node_id); +#endif /* CONFIG_SSDFS_DEBUG */ + } + +if (!err) { + lock = &child->descriptor_lock; +spin_lock (lock); + child->parent_node = parent; +spin_unlock (lock); + lock =NULL ; + } + +down_read (&parent->full_lock); + +if (err == -ENOENT ) { + err =0 ; +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node %u is absent\n" , + node_id); +#endif /* CONFIG_SSDFS_DEBUG */ +continue ; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find node in radix tree: " +"node_id %llu, err %d\n" , + (u64 )node_id, err); + goto finish_index_processing; + } + } + +finish_index_processing: +up_read (&parent->full_lock); + +if (unlikely (err)) +ssdfs_debug_show_btree_node_indexes (tree, parent); + +return err; +} + +/* +* ssdfs_btree_process_hierarchy_for_add_nolock() - process hierarchy for add +* @tree: btree object +* @search: search object [in|out] +* @hierarchy: hierarchy object [in|out] +* +* This method tries to add a node into the tree with the goal +* to increase capacity of items in the tree. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +*/ +static +int ssdfs_btree_process_hierarchy_for_add_nolock (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search, +struct ssdfs_btree_hierarchy *hierarchy) +{ +struct ssdfs_btree_level *level; +struct ssdfs_btree_node *node; +int cur_height, tree_height; +u32 node_id; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree || !tree->fsi || !search); +BUG_ON (!rwsem_is_locked (&tree->lock)); + +SSDFS_DBG ("tree %p, start_hash %llx\n" , + tree, search->request.start.hash); +#endif /* CONFIG_SSDFS_DEBUG */ + + tree_height =atomic_read (&tree->height); +if (tree_height <=0 ) { +SSDFS_ERR ("invalid tree_height %d\n" , + tree_height); +return -ERANGE ; + } + +for (cur_height = tree_height; cur_height >=0 ; cur_height--) { + level = hierarchy->array_ptr[cur_height]; + +if (!need_add_node (level)) +continue ; + + err =ssdfs_btree_create_empty_node (tree, cur_height, + hierarchy); +if (err) { +if (err == -ENOSPC ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to create empty node: " +"err %d\n" , + err); +#endif /* CONFIG_SSDFS_DEBUG */ + }else { +SSDFS_ERR ("fail to create empty node: " +"err %d\n" , + err); + } + +for (cur_height++; cur_height <= tree_height; cur_height++) { +if (!need_add_node (level)) +continue ; + + node = level->nodes.new_node.ptr; + +if (!node) +continue ; + + node_id = node->node_id; +ssdfs_btree_radix_tree_delete (tree, node_id); +ssdfs_btree_destroy_empty_node (tree, node); + } + + goto finish_create_node; + } + + node = level->nodes.new_node.ptr; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_btree_radix_tree_insert (tree, node->node_id, + node); +if (unlikely (err)) { +SSDFS_ERR ("fail to insert node %u into radix tree: " +"err %d\n" , + node->node_id, err); + +for (; cur_height < tree_height; cur_height++) { + level = hierarchy->array_ptr[cur_height]; + +if (!need_add_node (level)) +continue ; + + node = level->nodes.new_node.ptr; + node_id = node->node_id; +ssdfs_btree_radix_tree_delete (tree, node_id); +ssdfs_btree_destroy_empty_node (tree, node); + } + + goto finish_create_node; + } + +set_ssdfs_btree_node_dirty (node); + } + + cur_height =0 ; +for (; cur_height < hierarchy->desc.height; cur_height++) { + err =ssdfs_btree_process_level_for_add (hierarchy, cur_height, + search); +if (unlikely (err)) { +SSDFS_ERR ("fail to process the tree's level: " +"cur_height %u, err %d\n" , + cur_height, err); + goto finish_create_node; + } + } + +for (cur_height = tree_height; cur_height >=0 ; cur_height--) { + level = hierarchy->array_ptr[cur_height]; + +if (!need_add_node (level)) +continue ; + + node = level->nodes.new_node.ptr; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (tree->btree_ops && tree->btree_ops->add_node) { + err = tree->btree_ops->add_node (node); +if (unlikely (err)) { +SSDFS_ERR ("fail to add the node: " +"err %d\n" , err); + +for (; cur_height < tree_height; cur_height++) { + level = hierarchy->array_ptr[cur_height]; + +if (!need_add_node (level)) +continue ; + + node = level->nodes.new_node.ptr; + node_id = node->node_id; +ssdfs_btree_radix_tree_delete (tree, + node_id); +if (tree->btree_ops && + tree->btree_ops->delete_node) { + tree->btree_ops->delete_node (node); + } +ssdfs_btree_destroy_empty_node (tree, + node); + } + + goto finish_create_node; + } + } + } + +if (hierarchy->desc.increment_height) { +/* increase tree's height */ +atomic_inc (&tree->height); + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("tree->height %d\n" , +atomic_read (&tree->height)); +#endif /* CONFIG_SSDFS_DEBUG */ + +atomic_set (&tree->state,SSDFS_BTREE_DIRTY ); + +finish_create_node: +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("finished\n" ); +#endif /* CONFIG_SSDFS_DEBUG */ + +return err; +} + +/* +* __ssdfs_btree_add_node() - add a node into the btree +* @tree: btree object +* @search: search object [in|out] +* +* This method tries to add a node into the tree with the goal +* to increase capacity of items in the tree. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +* %-ENOMEM - fail to allocate memory. +* %-EEXIST - node exists already. +*/ +static +int __ssdfs_btree_add_node (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search) +{ +struct ssdfs_btree_hierarchy *hierarchy; +struct ssdfs_btree_level *level; +struct ssdfs_btree_node *node; +struct ssdfs_btree_node *parent_node; +int cur_height, tree_height; +#define SSDFS_BTREE_MODIFICATION_PHASE_MAX (3) +int phase_id; +spinlock_t *lock; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree || !tree->fsi || !search); + +SSDFS_DBG ("tree %p, start_hash %llx\n" , + tree, search->request.start.hash); +#endif /* CONFIG_SSDFS_DEBUG */ + +switch (search->node.state) { +case SSDFS_BTREE_SEARCH_ROOT_NODE_DESC : +case SSDFS_BTREE_SEARCH_FOUND_INDEX_NODE_DESC : +case SSDFS_BTREE_SEARCH_FOUND_LEAF_NODE_DESC : +/* expected state */ +break ; + +default : +SSDFS_ERR ("invalid node state %#x\n" , + search->node.state); +return -ERANGE ; + } + +if (!search->node.parent) { +SSDFS_ERR ("parent node is NULL\n" ); +return -ERANGE ; + } + + tree_height =atomic_read (&tree->height); +if (tree_height <=0 ) { +SSDFS_ERR ("invalid tree_height %d\n" , + tree_height); +return -ERANGE ; + } + + hierarchy =ssdfs_btree_hierarchy_allocate (tree); +if (IS_ERR_OR_NULL (hierarchy)) { + err = !hierarchy ? -ENOMEM :PTR_ERR (hierarchy); +SSDFS_ERR ("fail to allocate tree levels' array: " +"err %d\n" , err); +return err; + } + +down_write (&tree->lock); + + err =ssdfs_check_leaf_node_absence (tree, search); +if (err == -EEXIST ) { +up_write (&tree->lock); +SSDFS_DBG ("new node has been added\n" ); +return ssdfs_check_leaf_node_state (search); + }else if (unlikely (err)) { +SSDFS_ERR ("fail to check leaf node absence: " +"err %d\n" , err); + goto finish_create_node; + } + + err =ssdfs_btree_check_hierarchy_for_add (tree, search, hierarchy); + + phase_id =0 ; +while (err == -EAGAIN ) { +if (phase_id >SSDFS_BTREE_MODIFICATION_PHASE_MAX ) { + err = -ERANGE ; +SSDFS_WARN ("too many phases of modification\n" ); + goto finish_create_node; + } + + err =ssdfs_btree_process_hierarchy_for_add_nolock (tree, + search, + hierarchy); +if (unlikely (err)) { +SSDFS_ERR ("fail to process hierarchy for add: " +"err %d\n" , err); + goto finish_create_node; + } + +ssdfs_btree_hierarchy_free (hierarchy); + + hierarchy =ssdfs_btree_hierarchy_allocate (tree); +if (IS_ERR_OR_NULL (hierarchy)) { + err = !hierarchy ? -ENOMEM :PTR_ERR (hierarchy); +SSDFS_ERR ("fail to allocate tree levels' array: " +"err %d\n" , err); + goto finish_create_node; + } + +/* correct parent node */ + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!search->node.child); +#endif /* CONFIG_SSDFS_DEBUG */ + + lock = &search->node.child->descriptor_lock; +spin_lock (lock); + parent_node = search->node.child->parent_node; +spin_unlock (lock); + lock =NULL ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!parent_node); +#endif /* CONFIG_SSDFS_DEBUG */ + +ssdfs_btree_search_define_parent_node (search, parent_node); + + err =ssdfs_btree_check_hierarchy_for_add (tree, search, + hierarchy); +if (unlikely (err)) { +SSDFS_ERR ("fail to prepare info about hierarchy: " +"err %d\n" , err); + goto finish_create_node; + } + + phase_id++; + } + +if (unlikely (err)) { +SSDFS_ERR ("fail to prepare information about hierarchy: " +"err %d\n" , err); + goto finish_create_node; + } + + err =ssdfs_btree_process_hierarchy_for_add_nolock (tree, search, + hierarchy); +if (err == -ENOSPC ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to process hierarchy for add: " +"err %d\n" , err); +#endif /* CONFIG_SSDFS_DEBUG */ + goto finish_create_node; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to process hierarchy for add: " +"err %d\n" , err); + goto finish_create_node; + } + +up_write (&tree->lock); + +if (search->node.parent) +complete_all (&search->node.parent->init_end); + + tree_height =atomic_read (&tree->height); +for (cur_height =0 ; cur_height < tree_height; cur_height++) { + level = hierarchy->array_ptr[cur_height]; + +if (!need_add_node (level)) +continue ; + + node = level->nodes.new_node.ptr; +complete_all (&node->init_end); + } + +ssdfs_btree_hierarchy_free (hierarchy); + + search->result.err =0 ; + search->node.state =SSDFS_BTREE_SEARCH_NODE_DESC_EMPTY ; + search->result.state =SSDFS_BTREE_SEARCH_UNKNOWN_RESULT ; +return 0 ; + +finish_create_node: +up_write (&tree->lock); + +if (search->node.parent) +complete_all (&search->node.parent->init_end); + +if (err != -ENOSPC ) +ssdfs_show_btree_hierarchy_object (hierarchy); + +ssdfs_btree_hierarchy_free (hierarchy); + + search->result.err = err; + search->result.state =SSDFS_BTREE_SEARCH_FAILURE ; +return err; +} + +/* +* ssdfs_btree_node_convert_index2id() - convert index into node ID +* @tree: btree object +* @search: search object [in|out] +*/ +static inline +int ssdfs_btree_node_convert_index2id (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search) +{ +u32 id; +u8 height; +u8 tree_height; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree || !search); +#endif /* CONFIG_SSDFS_DEBUG */ + + id =le32_to_cpu (search->node.found_index.node_id); + height = search->node.found_index.height; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node_id %u, height %u\n" , + id, height); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (id ==SSDFS_BTREE_NODE_INVALID_ID ) { +SSDFS_ERR ("invalid node_id\n" ); +return -ERANGE ; + } + + tree_height =atomic_read (&tree->height); + +if (height >= tree_height) { +SSDFS_ERR ("height %u >= tree->height %u\n" , + height, tree_height); +return -ERANGE ; + } + + search->node.id = id; + search->node.height = height; +return 0 ; +} + +/* +* ssdfs_btree_find_right_sibling_leaf_node() - find sibling right leaf node +* @tree: btree object +* @node: btree node object +* @search: search object [in|out] +*/ +static +int ssdfs_btree_find_right_sibling_leaf_node (struct ssdfs_btree *tree, +struct ssdfs_btree_node *node, +struct ssdfs_btree_search *search) +{ +struct ssdfs_btree_node *parent_node =NULL ; +struct ssdfs_btree_node_index_area area; +struct ssdfs_btree_index_key index_key; +spinlock_t *lock; +size_t desc_len =sizeof (struct ssdfs_btree_node_index_area ); +u64 start_hash =U64_MAX , end_hash =U64_MAX ; +u64 search_hash; +u16 items_count, items_capacity; +u16 index_count =0 ; +u16 index_capacity =0 ; +u16 index_position; +int node_type; +u32 node_id; +bool is_found = false; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree || !node || !search); +BUG_ON (!rwsem_is_locked (&tree->lock)); + +SSDFS_DBG ("node_id %u, start_hash %llx\n" , + node->node_id, search->request.start.hash); +#endif /* CONFIG_SSDFS_DEBUG */ + + lock = &node->descriptor_lock; +spin_lock (lock); + search_hash =le64_to_cpu (node->node_index.index.hash); + node = node->parent_node; +spin_unlock (lock); + lock =NULL ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node); +#endif /* CONFIG_SSDFS_DEBUG */ + +down_read (&node->header_lock); + +switch (atomic_read (&node->index_area.state)) { +case SSDFS_BTREE_NODE_INDEX_AREA_EXIST : + index_count = node->index_area.index_count; + index_capacity = node->index_area.index_capacity; +break ; + +default : + err = -ERANGE ; +break ; + } + +up_read (&node->header_lock); + +if (unlikely (err)) { +SSDFS_ERR ("index area is absent\n" ); +return -ERANGE ; + } + + err =ssdfs_btree_node_find_index_position (node, search_hash, + &index_position); +if (unlikely (err)) { +SSDFS_ERR ("fail to find the index position: " +"search_hash %llx, err %d\n" , + search_hash, err); +return err; + } + + index_position++; + +if (index_position >= index_count) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("index_position %u >= index_count %u\n" , + index_position, index_count); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ENOENT ; + } + + node_type =atomic_read (&node->type); + +down_read (&node->full_lock); + +if (node_type ==SSDFS_BTREE_ROOT_NODE ) { + err =__ssdfs_btree_root_node_extract_index (node, + index_position, + &index_key); + }else { +down_read (&node->header_lock); +ssdfs_memcpy (&area,0 , desc_len, + &node->index_area,0 , desc_len, + desc_len); +up_read (&node->header_lock); + + err =__ssdfs_btree_common_node_extract_index (node, + &area, + index_position, + &index_key); + } + +up_read (&node->full_lock); + +if (unlikely (err)) { +SSDFS_ERR ("fail to extract index key: " +"index_position %u, err %d\n" , + index_position, err); +ssdfs_debug_show_btree_node_indexes (tree, node); +return err; + } + + parent_node = node; + node_id =le32_to_cpu (index_key.node_id); + + err =ssdfs_btree_radix_tree_find (tree, node_id, &node); +if (err == -ENOENT ) { + err =0 ; + node =__ssdfs_btree_read_node (tree, parent_node, + &index_key, + index_key.node_type, + node_id); +if (unlikely (IS_ERR_OR_NULL (node))) { + err = !node ? -ENOMEM :PTR_ERR (node); +SSDFS_ERR ("fail to read: " +"node %llu, err %d\n" , + (u64 )node_id, err); +return err; + } + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find node in radix tree: " +"node_id %llu, err %d\n" , + (u64 )node_id, err); +return err; + }else if (!node) { +SSDFS_WARN ("empty node pointer\n" ); +return -ERANGE ; + } + +ssdfs_btree_search_define_parent_node (search, parent_node); +ssdfs_btree_search_define_child_node (search, node); + +ssdfs_memcpy (&search->node.found_index, +0 ,sizeof (struct ssdfs_btree_index_key ), + &index_key, +0 ,sizeof (struct ssdfs_btree_index_key ), +sizeof (struct ssdfs_btree_index_key )); + + err =ssdfs_btree_node_convert_index2id (tree, search); +if (unlikely (err)) { +SSDFS_ERR ("fail to convert index to ID: " +"err %d\n" , err); +return err; + } + +if (!is_btree_leaf_node_found (search)) { +SSDFS_ERR ("leaf node hasn't been found\n" ); +return -ERANGE ; + } + + node = search->node.child; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node); +#endif /* CONFIG_SSDFS_DEBUG */ + +switch (atomic_read (&node->items_area.state)) { +case SSDFS_BTREE_NODE_ITEMS_AREA_EXIST : +/* expected state */ +break ; + +default : +SSDFS_WARN ("corrupted node %u\n" , + node->node_id); +return -ERANGE ; + } + +down_read (&node->header_lock); + start_hash = node->items_area.start_hash; + end_hash = node->items_area.end_hash; + items_count = node->items_area.items_count; + items_capacity = node->items_area.items_capacity; +up_read (&node->header_lock); + +if (start_hash ==U64_MAX || end_hash ==U64_MAX ) { +SSDFS_ERR ("invalid items area's hash range: " +"start_hash %llx, end_hash %llx\n" , + start_hash, end_hash); +return -ERANGE ; + } + + is_found = start_hash <= search->request.start.hash && + search->request.start.hash <= end_hash; + +if (!is_found && items_count >= items_capacity) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node (start_hash %llx, end_hash %llx), " +"request (start_hash %llx, end_hash %llx), " +"items_count %u, items_capacity %u\n" , + start_hash, end_hash, + search->request.start.hash, + search->request.end.hash, + items_count, items_capacity); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ENOENT ; + } + +return 0 ; +} + +/* +* ssdfs_btree_check_found_leaf_node() - check found leaf node +* @tree: btree object +* @search: search object [in|out] +*/ +static +int ssdfs_btree_check_found_leaf_node (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search) +{ +struct ssdfs_btree_node *node =NULL ; +u64 start_hash =U64_MAX , end_hash =U64_MAX ; +u16 items_count, items_capacity; +bool is_found = false; +bool is_right_adjacent = false; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree || !search); +BUG_ON (!rwsem_is_locked (&tree->lock)); + +SSDFS_DBG ("tree %p, start_hash %llx\n" , + tree, search->request.start.hash); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!is_btree_leaf_node_found (search)) { +SSDFS_ERR ("leaf node hasn't been found\n" ); +return -EINVAL ; + } + + node = search->node.child; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node); +#endif /* CONFIG_SSDFS_DEBUG */ + +switch (atomic_read (&node->items_area.state)) { +case SSDFS_BTREE_NODE_ITEMS_AREA_EXIST : +/* expected state */ +break ; + +default : +SSDFS_WARN ("corrupted node %u\n" , + node->node_id); +return -ERANGE ; + } + +down_read (&node->header_lock); + start_hash = node->items_area.start_hash; + end_hash = node->items_area.end_hash; + items_count = node->items_area.items_count; + items_capacity = node->items_area.items_capacity; +up_read (&node->header_lock); + +if (start_hash ==U64_MAX || end_hash ==U64_MAX ) { +SSDFS_ERR ("invalid items area's hash range: " +"start_hash %llx, end_hash %llx\n" , + start_hash, end_hash); +return -ERANGE ; + } + + is_found = start_hash <= search->request.start.hash && + search->request.start.hash <= end_hash; + is_right_adjacent = search->request.start.hash > end_hash; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node (start_hash %llx, end_hash %llx), " +"request (start_hash %llx, end_hash %llx), " +"is_found %#x, is_right_adjacent %#x, " +"items_count %u, items_capacity %u\n" , + start_hash, end_hash, + search->request.start.hash, + search->request.end.hash, + is_found, is_right_adjacent, + items_count, items_capacity); +#endif /* CONFIG_SSDFS_DEBUG */ + +switch (node->tree->type) { +case SSDFS_INODES_BTREE : +if (!is_found) { +SSDFS_DBG ("unable to find leaf node\n" ); + goto unable_find_leaf_node; + } +break ; + +default : +if (!is_found && items_count >= items_capacity) { +if (!is_right_adjacent) + goto unable_find_leaf_node; + + err =ssdfs_btree_find_right_sibling_leaf_node (tree, + node, + search); +if (err == -ENOENT ) { +SSDFS_DBG ("unable to find leaf node\n" ); + goto unable_find_leaf_node; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find leaf node: " +"node %u, err %d\n" , + node->node_id, err); +return err; + } + } +break ; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("found leaf node: " +"node_id %u, start_hash %llx, " +"end_hash %llx, search_hash %llx\n" , + node->node_id, start_hash, end_hash, + search->request.start.hash); +#endif /* CONFIG_SSDFS_DEBUG */ + +return 0 ; + +unable_find_leaf_node: + search->node.state =SSDFS_BTREE_SEARCH_FOUND_INDEX_NODE_DESC ; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to find a leaf node: " +"start_hash %llx, end_hash %llx, " +"search_hash %llx\n" , + start_hash, end_hash, + search->request.start.hash); +#endif /* CONFIG_SSDFS_DEBUG */ + +return -ENOENT ; +} + +/* +* ssdfs_btree_find_leaf_node() - find a leaf node in the tree +* @tree: btree object +* @search: search object [in|out] +* +* This method tries to find a leaf node for the requested +* start hash and end hash pair. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +* %-EEXIST - try the old search result. +* %-ENOENT - leaf node hasn't been found. +*/ +static +int ssdfs_btree_find_leaf_node (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search) +{ +struct ssdfs_btree_node *node; +u8 upper_height; +u8 prev_height; +u64 start_hash =U64_MAX , end_hash =U64_MAX ; +bool is_found = false; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree || !search); +BUG_ON (!rwsem_is_locked (&tree->lock)); + +SSDFS_DBG ("tree %p, start_hash %llx\n" , + tree, search->request.start.hash); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (search->node.state ==SSDFS_BTREE_SEARCH_FOUND_LEAF_NODE_DESC ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("try to use old search result: " +"node_id %llu, height %u\n" , + (u64 )search->node.id, search->node.height); +#endif /* CONFIG_SSDFS_DEBUG */ +return -EEXIST ; + } + +if (search->request.start.hash ==U64_MAX || + search->request.end.hash ==U64_MAX ) { +SSDFS_ERR ("invalid hash range in the request: " +"start_hash %llx, end_hash %llx\n" , + search->request.start.hash, + search->request.end.hash); +return -ERANGE ; + } + + upper_height =atomic_read (&tree->height); +if (upper_height <=0 ) { +SSDFS_ERR ("invalid tree height %u\n" , + upper_height); +return -ERANGE ; + }else + upper_height--; + + search->node.id =SSDFS_BTREE_ROOT_NODE_ID ; + search->node.height = upper_height; + search->node.state =SSDFS_BTREE_SEARCH_ROOT_NODE_DESC ; + +do { +unsigned long prev_id = search->node.id; +int node_height; +int node_type; + prev_height = search->node.height; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node_id %u, hash %llx\n" , + search->node.id, + search->request.start.hash); +#endif /* CONFIG_SSDFS_DEBUG */ + +ssdfs_btree_search_define_parent_node (search, + search->node.child); + + err =ssdfs_btree_radix_tree_find (tree, search->node.id, + &node); +if (err == -ENOENT ) { + err =0 ; + node =ssdfs_btree_read_node (tree, search); +if (unlikely (IS_ERR_OR_NULL (node))) { + err = !node ? -ENOMEM :PTR_ERR (node); +SSDFS_ERR ("fail to read: " +"node %llu, err %d\n" , + (u64 )search->node.id, err); + goto finish_search_leaf_node; + } + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find node in radix tree: " +"node_id %llu, err %d\n" , + (u64 )search->node.id, err); + goto finish_search_leaf_node; + }else if (!node) { + err = -ERANGE ; +SSDFS_WARN ("empty node pointer\n" ); + goto finish_search_leaf_node; + } + +ssdfs_btree_search_define_child_node (search, node); + node_height =atomic_read (&node->height); + +if (search->node.height != node_height) { + err = -ERANGE ; +SSDFS_WARN ("search->height %u != height %u\n" , + search->node.height, + node_height); + goto finish_search_leaf_node; + } + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (node_height >=U8_MAX ); +#endif /* CONFIG_SSDFS_DEBUG */ + + search->node.height = (u8 )node_height; + +if (node_height ==SSDFS_BTREE_LEAF_NODE_HEIGHT ) { +if (upper_height ==SSDFS_BTREE_LEAF_NODE_HEIGHT ) { +/* there is only root node */ + search->node.state = +SSDFS_BTREE_SEARCH_ROOT_NODE_DESC ; + }else { + search->node.state = +SSDFS_BTREE_SEARCH_FOUND_LEAF_NODE_DESC ; + } + goto check_found_node; + } + +down_read (&node->header_lock); + start_hash = node->index_area.start_hash; + end_hash = node->index_area.end_hash; +up_read (&node->header_lock); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node_id %u, start_hash %llx, " +"end_hash %llx\n" , + node->node_id, start_hash, + end_hash); +#endif /* CONFIG_SSDFS_DEBUG */ + + node_type =atomic_read (&node->type); +if (node_type ==SSDFS_BTREE_HYBRID_NODE ) { +switch (atomic_read (&node->items_area.state)) { +case SSDFS_BTREE_NODE_ITEMS_AREA_EXIST : +/* expected state */ +break ; + +default : + err = -ERANGE ; +SSDFS_WARN ("corrupted node %u\n" , + node->node_id); + goto finish_search_leaf_node; + } + +switch (atomic_read (&node->index_area.state)) { +case SSDFS_BTREE_NODE_INDEX_AREA_EXIST : +/* expected state */ +break ; + +default : + err = -ERANGE ; +SSDFS_WARN ("corrupted node %u\n" , + node->node_id); + goto finish_search_leaf_node; + } + +down_read (&node->header_lock); + start_hash = node->items_area.start_hash; + end_hash = node->items_area.end_hash; + is_found = start_hash <= search->request.start.hash && + search->request.start.hash <= end_hash; +up_read (&node->header_lock); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node_id %u, start_hash %llx, " +"end_hash %llx, is_found %#x\n" , + node->node_id, start_hash, + end_hash, is_found); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (start_hash <U64_MAX && end_hash ==U64_MAX ) { + err = -ERANGE ; +SSDFS_ERR ("invalid items area's hash range: " +"start_hash %llx, end_hash %llx\n" , + start_hash, end_hash); + goto finish_search_leaf_node; + } + +if (is_found) { + search->node.state = +SSDFS_BTREE_SEARCH_FOUND_LEAF_NODE_DESC ; + goto check_found_node; + }else if (search->request.start.hash > end_hash) { +/* +* Hybrid node is exausted already. +* It needs to use this node as +* starting point for adding a new node. +*/ +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node_id %u, " +"request.start.hash %llx, " +"end_hash %llx\n" , + node->node_id, + search->request.start.hash, + end_hash); +#endif /* CONFIG_SSDFS_DEBUG */ + + search->node.state = +SSDFS_BTREE_SEARCH_FOUND_LEAF_NODE_DESC ; + goto check_found_node; + } + } + +try_find_index: + err =ssdfs_btree_node_find_index (search); +if (err == -ENODATA ) { + err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to find node index: " +"node_state %#x, node_id %llu, " +"height %u\n" , + search->node.state, + (u64 )search->node.id, + search->node.height); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (upper_height ==0 ) { + search->node.state = +SSDFS_BTREE_SEARCH_ROOT_NODE_DESC ; + }else { + search->node.state = +SSDFS_BTREE_SEARCH_FOUND_LEAF_NODE_DESC ; + } + goto check_found_node; + }else if (err == -EACCES ) { + err =SSDFS_WAIT_COMPLETION (&node->init_end); +if (unlikely (err)) { +SSDFS_ERR ("node init failed: " +"err %d\n" , err); + goto finish_search_leaf_node; + }else + goto try_find_index; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find index: " +"start_hash %llx, err %d\n" , + search->request.start.hash, + err); + goto finish_search_leaf_node; + } + + err =ssdfs_btree_node_convert_index2id (tree, search); +if (unlikely (err)) { +SSDFS_ERR ("fail to convert index to ID: " +"err %d\n" , err); + goto finish_search_leaf_node; + } + + search->node.state =SSDFS_BTREE_SEARCH_FOUND_INDEX_NODE_DESC ; + +if (!is_btree_index_search_request_valid (search, + prev_id, + prev_height)) { + err = -ERANGE ; +SSDFS_ERR ("invalid index search request: " +"prev_id %llu, prev_height %u, " +"id %llu, height %u\n" , + (u64 )prev_id, prev_height, + (u64 )search->node.id, + search->node.height); + goto finish_search_leaf_node; + } + }while (prev_height >SSDFS_BTREE_LEAF_NODE_HEIGHT ); + +check_found_node: +if (search->node.state ==SSDFS_BTREE_SEARCH_ROOT_NODE_DESC ) { + err = -ENOENT ; +ssdfs_btree_search_define_parent_node (search, + search->node.child); +ssdfs_btree_search_define_child_node (search,NULL ); +SSDFS_DBG ("btree has empty root node\n" ); + goto finish_search_leaf_node; + }else if (is_btree_leaf_node_found (search)) { + err =ssdfs_btree_check_found_leaf_node (tree, search); +if (err) + goto finish_search_leaf_node; + }else { + err = -ENOENT ; +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("invalid leaf node descriptor: " +"node_state %#x, node_id %llu, " +"height %u\n" , + search->node.state, + (u64 )search->node.id, + search->node.height); +#endif /* CONFIG_SSDFS_DEBUG */ + } + +finish_search_leaf_node: +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node descriptor: " +"node_state %#x, node_id %llu, " +"height %u\n" , + search->node.state, + (u64 )search->node.id, + search->node.height); +#endif /* CONFIG_SSDFS_DEBUG */ + +return err; +} + +/* +* ssdfs_btree_add_node() - add a node into the btree +* @tree: btree object +* @search: search object [in|out] +* +* This method tries to add a node into the tree with the goal +* to increase capacity of items in the tree. It means that +* the new leaf node should be added into the tail of leaf +* nodes' chain. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +* %-EFAULT - tree is corrupted. +* %-ENOSPC - unable to add the new node. +*/ +int ssdfs_btree_add_node (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search) +{ +struct ssdfs_fs_info *fsi; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree || !tree->fsi || !search); + +SSDFS_DBG ("tree %p, start_hash %llx\n" , + tree, search->request.start.hash); +#endif /* CONFIG_SSDFS_DEBUG */ + +switch (atomic_read (&tree->state)) { +case SSDFS_BTREE_CREATED : +case SSDFS_BTREE_DIRTY : +/* expected state */ +break ; + +default : +#ifdef CONFIG_SSDFS_DEBUG +BUG (); +#else +SSDFS_WARN ("invalid tree state %#x\n" , +atomic_read (&tree->state)); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ERANGE ; + } + + fsi = tree->fsi; + + err =ssdfs_reserve_free_pages (fsi, tree->pages_per_node, +SSDFS_METADATA_PAGES ); +if (err) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to add the new node: " +"pages_per_node %u, err %d\n" , + tree->pages_per_node, err); +#endif /* CONFIG_SSDFS_DEBUG */ +return err; + } + +down_read (&tree->lock); + err =ssdfs_btree_find_leaf_node (tree, search); +up_read (&tree->lock); + +if (!err) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("found leaf node %u\n" , + search->node.id); +#endif /* CONFIG_SSDFS_DEBUG */ +return ssdfs_check_leaf_node_state (search); + }else if (err == -ENOENT ) { +/* +* Parent node was found. +*/ + err =0 ; + }else { + err = -ERANGE ; +SSDFS_ERR ("fail to define the parent node: " +"hash %llx, err %d\n" , + search->request.start.hash, + err); +return err; + } + + err =__ssdfs_btree_add_node (tree, search); +if (err == -EEXIST ) { +SSDFS_DBG ("node has been added\n" ); + }else if (err == -ENOSPC ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to add a new node: err %d\n" , + err); +#endif /* CONFIG_SSDFS_DEBUG */ + }else if (unlikely (err)) { +SSDFS_ERR ("fail to add a new node: err %d\n" , + err); + } + +ssdfs_debug_btree_object (tree); +ssdfs_check_btree_consistency (tree); + +return err; +} + +/* +* ssdfs_btree_insert_node() - insert a node into the btree +* @tree: btree object +* @search: search object [in|out] +* +* This method tries to insert a node into the tree for +* the requested hash value. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +* %-EFAULT - tree is corrupted. +* %-ENOSPC - unable to insert the new node. +*/ +int ssdfs_btree_insert_node (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search) +{ +struct ssdfs_fs_info *fsi; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree || !tree->fsi || !search); + +SSDFS_DBG ("tree %p, start_hash %llx\n" , + tree, search->request.start.hash); +#endif /* CONFIG_SSDFS_DEBUG */ + +switch (atomic_read (&tree->state)) { +case SSDFS_BTREE_CREATED : +case SSDFS_BTREE_DIRTY : +/* expected state */ +break ; + +default : +#ifdef CONFIG_SSDFS_DEBUG +BUG (); +#else +SSDFS_WARN ("invalid tree state %#x\n" , +atomic_read (&tree->state)); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ERANGE ; + } + + fsi = tree->fsi; + + err =ssdfs_reserve_free_pages (fsi, tree->pages_per_node, +SSDFS_METADATA_PAGES ); +if (err) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to add the new node: " +"pages_per_node %u, err %d\n" , + tree->pages_per_node, err); +#endif /* CONFIG_SSDFS_DEBUG */ +return err; + } + + err =__ssdfs_btree_add_node (tree, search); +if (err == -EEXIST ) +SSDFS_DBG ("node has been added\n" ); +else if (unlikely (err)) { +SSDFS_ERR ("fail to add a new node: err %d\n" , + err); + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("finished\n" ); + +ssdfs_debug_btree_object (tree); +#endif /* CONFIG_SSDFS_DEBUG */ + +ssdfs_check_btree_consistency (tree); + +return err; +} + +/* +* ssdfs_segment_invalidate_node() - invalidate the node in the segment +* @node: node object +* +* This method tries to invalidate the node +* in the current segment. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +* %-EFAULT - tree is corrupted. +*/ +static +int ssdfs_segment_invalidate_node (struct ssdfs_btree_node *node) +{ +struct ssdfs_segment_info *seg; +u32 start_blk; +u32 len; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node); +#endif /* CONFIG_SSDFS_DEBUG */ + +spin_lock (&node->descriptor_lock); + start_blk =le32_to_cpu (node->extent.logical_blk); + len =le32_to_cpu (node->extent.len); + seg = node->seg; +spin_unlock (&node->descriptor_lock); + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!seg); + +SSDFS_DBG ("node_id %u, seg_id %llu, start_blk %u, len %u\n" , + node->node_id, seg->seg_id, start_blk, len); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_segment_invalidate_logical_extent (seg, start_blk, len); +if (unlikely (err)) { +SSDFS_ERR ("fail to invalidate node: " +"node_id %u, seg_id %llu, " +"start_blk %u, len %u\n" , + node->node_id, seg->seg_id, + start_blk, len); + } + +return 0 ; +} + +/* +* ssdfs_btree_delete_index_in_parent_node() - delete index in parent node +* @tree: btree object +* @search: search object +* +* This method tries to delete the index records in all parent nodes. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +* %-EFAULT - tree is corrupted. +*/ +static +int ssdfs_btree_delete_index_in_parent_node (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search) +{ +struct ssdfs_btree_hierarchy *hierarchy; +struct ssdfs_btree_level *level; +struct ssdfs_btree_node *node; +int cur_height, tree_height; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree || !tree->fsi || !search); + +SSDFS_DBG ("tree %p, start_hash %llx\n" , + tree, search->request.start.hash); +#endif /* CONFIG_SSDFS_DEBUG */ + +switch (atomic_read (&tree->state)) { +case SSDFS_BTREE_CREATED : +case SSDFS_BTREE_DIRTY : +/* expected state */ +break ; + +default : +#ifdef CONFIG_SSDFS_DEBUG +BUG (); +#else +SSDFS_WARN ("invalid tree state %#x\n" , +atomic_read (&tree->state)); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ERANGE ; + } + +switch (search->node.state) { +case SSDFS_BTREE_SEARCH_FOUND_LEAF_NODE_DESC : +/* expected state */ +break ; + +default : +SSDFS_ERR ("invalid node state %#x\n" , + search->node.state); +return -ERANGE ; + } + +if (!search->node.child) { +SSDFS_ERR ("child node is NULL\n" ); +return -ERANGE ; + } + +if (!search->node.parent) { +SSDFS_ERR ("parent node is NULL\n" ); +return -ERANGE ; + } + + tree_height =atomic_read (&tree->height); +if (tree_height <=0 ) { +SSDFS_ERR ("invalid tree_height %u\n" , + tree_height); +return -ERANGE ; + } + + hierarchy =ssdfs_btree_hierarchy_allocate (tree); +if (IS_ERR_OR_NULL (hierarchy)) { + err = !hierarchy ? -ENOMEM :PTR_ERR (hierarchy); +SSDFS_ERR ("fail to allocate tree levels' array: " +"err %d\n" , err); +return err; + } + + err =ssdfs_btree_check_hierarchy_for_delete (tree, search, hierarchy); +if (unlikely (err)) { +SSDFS_ERR ("fail to prepare information about hierarchy: " +"err %d\n" , + err); + goto finish_delete_index; + } + +for (cur_height =0 ; cur_height < tree_height; cur_height++) { + err =ssdfs_btree_process_level_for_delete (hierarchy, + cur_height, + search); +if (unlikely (err)) { +SSDFS_ERR ("fail to process the tree's level: " +"cur_height %u, err %d\n" , + cur_height, err); + goto finish_delete_index; + } + } + +for (cur_height =0 ; cur_height < (tree_height -1 ); cur_height++) { + level = hierarchy->array_ptr[cur_height]; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("cur_height %d, tree_height %d\n" , + cur_height, tree_height); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!need_delete_node (level)) +continue ; + + node = level->nodes.old_node.ptr; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node); +SSDFS_DBG ("node_id %u\n" , node->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ + +set_ssdfs_btree_node_pre_deleted (node); + + err =ssdfs_segment_invalidate_node (node); +if (unlikely (err)) { +SSDFS_ERR ("fail to invalidate node: id %u, err %d\n" , + node->node_id, err); + } + } + +atomic_set (&tree->state,SSDFS_BTREE_DIRTY ); +ssdfs_btree_hierarchy_free (hierarchy); + +ssdfs_btree_search_define_child_node (search,NULL ); + search->result.err =0 ; + search->result.state =SSDFS_BTREE_SEARCH_UNKNOWN_RESULT ; +return 0 ; + +finish_delete_index: +ssdfs_btree_hierarchy_free (hierarchy); + + search->result.err = err; + search->result.state =SSDFS_BTREE_SEARCH_FAILURE ; +return err; +} + +/* +* ssdfs_btree_delete_node() - delete the node from the btree +* @tree: btree object +* @search: search object [in|out] +* +* This method tries to delete a node from the tree. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +* %-EFAULT - cannot delete the node. +* %-EBUSY - node has several owners. +*/ +int ssdfs_btree_delete_node (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search) +{ +struct ssdfs_btree_node *node; +u16 items_count; +u16 items_capacity; +u16 index_count; +u16 index_capacity; +bool cannot_delete = false; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree || !search); + +SSDFS_DBG ("tree %p, start_hash %llx\n" , + tree, search->request.start.hash); +#endif /* CONFIG_SSDFS_DEBUG */ + +switch (atomic_read (&tree->state)) { +case SSDFS_BTREE_CREATED : +case SSDFS_BTREE_DIRTY : +/* expected state */ +break ; + +default : +#ifdef CONFIG_SSDFS_DEBUG +BUG (); +#else +SSDFS_WARN ("invalid tree state %#x\n" , +atomic_read (&tree->state)); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ERANGE ; + } + +if (search->result.state !=SSDFS_BTREE_SEARCH_PLEASE_DELETE_NODE ) { +SSDFS_ERR ("invalid search->result.state %#x\n" , + search->result.state); +return -ERANGE ; + } + +switch (search->node.state) { +case SSDFS_BTREE_SEARCH_FOUND_INDEX_NODE_DESC : +case SSDFS_BTREE_SEARCH_FOUND_LEAF_NODE_DESC : +/* expected state */ +break ; + +case SSDFS_BTREE_SEARCH_ROOT_NODE_DESC : +SSDFS_ERR ("fail to delete root node\n" ); +return -ERANGE ; + +default : +BUG (); + } + +if (!search->node.child) { +SSDFS_ERR ("child node pointer is NULL\n" ); +return -ERANGE ; + } + +if (!search->node.parent) { +SSDFS_ERR ("parent node pointer is NULL\n" ); +return -ERANGE ; + } + + node = search->node.child; + +if (node->node_id != search->node.id || +atomic_read (&node->height) != search->node.height) { +SSDFS_ERR ("corrupted search object: " +"node->node_id %u, search->node.id %u, " +"node->height %u, search->node.height %u\n" , + node->node_id, search->node.id, +atomic_read (&node->height), + search->node.height); +return -ERANGE ; + }
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.34.1
Part of the diff is not shown here. View the raw message.
RFC PATCH 49/76 ssdfs: b-tree API implementation
In reply to Viacheslav DubeykoGeneralized b-tree functionality implements API:
(1) create - create empty b-tree object
(2) destroy - destroy b-tree object
(3) flush - flush dirty b-tree object
(4) find_item - find item in b-tree hierarchy
(5) find_range - find range of items in b-tree hierarchy
(6) allocate_item - allocate item in existing b-tree's node
(7) allocate_range - allocate range of items in existing b-tree's node
(8) add_item - add item into b-tree
(9) add_range - add range of items into b-tree
(10) change_item - change existing item in b-tree
(11) delete_item - delete item from b-tree
(12) delete_range - delete range of items from b-tree
(13) delete_all - delete all items from b-tree
--- fs/ssdfs/btree.c | 3713 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 3713 insertions(+)
fs/ssdfs/btree.c +3713 -0
@@ -4072,3 +4072,3716 @@ int ssdfs_btree_delete_node(struct ssdfs_btree *tree,return err; } + +/* +* node_needs_in_additional_check() - does it need to check the node? +* @err: error code +* @search: search object +*/ +static inline +bool node_needs_in_additional_check (int err, +struct ssdfs_btree_search *search) +{ +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!search); +#endif /* CONFIG_SSDFS_DEBUG */ + +return err == -ENODATA && + search->result.state ==SSDFS_BTREE_SEARCH_PLEASE_ADD_NODE ; +} + +/* +* ssdfs_btree_switch_on_hybrid_parent_node() - change current node +* @tree: btree object +* @search: search object [in|out] +* +* This method tries to change the current node on hybrid parent one. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +* %-ENODATA - item can be added. +* %-ENOENT - no space for a new item. +*/ +static +int ssdfs_btree_switch_on_hybrid_parent_node (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search) +{ +struct ssdfs_btree_node *node; +int state; +u64 start_hash, end_hash; +u16 items_count, items_capacity; +u16 free_items; +u16 flags; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree || !search); + +SSDFS_DBG ("tree %p, type %#x, " +"request->type %#x, request->flags %#x, " +"result->err %d, result->state %#x, " +"start_hash %llx, end_hash %llx\n" , + tree, tree->type, + search->request.type, search->request.flags, + search->result.err, + search->result.state, + search->request.start.hash, + search->request.end.hash); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (search->result.err != -ENODATA ) { +SSDFS_ERR ("unexpected result's error %d\n" , + search->result.err); +return -ERANGE ; + } + +if (search->result.state !=SSDFS_BTREE_SEARCH_PLEASE_ADD_NODE ) { +SSDFS_ERR ("unexpected result's state %#x\n" , + search->result.state); +return -ERANGE ; + } + +if (search->request.start.hash ==U64_MAX || + search->request.end.hash ==U64_MAX ) { +SSDFS_ERR ("invalid request: " +"start_hash %llx, end_hash %llx\n" , + search->request.start.hash, + search->request.end.hash); +return -ERANGE ; + } + + node = search->node.child; +if (!node) { + node = search->node.parent; + +if (!node) { +SSDFS_ERR ("corrupted search request: " +"child and parent nodes are NULL\n" ); +return -ERANGE ; + } + +if (atomic_read (&node->type) ==SSDFS_BTREE_ROOT_NODE ) { +SSDFS_DBG ("parent is root node\n" ); +return -ENOENT ; + }else { +SSDFS_ERR ("corrupted search request: " +"child nodes is NULL\n" ); +return -ERANGE ; + } + } + +if (atomic_read (&node->type) ==SSDFS_BTREE_ROOT_NODE ) { +SSDFS_DBG ("child is root node\n" ); +return -ENOENT ; + } + + state =atomic_read (&node->items_area.state); +if (state !=SSDFS_BTREE_NODE_ITEMS_AREA_EXIST ) { +SSDFS_ERR ("invalid items area's state %#x\n" , + state); +return -ERANGE ; + } + +down_read (&node->header_lock); + items_count = node->items_area.items_count; + items_capacity = node->items_area.items_capacity; + start_hash = node->items_area.start_hash; + end_hash = node->items_area.end_hash; +up_read (&node->header_lock); + +if (start_hash ==U64_MAX || end_hash ==U64_MAX ) { +SSDFS_ERR ("corrupted items area: " +"start_hash %llx, end_hash %llx\n" , + start_hash, end_hash); +return -ERANGE ; + } + +if (start_hash > end_hash) { +SSDFS_ERR ("corrupted items area: " +"start_hash %llx, end_hash %llx\n" , + start_hash, end_hash); +return -ERANGE ; + } + +if (items_count > items_capacity) { +SSDFS_ERR ("corrupted items area: " +"items_count %u, items_capacity %u\n" , + items_count, items_capacity); +return -ERANGE ; + } + + free_items = items_capacity - items_count; + +if (free_items !=0 ) { +SSDFS_WARN ("invalid free_items %u, " +"items_count %u, items_capacity %u\n" , + free_items, items_count, items_capacity); +return -ERANGE ; + } + + node = search->node.parent; +if (!node) { +SSDFS_ERR ("corrupted search request: parent node is NULL\n" ); +return -ERANGE ; + } + +switch (atomic_read (&node->type)) { +case SSDFS_BTREE_ROOT_NODE : +case SSDFS_BTREE_INDEX_NODE : +/* nothing can be done */ +SSDFS_DBG ("node is root or index\n" ); +return -ENOENT ; + +case SSDFS_BTREE_HYBRID_NODE : +/* it needs to check the node's state */ +break ; + +case SSDFS_BTREE_LEAF_NODE : +SSDFS_WARN ("btree is corrupted: " +"leaf node %u cannot be the parent\n" , + node->node_id); +return -ERANGE ; + +default : +SSDFS_ERR ("invalid node's type %#x\n" , +atomic_read (&node->type)); +return -ERANGE ; + } + +switch (atomic_read (&node->state)) { +case SSDFS_BTREE_NODE_INITIALIZED : +case SSDFS_BTREE_NODE_DIRTY : +/* expected state */ +break ; + +default : +SSDFS_WARN ("invalid node's %u state %#x\n" , + node->node_id, +atomic_read (&node->state)); +return -ERANGE ; + } + + flags =atomic_read (&node->flags); + +if (!(flags &SSDFS_BTREE_NODE_HAS_ITEMS_AREA )) { +SSDFS_WARN ("hybrid node %u hasn't items area\n" , + node->node_id); +return -ENOENT ; + } + +ssdfs_btree_search_define_child_node (search, node); + +spin_lock (&node->descriptor_lock); +ssdfs_btree_search_define_parent_node (search, node->parent_node); +spin_unlock (&node->descriptor_lock); + +ssdfs_memcpy (&search->node.found_index, +0 ,sizeof (struct ssdfs_btree_index_key ), + &node->node_index, +0 ,sizeof (struct ssdfs_btree_index_key ), +sizeof (struct ssdfs_btree_index_key )); + + err =ssdfs_btree_node_convert_index2id (tree, search); +if (unlikely (err)) { +SSDFS_ERR ("fail to convert index to ID: " +"node %u, height %u\n" , + node->node_id, +atomic_read (&node->height)); +return err; + } + +down_read (&node->header_lock); + items_count = node->items_area.items_count; + items_capacity = node->items_area.items_capacity; + start_hash = node->items_area.start_hash; + end_hash = node->items_area.end_hash; +up_read (&node->header_lock); + + free_items = items_capacity - items_count; + +if (free_items ==0 ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node %u is exhausted: free_items %u, " +"items_count %u, items_capacity %u\n" , + node->node_id, + free_items, items_count, items_capacity); +#endif /* CONFIG_SSDFS_DEBUG */ + search->result.state =SSDFS_BTREE_SEARCH_PLEASE_ADD_NODE ; +return -ENOENT ; + } + +if (items_count ==0 ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node %u is empty: free_items %u, " +"items_count %u, items_capacity %u\n" , + node->node_id, + free_items, items_count, items_capacity); +#endif /* CONFIG_SSDFS_DEBUG */ + goto finish_node_check; + } + +if (search->request.start.hash < start_hash) { +if (search->request.end.hash < start_hash) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("request (start_hash %llx, end_hash %llx), " +"area (start_hash %llx, end_hash %llx)\n" , + search->request.start.hash, + search->request.end.hash, + start_hash, end_hash); +#endif /* CONFIG_SSDFS_DEBUG */ + goto finish_node_check; + }else { +SSDFS_ERR ("invalid request: " +"request (start_hash %llx, end_hash %llx), " +"area (start_hash %llx, end_hash %llx)\n" , + search->request.start.hash, + search->request.end.hash, + start_hash, end_hash); +return -ERANGE ; + } + } + +finish_node_check: + search->node.state =SSDFS_BTREE_SEARCH_FOUND_LEAF_NODE_DESC ; + search->result.state =SSDFS_BTREE_SEARCH_OUT_OF_RANGE ; + search->result.err = -ENODATA ; + +if (items_count ==0 ) + search->result.start_index =0 ; +else + search->result.start_index = items_count; + + search->result.count = search->request.count; + search->result.search_cno =ssdfs_current_cno (tree->fsi->sb); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node_id %u, items_count %u, items_capacity %u, " +"start_hash %llx, end_hash %llx\n" , + node->node_id, items_count, items_capacity, + start_hash, end_hash); +#endif /* CONFIG_SSDFS_DEBUG */ + +return -ENODATA ; +} + +/* +* __ssdfs_btree_find_item() - find item into btree +* @tree: btree object +* @search: search object [in|out] +* +* This method tries to find an item into the tree. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +* %-ENODATA - item hasn't been found +* %-ENOSPC - node hasn't free space. +*/ +static +int __ssdfs_btree_find_item (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search) +{ +int tree_state; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree || !search); +BUG_ON (!rwsem_is_locked (&tree->lock)); +#endif /* CONFIG_SSDFS_DEBUG */ + + tree_state =atomic_read (&tree->state); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("tree %p, type %#x, state %#x, " +"request->type %#x, request->flags %#x, " +"start_hash %llx, end_hash %llx\n" , + tree, tree->type, tree_state, + search->request.type, search->request.flags, + search->request.start.hash, + search->request.end.hash); +#endif /* CONFIG_SSDFS_DEBUG */ + +switch (tree_state) { +case SSDFS_BTREE_CREATED : +case SSDFS_BTREE_DIRTY : +/* expected state */ +break ; + +default : +#ifdef CONFIG_SSDFS_DEBUG +BUG (); +#else +SSDFS_WARN ("invalid tree state %#x\n" , + tree_state); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ERANGE ; + } + +if (!is_btree_search_request_valid (search)) { +SSDFS_ERR ("invalid search object\n" ); +return -EINVAL ; + } + +switch (search->request.type) { +case SSDFS_BTREE_SEARCH_FIND_ITEM : +case SSDFS_BTREE_SEARCH_ALLOCATE_ITEM : +case SSDFS_BTREE_SEARCH_ALLOCATE_RANGE : +case SSDFS_BTREE_SEARCH_ADD_ITEM : +case SSDFS_BTREE_SEARCH_ADD_RANGE : +case SSDFS_BTREE_SEARCH_CHANGE_ITEM : +case SSDFS_BTREE_SEARCH_DELETE_ITEM : +case SSDFS_BTREE_SEARCH_INVALIDATE_TAIL : +/* expected state */ +break ; + +default : +SSDFS_ERR ("invalid request type %#x\n" , + search->request.type); +return -EINVAL ; + } + + err =ssdfs_btree_find_leaf_node (tree, search); +if (err == -EEXIST ) { + err =0 ; +/* try to find an item */ + }else if (err == -ENOENT ) { + err = -ENODATA ; + search->result.err = -ENODATA ; + search->result.state =SSDFS_BTREE_SEARCH_PLEASE_ADD_NODE ; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("index node was found\n" ); +#endif /* CONFIG_SSDFS_DEBUG */ + +switch (search->request.type) { +case SSDFS_BTREE_SEARCH_ADD_ITEM : + err =ssdfs_btree_switch_on_hybrid_parent_node (tree, + search); +if (err == -ENODATA ) + goto finish_search_item; +else if (err == -ENOENT ) { + err = -ENODATA ; + goto finish_search_item; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to switch on parent node: " +"err %d\n" , err); + }else { + err = -ENODATA ; + goto finish_search_item; + } +break ; + +default : +/* do nothing */ +break ; + } + + goto finish_search_item; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find leaf node: err %d\n" , + err); + goto finish_search_item; + } + +if (search->request.type ==SSDFS_BTREE_SEARCH_ADD_ITEM ) { +try_another_node: + err =ssdfs_btree_node_find_item (search); +if (node_needs_in_additional_check (err, search)) { + search->result.err = -ENODATA ; + err =ssdfs_btree_switch_on_hybrid_parent_node (tree, + search); +if (err == -ENODATA ) + goto finish_search_item; +else if (err == -ENOENT ) { + err = -ENODATA ; + goto finish_search_item; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to switch on parent node: " +"err %d\n" , err); + goto finish_search_item; + }else { + err = -ENODATA ; + goto finish_search_item; + } + }else if (err == -EACCES ) { +struct ssdfs_btree_node *node = search->node.child; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =SSDFS_WAIT_COMPLETION (&node->init_end); +if (unlikely (err)) { +SSDFS_ERR ("node init failed: " +"err %d\n" , err); + goto finish_search_item; + }else + goto try_another_node; + } + }else { +try_find_item_again: + err =ssdfs_btree_node_find_item (search); +if (err == -EACCES ) { +struct ssdfs_btree_node *node = search->node.child; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =SSDFS_WAIT_COMPLETION (&node->init_end); +if (unlikely (err)) { +SSDFS_ERR ("node init failed: " +"err %d\n" , err); + goto finish_search_item; + }else + goto try_find_item_again; + } + } + +if (err == -EAGAIN ) { +if (search->result.items_in_buffer >0 && + search->result.state ==SSDFS_BTREE_SEARCH_VALID_ITEM ) { +/* finish search */ + err =0 ; + search->result.err =0 ; + goto finish_search_item; + }else { + err = -ENODATA ; +SSDFS_DBG ("node hasn't requested data\n" ); + goto finish_search_item; + } + }else if (err == -ENODATA ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to find item: " +"start_hash %llx, end_hash %llx\n" , + search->request.start.hash, + search->request.end.hash); +#endif /* CONFIG_SSDFS_DEBUG */ + goto finish_search_item; + }else if (err == -ENOSPC ) { + err = -ENODATA ; + search->result.state =SSDFS_BTREE_SEARCH_PLEASE_ADD_NODE ; +SSDFS_DBG ("index node was found\n" ); + goto finish_search_item; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find item: " +"start_hash %llx, end_hash %llx, " +"err %d\n" , + search->request.start.hash, + search->request.end.hash, + err); + goto finish_search_item; + } + +finish_search_item: +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("search->result.state %#x, err %d\n" , + search->result.state, err); +#endif /* CONFIG_SSDFS_DEBUG */ + +return err; +} + +/* +* ssdfs_btree_find_item() - find item into btree +* @tree: btree object +* @search: search object [in|out] +* +* This method tries to find an item into the tree. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +* %-ENODATA - item hasn't been found +*/ +int ssdfs_btree_find_item (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search) +{ +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree || !search); + +SSDFS_DBG ("tree %p, type %#x, " +"request->type %#x, request->flags %#x, " +"start_hash %llx, end_hash %llx\n" , + tree, tree->type, + search->request.type, search->request.flags, + search->request.start.hash, + search->request.end.hash); +#endif /* CONFIG_SSDFS_DEBUG */ + +down_read (&tree->lock); + err =__ssdfs_btree_find_item (tree, search); +up_read (&tree->lock); + +return err; +} + +/* +* __ssdfs_btree_find_range() - find a range of items into btree +* @tree: btree object +* @search: search object [in|out] +* +* This method tries to find a range of item into the tree. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +*/ +static +int __ssdfs_btree_find_range (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search) +{ +int tree_state; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree || !search); +BUG_ON (!rwsem_is_locked (&tree->lock)); +#endif /* CONFIG_SSDFS_DEBUG */ + + tree_state =atomic_read (&tree->state); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("tree %p, type %#x, state %#x, " +"request->type %#x, request->flags %#x, " +"start_hash %llx, end_hash %llx\n" , + tree, tree->type, tree_state, + search->request.type, search->request.flags, + search->request.start.hash, + search->request.end.hash); +#endif /* CONFIG_SSDFS_DEBUG */ + +switch (tree_state) { +case SSDFS_BTREE_CREATED : +case SSDFS_BTREE_DIRTY : +/* expected state */ +break ; + +default : +#ifdef CONFIG_SSDFS_DEBUG +BUG (); +#else +SSDFS_WARN ("invalid tree state %#x\n" , + tree_state); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ERANGE ; + } + +if (!is_btree_search_request_valid (search)) { +SSDFS_ERR ("invalid search object\n" ); +return -EINVAL ; + } + +switch (search->request.type) { +case SSDFS_BTREE_SEARCH_FIND_RANGE : +case SSDFS_BTREE_SEARCH_ADD_RANGE : +case SSDFS_BTREE_SEARCH_DELETE_RANGE : +case SSDFS_BTREE_SEARCH_INVALIDATE_TAIL : +/* expected state */ +break ; + +default : +SSDFS_ERR ("invalid request type %#x\n" , + search->request.type); +return -EINVAL ; + } + +try_next_search: + err =ssdfs_btree_find_leaf_node (tree, search); +if (err == -EEXIST ) { + err =0 ; +/* try to find an item */ + }else if (err == -ENOENT ) { + err = -ENODATA ; + search->result.err = -ENODATA ; + search->result.state =SSDFS_BTREE_SEARCH_PLEASE_ADD_NODE ; +SSDFS_DBG ("index node was found\n" ); + +switch (search->request.type) { +case SSDFS_BTREE_SEARCH_ADD_ITEM : + err =ssdfs_btree_switch_on_hybrid_parent_node (tree, + search); +if (err == -ENODATA ) { +/* +* do nothing +*/ + }else if (unlikely (err)) { +SSDFS_ERR ("fail to switch on parent node: " +"err %d\n" , err); + }else { +/* finish search */ + err = -ENODATA ; + } +break ; + +default : +/* do nothing */ +break ; + } + + goto finish_search_range; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find leaf node: err %d\n" , + err); + goto finish_search_range; + } + +if (search->request.type ==SSDFS_BTREE_SEARCH_ADD_RANGE ) { +try_another_node: + err =ssdfs_btree_node_find_range (search); + +if (node_needs_in_additional_check (err, search)) { + search->result.err = -ENODATA ; + err =ssdfs_btree_switch_on_hybrid_parent_node (tree, + search); +if (err == -ENODATA ) + goto finish_search_range; +else if (unlikely (err)) { +SSDFS_ERR ("fail to switch on parent node: " +"err %d\n" , err); + goto finish_search_range; + }else { +/* finish search */ + err = -ENODATA ; + goto finish_search_range; + } + }else if (err == -EACCES ) { +struct ssdfs_btree_node *node = search->node.child; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =SSDFS_WAIT_COMPLETION (&node->init_end); +if (unlikely (err)) { +SSDFS_ERR ("node init failed: " +"err %d\n" , err); + goto finish_search_range; + }else + goto try_another_node; + } + }else { +try_find_range_again: + err =ssdfs_btree_node_find_range (search); +if (err == -EACCES ) { +struct ssdfs_btree_node *node = search->node.child; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =SSDFS_WAIT_COMPLETION (&node->init_end); +if (unlikely (err)) { +SSDFS_ERR ("node init failed: " +"err %d\n" , err); + goto finish_search_range; + }else + goto try_find_range_again; + } + } + +if (err == -EAGAIN ) { +if (search->result.items_in_buffer >0 && + search->result.state ==SSDFS_BTREE_SEARCH_VALID_ITEM ) { +/* finish search */ + err =0 ; + search->result.err =0 ; + goto finish_search_range; + }else { + err =0 ; + search->node.state =SSDFS_BTREE_SEARCH_NODE_DESC_EMPTY ; +SSDFS_DBG ("try next search\n" ); + goto try_next_search; + } + }else if (err == -ENODATA ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to find range: " +"start_hash %llx, end_hash %llx\n" , + search->request.start.hash, + search->request.end.hash); +#endif /* CONFIG_SSDFS_DEBUG */ + goto finish_search_range; + }else if (err == -ENOSPC ) { + err = -ENODATA ; + search->result.state =SSDFS_BTREE_SEARCH_PLEASE_ADD_NODE ; +SSDFS_DBG ("index node was found\n" ); + goto finish_search_range; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find range: " +"start_hash %llx, end_hash %llx, " +"err %d\n" , + search->request.start.hash, + search->request.end.hash, + err); + goto finish_search_range; + } + +finish_search_range: +return err; +} + +/* +* ssdfs_btree_find_range() - find a range of items into btree +* @tree: btree object +* @search: search object [in|out] +* +* This method tries to find a range of item into the tree. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +*/ +int ssdfs_btree_find_range (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search) +{ +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree || !search); + +SSDFS_DBG ("tree %p, type %#x, " +"request->type %#x, request->flags %#x, " +"start_hash %llx, end_hash %llx\n" , + tree, tree->type, + search->request.type, search->request.flags, + search->request.start.hash, + search->request.end.hash); +#endif /* CONFIG_SSDFS_DEBUG */ + +down_read (&tree->lock); + err =__ssdfs_btree_find_range (tree, search); +up_read (&tree->lock); + +return err; +} + +/* +* ssdfs_btree_allocate_item() - allocate item into btree +* @tree: btree object +* @search: search object [in|out] +* +* This method tries to allocate the item into the tree. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +*/ +int ssdfs_btree_allocate_item (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search) +{ +int tree_state; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree || !search); +#endif /* CONFIG_SSDFS_DEBUG */ + + tree_state =atomic_read (&tree->state); + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("tree %p, type %#x, state %#x, " +"request->type %#x, request->flags %#x, " +"start_hash %llx, end_hash %llx\n" , + tree, tree->type, tree_state, + search->request.type, search->request.flags, + search->request.start.hash, + search->request.end.hash); +#else +SSDFS_DBG ("tree %p, type %#x, state %#x, " +"request->type %#x, request->flags %#x, " +"start_hash %llx, end_hash %llx\n" , + tree, tree->type, tree_state, + search->request.type, search->request.flags, + search->request.start.hash, + search->request.end.hash); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + +switch (tree_state) { +case SSDFS_BTREE_CREATED : +case SSDFS_BTREE_DIRTY : +/* expected state */ +break ; + +default : +#ifdef CONFIG_SSDFS_DEBUG +BUG (); +#else +SSDFS_WARN ("invalid tree state %#x\n" , + tree_state); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ERANGE ; + } + +if (!is_btree_search_request_valid (search)) { +SSDFS_ERR ("invalid search object\n" ); +return -EINVAL ; + } + +if (search->request.type !=SSDFS_BTREE_SEARCH_ALLOCATE_ITEM ) { +SSDFS_ERR ("invalid request type %#x\n" , + search->request.type); +return -EINVAL ; + } + +down_read (&tree->lock); + +try_next_search: + err =ssdfs_btree_find_leaf_node (tree, search); +if (err == -EEXIST ) { + err =0 ; +/* try the old search result */ + }else if (err == -ENOENT ) { + err = -ENODATA ; + search->result.err = -ENODATA ; + search->result.state =SSDFS_BTREE_SEARCH_PLEASE_ADD_NODE ; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("index node was found\n" ); +#endif /* CONFIG_SSDFS_DEBUG */ + +up_read (&tree->lock); + err =ssdfs_btree_insert_node (tree, search); +down_read (&tree->lock); + +if (unlikely (err)) { +SSDFS_ERR ("fail to insert node: err %d\n" , + err); + goto finish_allocate_item; + } + + err =ssdfs_btree_find_leaf_node (tree, search); +if (err == -EEXIST ) { + err =0 ; +/* try the old search result */ + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find leaf node: err %d\n" , + err); + goto finish_allocate_item; + } + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find leaf node: err %d\n" , + err); + goto finish_allocate_item; + } + +try_allocate_item: + err =ssdfs_btree_node_allocate_item (search); +if (err == -EAGAIN ) { + err =0 ; + search->node.state =SSDFS_BTREE_SEARCH_NODE_DESC_EMPTY ; + goto try_next_search; + }else if (err == -EACCES ) { +struct ssdfs_btree_node *node = search->node.child; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =SSDFS_WAIT_COMPLETION (&node->init_end); +if (unlikely (err)) { +SSDFS_ERR ("node init failed: " +"err %d\n" , err); + goto finish_allocate_item; + }else + goto try_allocate_item; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to allocate item: " +"start_hash %llx, end_hash %llx, " +"err %d\n" , + search->request.start.hash, + search->request.end.hash, + err); + goto finish_allocate_item; + } + +atomic_set (&tree->state,SSDFS_BTREE_DIRTY ); + +finish_allocate_item: +up_read (&tree->lock); + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("finished\n" ); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + +ssdfs_debug_btree_object (tree); + +#ifdef CONFIG_SSDFS_BTREE_STRICT_CONSISTENCY_CHECK +ssdfs_check_btree_consistency (tree); +#endif /* CONFIG_SSDFS_BTREE_STRICT_CONSISTENCY_CHECK */ + +return err; +} + +/* +* ssdfs_btree_allocate_range() - allocate range of items into btree +* @tree: btree object +* @search: search object [in|out] +* +* This method tries to allocate the range of items into the tree. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +*/ +int ssdfs_btree_allocate_range (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search) +{ +int tree_state; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree || !search); +#endif /* CONFIG_SSDFS_DEBUG */ + + tree_state =atomic_read (&tree->state); + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("tree %p, type %#x, state %#x, " +"request->type %#x, request->flags %#x, " +"start_hash %llx, end_hash %llx\n" , + tree, tree->type, tree_state, + search->request.type, search->request.flags, + search->request.start.hash, + search->request.end.hash); +#else +SSDFS_DBG ("tree %p, type %#x, state %#x, " +"request->type %#x, request->flags %#x, " +"start_hash %llx, end_hash %llx\n" , + tree, tree->type, tree_state, + search->request.type, search->request.flags, + search->request.start.hash, + search->request.end.hash); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + +switch (tree_state) { +case SSDFS_BTREE_CREATED : +case SSDFS_BTREE_DIRTY : +/* expected state */ +break ; + +default : +#ifdef CONFIG_SSDFS_DEBUG +BUG (); +#else +SSDFS_WARN ("invalid tree state %#x\n" , + tree_state); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ERANGE ; + } + +if (!is_btree_search_request_valid (search)) { +SSDFS_ERR ("invalid search object\n" ); +return -EINVAL ; + } + +if (search->request.type !=SSDFS_BTREE_SEARCH_ALLOCATE_RANGE ) { +SSDFS_ERR ("invalid request type %#x\n" , + search->request.type); +return -EINVAL ; + } + +down_read (&tree->lock); + +try_next_search: + err =ssdfs_btree_find_leaf_node (tree, search); +if (err == -EEXIST ) { + err =0 ; +/* try the old search result */ + }else if (err == -ENOENT ) { + err = -ENODATA ; + search->result.err = -ENODATA ; + search->result.state =SSDFS_BTREE_SEARCH_PLEASE_ADD_NODE ; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("index node was found\n" ); +#endif /* CONFIG_SSDFS_DEBUG */ + +up_read (&tree->lock); + err =ssdfs_btree_insert_node (tree, search); +down_read (&tree->lock); + +if (unlikely (err)) { +SSDFS_ERR ("fail to insert node: err %d\n" , + err); + goto finish_allocate_range; + } + + err =ssdfs_btree_find_leaf_node (tree, search); +if (err == -EEXIST ) { + err =0 ; +/* try the old search result */ + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find leaf node: err %d\n" , + err); + goto finish_allocate_range; + } + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find leaf node: err %d\n" , + err); + goto finish_allocate_range; + } + +try_allocate_range: + err =ssdfs_btree_node_allocate_range (search); +if (err == -EAGAIN ) { + err =0 ; + search->node.state =SSDFS_BTREE_SEARCH_NODE_DESC_EMPTY ; + goto try_next_search; + }else if (err == -EACCES ) { +struct ssdfs_btree_node *node = search->node.child; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =SSDFS_WAIT_COMPLETION (&node->init_end); +if (unlikely (err)) { +SSDFS_ERR ("node init failed: " +"err %d\n" , err); + goto finish_allocate_range; + }else + goto try_allocate_range; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to allocate range: " +"start_hash %llx, end_hash %llx, " +"err %d\n" , + search->request.start.hash, + search->request.end.hash, + err); + goto finish_allocate_range; + } + +atomic_set (&tree->state,SSDFS_BTREE_DIRTY ); + +finish_allocate_range: +up_read (&tree->lock); + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("finished\n" ); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + +ssdfs_debug_btree_object (tree); + +#ifdef CONFIG_SSDFS_BTREE_STRICT_CONSISTENCY_CHECK +ssdfs_check_btree_consistency (tree); +#endif /* CONFIG_SSDFS_BTREE_STRICT_CONSISTENCY_CHECK */ + +return err; +} + +/* +* need_update_parent_node() - check necessity to update index in parent node +* @search: search object +*/ +static inline +bool need_update_parent_node (struct ssdfs_btree_search *search) +{ +struct ssdfs_btree_node *child; +u64 start_hash; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!search); +#endif /* CONFIG_SSDFS_DEBUG */ + + start_hash = search->request.start.hash; + + child = search->node.child; +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!child); +#endif /* CONFIG_SSDFS_DEBUG */ + +return need_update_parent_index_area (start_hash, child); +} + +/* +* ssdfs_btree_update_index_in_parent_node() - update index in parent node +* @tree: btree object +* @search: search object [in|out] +* @ptr: hierarchy object +* +* This method tries to update an index in parent nodes. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +*/ +static +int ssdfs_btree_update_index_in_parent_node (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search, +struct ssdfs_btree_hierarchy *ptr) +{ +int cur_height, tree_height; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree || !ptr); +BUG_ON (!rwsem_is_locked (&tree->lock)); + +SSDFS_DBG ("tree %p, hierarchy %p\n" , + tree, ptr); +#endif /* CONFIG_SSDFS_DEBUG */ + + tree_height =atomic_read (&tree->height); +if (tree_height <=0 ) { +SSDFS_ERR ("invalid tree_height %u\n" , + tree_height); +return -ERANGE ; + } + +for (cur_height =0 ; cur_height < tree_height; cur_height++) { + err =ssdfs_btree_process_level_for_update (ptr, + cur_height, + search); +if (unlikely (err)) { +SSDFS_ERR ("fail to process the tree's level: " +"cur_height %u, err %d\n" , + cur_height, err); +return err; + } + } + +return 0 ; +} + +/* +* ssdfs_btree_add_item() - add item into btree +* @tree: btree object +* @search: search object [in|out] +* +* This method tries to add the item into the tree. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +* %-EEXIST - item exists in the tree. +*/ +int ssdfs_btree_add_item (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search) +{ +struct ssdfs_btree_hierarchy *hierarchy =NULL ; +int tree_state; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree || !search); +#endif /* CONFIG_SSDFS_DEBUG */ + + tree_state =atomic_read (&tree->state); + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("tree %p, type %#x, state %#x, " +"request->type %#x, request->flags %#x, " +"start_hash %llx, end_hash %llx\n" , + tree, tree->type, tree_state, + search->request.type, search->request.flags, + search->request.start.hash, + search->request.end.hash); +#else +SSDFS_DBG ("tree %p, type %#x, state %#x, " +"request->type %#x, request->flags %#x, " +"start_hash %llx, end_hash %llx\n" , + tree, tree->type, tree_state, + search->request.type, search->request.flags, + search->request.start.hash, + search->request.end.hash); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + +switch (tree_state) { +case SSDFS_BTREE_CREATED : +case SSDFS_BTREE_DIRTY : +/* expected state */ +break ; + +default : +#ifdef CONFIG_SSDFS_DEBUG +BUG (); +#else +SSDFS_WARN ("invalid tree state %#x\n" , + tree_state); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ERANGE ; + } + +if (!is_btree_search_request_valid (search)) { +SSDFS_ERR ("invalid search object\n" ); +return -EINVAL ; + } + +if (search->request.type !=SSDFS_BTREE_SEARCH_ADD_ITEM ) { +SSDFS_ERR ("invalid request type %#x\n" , + search->request.type); +return -EINVAL ; + } + +down_read (&tree->lock); + +try_find_item: + err =__ssdfs_btree_find_item (tree, search); +if (!err) { + err = -EEXIST ; +SSDFS_ERR ("item exists in the tree: " +"start_hash %llx, end_hash %llx\n" , + search->request.start.hash, + search->request.end.hash); + goto finish_add_item; + }else if (err == -ENODATA ) { + err =0 ; +switch (search->result.state) { +case SSDFS_BTREE_SEARCH_POSSIBLE_PLACE_FOUND : +case SSDFS_BTREE_SEARCH_OUT_OF_RANGE : +/* position in node was found */ +break ; +case SSDFS_BTREE_SEARCH_PLEASE_ADD_NODE : +/* none node is able to store the new item */ +break ; +default : + err = -ERANGE ; +SSDFS_ERR ("invalid search result: " +"start_hash %llx, end_hash %llx, " +"state %#x\n" , + search->request.start.hash, + search->request.end.hash, + search->result.state); + goto finish_add_item; + }; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find item: " +"start_hash %llx, end_hash %llx, err %d\n" , + search->request.start.hash, + search->request.end.hash, + err); + goto finish_add_item; + } + +if (search->result.state ==SSDFS_BTREE_SEARCH_PLEASE_ADD_NODE ) { +up_read (&tree->lock); + err =ssdfs_btree_insert_node (tree, search); +down_read (&tree->lock); + +if (unlikely (err)) { +SSDFS_ERR ("fail to insert node: err %d\n" , + err); + goto finish_add_item; + } + + err =__ssdfs_btree_find_item (tree, search); +if (!err) { + err = -EEXIST ; +SSDFS_ERR ("item exists in the tree: " +"start_hash %llx, end_hash %llx\n" , + search->request.start.hash, + search->request.end.hash); + goto finish_add_item; + }else if (err == -ENODATA ) { + err =0 ; +switch (search->result.state) { +case SSDFS_BTREE_SEARCH_POSSIBLE_PLACE_FOUND : +case SSDFS_BTREE_SEARCH_OUT_OF_RANGE : +/* position in node was found */ +break ; +default : + err = -ERANGE ; +SSDFS_ERR ("invalid search result: " +"start_hash %llx, end_hash %llx, " +"state %#x\n" , + search->request.start.hash, + search->request.end.hash, + search->result.state); + goto finish_add_item; + }; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find item: " +"start_hash %llx, end_hash %llx, err %d\n" , + search->request.start.hash, + search->request.end.hash, + err); + goto finish_add_item; + } + } + +try_insert_item: + err =ssdfs_btree_node_insert_item (search); +if (err == -EACCES ) { +struct ssdfs_btree_node *node = search->node.child; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =SSDFS_WAIT_COMPLETION (&node->init_end); +if (unlikely (err)) { +SSDFS_ERR ("node init failed: " +"err %d\n" , err); + goto finish_add_item; + }else + goto try_insert_item; + }else if (err == -EFBIG ) { +int state = search->result.state; + + err =0 ; + +if (state !=SSDFS_BTREE_SEARCH_PLEASE_MOVE_BUF_CONTENT ) { + err = -ERANGE ; +SSDFS_WARN ("invalid search's result state %#x\n" , + state); + goto finish_add_item; + }else + goto try_find_item; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to insert item: " +"start_hash %llx, end_hash %llx, " +"err %d\n" , + search->request.start.hash, + search->request.end.hash, + err); + goto finish_add_item; + } + +if (need_update_parent_node (search)) { + hierarchy =ssdfs_btree_hierarchy_allocate (tree); +if (!hierarchy) { + err = -ENOMEM ; +SSDFS_ERR ("fail to allocate tree levels' array\n" ); + goto finish_add_item; + } + + err =ssdfs_btree_check_hierarchy_for_update (tree, search, + hierarchy); +if (unlikely (err)) { +atomic_set (&search->node.child->state, +SSDFS_BTREE_NODE_CORRUPTED ); +SSDFS_ERR ("fail to prepare hierarchy information : " +"err %d\n" , + err); + goto finish_update_parent; + } + + err =ssdfs_btree_update_index_in_parent_node (tree, search, + hierarchy); +if (unlikely (err)) { +SSDFS_ERR ("fail to update index records: " +"err %d\n" , + err); + goto finish_update_parent; + } + +finish_update_parent: +ssdfs_btree_hierarchy_free (hierarchy); + +if (unlikely (err)) + goto finish_add_item; + } + +atomic_set (&tree->state,SSDFS_BTREE_DIRTY ); + +finish_add_item: +up_read (&tree->lock); + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("finished\n" ); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + +ssdfs_debug_btree_object (tree); + +#ifdef CONFIG_SSDFS_BTREE_STRICT_CONSISTENCY_CHECK +ssdfs_check_btree_consistency (tree); +#endif /* CONFIG_SSDFS_BTREE_STRICT_CONSISTENCY_CHECK */ + +return err; +} + +/* +* ssdfs_btree_add_range() - add a range of items into btree +* @tree: btree object +* @search: search object [in|out] +* +* This method tries to add the range of items into the tree. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +* %-EEXIST - range exists in the tree. +*/ +int ssdfs_btree_add_range (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search) +{ +struct ssdfs_btree_hierarchy *hierarchy =NULL ; +int tree_state; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree || !search); +#endif /* CONFIG_SSDFS_DEBUG */ + + tree_state =atomic_read (&tree->state); + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("tree %p, type %#x, state %#x, " +"request->type %#x, request->flags %#x, " +"start_hash %llx, end_hash %llx\n" , + tree, tree->type, tree_state, + search->request.type, search->request.flags, + search->request.start.hash, + search->request.end.hash); +#else +SSDFS_DBG ("tree %p, type %#x, state %#x, " +"request->type %#x, request->flags %#x, " +"start_hash %llx, end_hash %llx\n" , + tree, tree->type, tree_state, + search->request.type, search->request.flags, + search->request.start.hash, + search->request.end.hash); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + +switch (tree_state) { +case SSDFS_BTREE_CREATED : +case SSDFS_BTREE_DIRTY : +/* expected state */ +break ; + +default : +#ifdef CONFIG_SSDFS_DEBUG +BUG (); +#else +SSDFS_WARN ("invalid tree state %#x\n" , + tree_state); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ERANGE ; + } + +if (!is_btree_search_request_valid (search)) { +SSDFS_ERR ("invalid search object\n" ); +return -EINVAL ; + } + +if (search->request.type !=SSDFS_BTREE_SEARCH_ADD_RANGE ) { +SSDFS_ERR ("invalid request type %#x\n" , + search->request.type); +return -EINVAL ; + } + +down_read (&tree->lock); + +try_find_range: + err =__ssdfs_btree_find_range (tree, search); +if (!err) { + err = -EEXIST ; +SSDFS_ERR ("range exists in the tree: " +"start_hash %llx, end_hash %llx\n" , + search->request.start.hash, + search->request.end.hash); + goto finish_add_range; + }else if (err == -ENODATA ) { + err =0 ; +switch (search->result.state) { +case SSDFS_BTREE_SEARCH_POSSIBLE_PLACE_FOUND : +/* position in node was found */ +break ; +case SSDFS_BTREE_SEARCH_PLEASE_ADD_NODE : +/* none node is able to store the new range */ +break ; +default : + err = -ERANGE ; +SSDFS_ERR ("invalid search result: " +"start_hash %llx, end_hash %llx, " +"state %#x\n" , + search->request.start.hash, + search->request.end.hash, + search->result.state); + goto finish_add_range; + }; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find range: " +"start_hash %llx, end_hash %llx, err %d\n" , + search->request.start.hash, + search->request.end.hash, + err); + goto finish_add_range; + } + +if (search->result.state ==SSDFS_BTREE_SEARCH_PLEASE_ADD_NODE ) { +up_read (&tree->lock); + err =ssdfs_btree_insert_node (tree, search); +down_read (&tree->lock); + +if (unlikely (err)) { +SSDFS_ERR ("fail to insert node: err %d\n" , + err); + goto finish_add_range; + } + + err =__ssdfs_btree_find_range (tree, search); +if (!err) { + err = -EEXIST ; +SSDFS_ERR ("range exists in the tree: " +"start_hash %llx, end_hash %llx\n" , + search->request.start.hash, + search->request.end.hash); + goto finish_add_range; + }else if (err == -ENODATA ) { + err =0 ; +switch (search->result.state) { +case SSDFS_BTREE_SEARCH_POSSIBLE_PLACE_FOUND : +case SSDFS_BTREE_SEARCH_OUT_OF_RANGE : +/* position in node was found */ +break ; +default : + err = -ERANGE ; +SSDFS_ERR ("invalid search result: " +"start_hash %llx, end_hash %llx, " +"state %#x\n" , + search->request.start.hash, + search->request.end.hash, + search->result.state); + goto finish_add_range; + }; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find range: " +"start_hash %llx, end_hash %llx, err %d\n" , + search->request.start.hash, + search->request.end.hash, + err); + goto finish_add_range; + } + } + +try_insert_range: + err =ssdfs_btree_node_insert_range (search); +if (err == -EAGAIN ) { + err =0 ; + goto try_find_range; + }else if (err == -EACCES ) { +struct ssdfs_btree_node *node = search->node.child; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =SSDFS_WAIT_COMPLETION (&node->init_end); +if (unlikely (err)) { +SSDFS_ERR ("node init failed: " +"err %d\n" , err); + goto finish_add_range; + }else + goto try_insert_range; + }else if (err == -EFBIG ) { +int state = search->result.state; + + err =0 ; + +if (state !=SSDFS_BTREE_SEARCH_PLEASE_MOVE_BUF_CONTENT ) { + err = -ERANGE ; +SSDFS_WARN ("invalid search's result state %#x\n" , + state); + goto finish_add_range; + }else + goto try_find_range; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to insert item: " +"start_hash %llx, end_hash %llx, " +"err %d\n" , + search->request.start.hash, + search->request.end.hash, + err); + goto finish_add_range; + } + +if (need_update_parent_node (search)) { + hierarchy =ssdfs_btree_hierarchy_allocate (tree); +if (!hierarchy) { + err = -ENOMEM ; +SSDFS_ERR ("fail to allocate tree levels' array\n" ); + goto finish_add_range; + } + + err =ssdfs_btree_check_hierarchy_for_update (tree, search, + hierarchy); +if (unlikely (err)) { +atomic_set (&search->node.child->state, +SSDFS_BTREE_NODE_CORRUPTED ); +SSDFS_ERR ("fail to prepare hierarchy information : " +"err %d\n" , + err); + goto finish_update_parent; + } + + err =ssdfs_btree_update_index_in_parent_node (tree, search, + hierarchy); +if (unlikely (err)) { +SSDFS_ERR ("fail to update index records: " +"err %d\n" , + err); + goto finish_update_parent; + } + +finish_update_parent: +ssdfs_btree_hierarchy_free (hierarchy); + +if (unlikely (err)) + goto finish_add_range; + } + +atomic_set (&tree->state,SSDFS_BTREE_DIRTY ); + +finish_add_range: +up_read (&tree->lock); + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("finished\n" ); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + +ssdfs_debug_btree_object (tree); + +#ifdef CONFIG_SSDFS_BTREE_STRICT_CONSISTENCY_CHECK +ssdfs_check_btree_consistency (tree); +#endif /* CONFIG_SSDFS_BTREE_STRICT_CONSISTENCY_CHECK */ + +return err; +} + +/* +* ssdfs_btree_change_item() - change an existing item in the btree +* @tree: btree object +* @search: search object [in|out] +* +* This method tries to change the existing item in the tree. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +*/ +int ssdfs_btree_change_item (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search) +{ +struct ssdfs_btree_hierarchy *hierarchy =NULL ; +int tree_state; +int tree_height; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree || !search); +#endif /* CONFIG_SSDFS_DEBUG */ + + tree_state =atomic_read (&tree->state); + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("tree %p, type %#x, state %#x, " +"request->type %#x, request->flags %#x, " +"start_hash %llx, end_hash %llx\n" , + tree, tree->type, tree_state, + search->request.type, search->request.flags, + search->request.start.hash, + search->request.end.hash); +#else +SSDFS_DBG ("tree %p, type %#x, state %#x, " +"request->type %#x, request->flags %#x, " +"start_hash %llx, end_hash %llx\n" , + tree, tree->type, tree_state, + search->request.type, search->request.flags, + search->request.start.hash, + search->request.end.hash); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + +switch (tree_state) { +case SSDFS_BTREE_CREATED : +case SSDFS_BTREE_DIRTY : +/* expected state */ +break ; + +default : +#ifdef CONFIG_SSDFS_DEBUG +BUG (); +#else +SSDFS_WARN ("invalid tree state %#x\n" , + tree_state); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ERANGE ; + } + +if (!is_btree_search_request_valid (search)) { +SSDFS_ERR ("invalid search object\n" ); +return -EINVAL ; + } + +switch (search->request.type) { +case SSDFS_BTREE_SEARCH_CHANGE_ITEM : +case SSDFS_BTREE_SEARCH_INVALIDATE_TAIL : +/* expected type */ +break ; + +default : +SSDFS_ERR ("invalid request type %#x\n" , + search->request.type); +return -EINVAL ; + } + + tree_height =atomic_read (&tree->height); +if (tree_height <=0 ) { +SSDFS_ERR ("invalid tree_height %u\n" , + tree_height); +return -ERANGE ; + } + +down_read (&tree->lock); + +try_next_search: + err =ssdfs_btree_find_leaf_node (tree, search); +if (err == -EEXIST ) { + err =0 ; +/* try the old search result */ + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find leaf node: err %d\n" , + err); + goto finish_change_item; + } + +try_change_item: + err =ssdfs_btree_node_change_item (search); +if (err == -EAGAIN ) { + err =0 ; + search->node.state =SSDFS_BTREE_SEARCH_NODE_DESC_EMPTY ; + goto try_next_search; + }else if (err == -EACCES ) { +struct ssdfs_btree_node *node = search->node.child; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =SSDFS_WAIT_COMPLETION (&node->init_end); +if (unlikely (err)) { +SSDFS_ERR ("node init failed: " +"err %d\n" , err); + goto finish_change_item; + }else + goto try_change_item; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to change item: " +"start_hash %llx, end_hash %llx, " +"err %d\n" , + search->request.start.hash, + search->request.end.hash, + err); + goto finish_change_item; + } + +if (need_update_parent_node (search)) { + hierarchy =ssdfs_btree_hierarchy_allocate (tree); +if (!hierarchy) { + err = -ENOMEM ; +SSDFS_ERR ("fail to allocate tree levels' array\n" ); + goto finish_change_item; + } + + err =ssdfs_btree_check_hierarchy_for_update (tree, search, + hierarchy); +if (unlikely (err)) { +atomic_set (&search->node.child->state, +SSDFS_BTREE_NODE_CORRUPTED ); +SSDFS_ERR ("fail to prepare hierarchy information : " +"err %d\n" , + err); + goto finish_update_parent; + } + + err =ssdfs_btree_update_index_in_parent_node (tree, search, + hierarchy); +if (unlikely (err)) { +SSDFS_ERR ("fail to update index records: " +"err %d\n" , + err); + goto finish_update_parent; + } + +finish_update_parent: +ssdfs_btree_hierarchy_free (hierarchy); + +if (unlikely (err)) + goto finish_change_item; + } + +atomic_set (&tree->state,SSDFS_BTREE_DIRTY ); + +finish_change_item: +up_read (&tree->lock); + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("finished\n" ); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + +ssdfs_debug_btree_object (tree); + +#ifdef CONFIG_SSDFS_BTREE_STRICT_CONSISTENCY_CHECK +ssdfs_check_btree_consistency (tree); +#endif /* CONFIG_SSDFS_BTREE_STRICT_CONSISTENCY_CHECK */ + +return err; +} + +/* +* ssdfs_btree_delete_item() - delete an existing item in the btree +* @tree: btree object +* @search: search object [in|out] +* +* This method tries to delete the existing item in the tree. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +*/ +int ssdfs_btree_delete_item (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search) +{ +struct ssdfs_btree_hierarchy *hierarchy =NULL ; +int tree_state; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree || !search); +#endif /* CONFIG_SSDFS_DEBUG */ + + tree_state =atomic_read (&tree->state); + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("tree %p, type %#x, state %#x, " +"request->type %#x, request->flags %#x, " +"start_hash %llx, end_hash %llx\n" , + tree, tree->type, tree_state, + search->request.type, search->request.flags, + search->request.start.hash, + search->request.end.hash); +#else +SSDFS_DBG ("tree %p, type %#x, state %#x, " +"request->type %#x, request->flags %#x, " +"start_hash %llx, end_hash %llx\n" , + tree, tree->type, tree_state, + search->request.type, search->request.flags, + search->request.start.hash, + search->request.end.hash); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + +switch (tree_state) { +case SSDFS_BTREE_CREATED : +case SSDFS_BTREE_DIRTY : +/* expected state */ +break ; + +default : +#ifdef CONFIG_SSDFS_DEBUG +BUG (); +#else +SSDFS_WARN ("invalid tree state %#x\n" , + tree_state); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ERANGE ; + } + +if (!is_btree_search_request_valid (search)) { +SSDFS_ERR ("invalid search object\n" ); +return -EINVAL ; + } + +switch (search->request.type) { +case SSDFS_BTREE_SEARCH_DELETE_ITEM : +case SSDFS_BTREE_SEARCH_INVALIDATE_TAIL : +/* expected type */ +break ; + +default : +SSDFS_ERR ("invalid request type %#x\n" , + search->request.type); +return -EINVAL ; + } + +down_read (&tree->lock); + + err =__ssdfs_btree_find_item (tree, search); +if (unlikely (err)) { +SSDFS_ERR ("fail to find item: " +"start_hash %llx, end_hash %llx, err %d\n" , + search->request.start.hash, + search->request.end.hash, + err); + goto finish_delete_item; + } + +try_delete_item: + err =ssdfs_btree_node_delete_item (search); +if (err == -EACCES ) { +struct ssdfs_btree_node *node = search->node.child; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =SSDFS_WAIT_COMPLETION (&node->init_end); +if (unlikely (err)) { +SSDFS_ERR ("node init failed: " +"err %d\n" , err); + goto finish_delete_item; + }else + goto try_delete_item; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to delete item: " +"start_hash %llx, end_hash %llx, err %d\n" , + search->request.start.hash, + search->request.end.hash, + err); + goto finish_delete_item; + } + +if (search->result.state ==SSDFS_BTREE_SEARCH_PLEASE_DELETE_NODE ) { +up_read (&tree->lock); + err =ssdfs_btree_delete_node (tree, search); +down_read (&tree->lock); + +if (unlikely (err)) { +SSDFS_ERR ("fail to delete btree node: " +"node_id %llu, err %d\n" , + (u64 )search->node.id, err); + goto finish_delete_item; + } + }else if (need_update_parent_node (search)) { + hierarchy =ssdfs_btree_hierarchy_allocate (tree); +if (!hierarchy) { + err = -ENOMEM ; +SSDFS_ERR ("fail to allocate tree levels' array\n" ); + goto finish_delete_item; + } + + err =ssdfs_btree_check_hierarchy_for_update (tree, search, + hierarchy); +if (unlikely (err)) { +atomic_set (&search->node.child->state, +SSDFS_BTREE_NODE_CORRUPTED ); +SSDFS_ERR ("fail to prepare hierarchy information : " +"err %d\n" , + err); + goto finish_update_parent; + } + + err =ssdfs_btree_update_index_in_parent_node (tree, search, + hierarchy); +if (unlikely (err)) { +SSDFS_ERR ("fail to update index records: " +"err %d\n" , + err); + goto finish_update_parent; + } + +finish_update_parent: +ssdfs_btree_hierarchy_free (hierarchy); + +if (unlikely (err)) + goto finish_delete_item; + } + +atomic_set (&tree->state,SSDFS_BTREE_DIRTY ); + +finish_delete_item: +up_read (&tree->lock); + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("finished\n" ); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + +ssdfs_debug_btree_object (tree); + +#ifdef CONFIG_SSDFS_BTREE_STRICT_CONSISTENCY_CHECK +ssdfs_check_btree_consistency (tree); +#endif /* CONFIG_SSDFS_BTREE_STRICT_CONSISTENCY_CHECK */ + +return err; +} + +/* +* ssdfs_btree_delete_range() - delete a range of items in the btree +* @tree: btree object +* @search: search object [in|out] +* +* This method tries to delete a range of existing items in the tree. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +*/ +int ssdfs_btree_delete_range (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search) +{ +struct ssdfs_btree_hierarchy *hierarchy =NULL ; +int tree_state; +bool need_continue_deletion = false; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree || !search); +#endif /* CONFIG_SSDFS_DEBUG */ + + tree_state =atomic_read (&tree->state); + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("tree %p, type %#x, state %#x, " +"request->type %#x, request->flags %#x, " +"start_hash %llx, end_hash %llx\n" , + tree, tree->type, tree_state, + search->request.type, search->request.flags, + search->request.start.hash, + search->request.end.hash); +#else +SSDFS_DBG ("tree %p, type %#x, state %#x, " +"request->type %#x, request->flags %#x, " +"start_hash %llx, end_hash %llx\n" , + tree, tree->type, tree_state, + search->request.type, search->request.flags, + search->request.start.hash, + search->request.end.hash); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + +switch (tree_state) { +case SSDFS_BTREE_CREATED : +case SSDFS_BTREE_DIRTY : +/* expected state */ +break ; + +default : +#ifdef CONFIG_SSDFS_DEBUG +BUG (); +#else +SSDFS_WARN ("invalid tree state %#x\n" , + tree_state); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ERANGE ; + } + +if (!is_btree_search_request_valid (search)) { +SSDFS_ERR ("invalid search object\n" ); +return -EINVAL ; + } + +switch (search->request.type) { +case SSDFS_BTREE_SEARCH_DELETE_RANGE : +case SSDFS_BTREE_SEARCH_DELETE_ALL : +/* expected state */ +break ; + +default : +SSDFS_ERR ("invalid request type %#x\n" , + search->request.type); +return -EINVAL ; + } + +down_read (&tree->lock); + +try_delete_next_range: + err =__ssdfs_btree_find_range (tree, search); +if (unlikely (err)) { +SSDFS_ERR ("fail to find range: " +"start_hash %llx, end_hash %llx, err %d\n" , + search->request.start.hash, + search->request.end.hash, + err); +up_read (&tree->lock); +return err; + } + +try_delete_range_again: + err =ssdfs_btree_node_delete_range (search); +if (err == -EACCES ) { +struct ssdfs_btree_node *node = search->node.child; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =SSDFS_WAIT_COMPLETION (&node->init_end); +if (unlikely (err)) { +SSDFS_ERR ("node init failed: " +"err %d\n" , err); + goto finish_delete_range; + }else + goto try_delete_range_again; + } + +finish_delete_range: +if (err == -EAGAIN ) { +/* the range have to be deleted in the next node */ + err =0 ; + need_continue_deletion = true; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to delete range: " +"start_hash %llx, end_hash %llx, err %d\n" , + search->request.start.hash, + search->request.end.hash, + err); +up_read (&tree->lock); +return err; + } + +if (search->result.state ==SSDFS_BTREE_SEARCH_PLEASE_DELETE_NODE ) { +up_read (&tree->lock); + err =ssdfs_btree_delete_node (tree, search); +down_read (&tree->lock); + +if (unlikely (err)) { +SSDFS_ERR ("fail to delete btree node: " +"node_id %llu, err %d\n" , + (u64 )search->node.id, err); + goto fail_delete_range; + } + }else if (need_update_parent_node (search)) { + hierarchy =ssdfs_btree_hierarchy_allocate (tree); +if (!hierarchy) { + err = -ENOMEM ; +SSDFS_ERR ("fail to allocate tree levels' array\n" ); + goto fail_delete_range; + } + + err =ssdfs_btree_check_hierarchy_for_update (tree, search, + hierarchy); +if (unlikely (err)) { +atomic_set (&search->node.child->state, +SSDFS_BTREE_NODE_CORRUPTED ); +SSDFS_ERR ("fail to prepare hierarchy information : " +"err %d\n" , + err); + goto finish_update_parent; + } + + err =ssdfs_btree_update_index_in_parent_node (tree, search, + hierarchy); +if (unlikely (err)) { +SSDFS_ERR ("fail to update index records: " +"err %d\n" , + err); + goto finish_update_parent; + } + +finish_update_parent: +ssdfs_btree_hierarchy_free (hierarchy); + +if (unlikely (err)) + goto fail_delete_range; + } + +if (need_continue_deletion) { + need_continue_deletion = false; + goto try_delete_next_range; + } + +atomic_set (&tree->state,SSDFS_BTREE_DIRTY ); + +fail_delete_range: +up_read (&tree->lock); + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("finished\n" ); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + +ssdfs_debug_btree_object (tree); + +#ifdef CONFIG_SSDFS_BTREE_STRICT_CONSISTENCY_CHECK +ssdfs_check_btree_consistency (tree); +#endif /* CONFIG_SSDFS_BTREE_STRICT_CONSISTENCY_CHECK */ + +return err; +} + +/* +* ssdfs_btree_delete_all() - delete all items in the btree +* @tree: btree object +* @search: search object [in|out] +* +* This method tries to delete all items in the tree. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +*/ +int ssdfs_btree_delete_all (struct ssdfs_btree *tree) +{ +struct ssdfs_btree_search *search; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree); +#endif /* CONFIG_SSDFS_DEBUG */ + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("tree %p\n" , tree); +#else +SSDFS_DBG ("tree %p\n" , tree); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + + search =ssdfs_btree_search_alloc (); +if (!search) { +SSDFS_ERR ("fail to allocate btree search object\n" ); +return -ENOMEM ; + } + +ssdfs_btree_search_init (search); + + search->request.type =SSDFS_BTREE_SEARCH_DELETE_ALL ; + search->request.start.hash =0 ; + search->request.end.hash =U64_MAX ; + + err =ssdfs_btree_delete_range (tree, search); +if (unlikely (err)) +SSDFS_ERR ("fail to delete all items: err %d\n" , err); + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("finished\n" ); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + +ssdfs_btree_search_free (search); +return err; +} + +/* +* ssdfs_btree_get_head_range() - extract head range of the tree +* @tree: btree object +* @expected_len: expected length of the range +* @search: search object +* +* This method tries to extract a head range of items from the tree. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +* %-EAGAIN - expected length of the range is not extracted +*/ +int ssdfs_btree_get_head_range (struct ssdfs_btree *tree, +u32 expected_len, +struct ssdfs_btree_search *search) +{ +struct ssdfs_btree_node *node; +struct ssdfs_btree_index_key key; +int tree_state; +u64 hash; +u32 buf_size; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree || !search); +#endif /* CONFIG_SSDFS_DEBUG */ + + tree_state =atomic_read (&tree->state); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("tree %p, type %#x, state %#x, " +"expected_len %u\n" , + tree, tree->type, tree_state, + expected_len); +#endif /* CONFIG_SSDFS_DEBUG */ + +switch (tree_state) { +case SSDFS_BTREE_CREATED : +case SSDFS_BTREE_DIRTY : +/* expected state */ +break ; + +default : +#ifdef CONFIG_SSDFS_DEBUG +BUG (); +#else +SSDFS_WARN ("invalid tree state %#x\n" , + tree_state); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ERANGE ; + } + +down_read (&tree->lock); + + err =ssdfs_btree_radix_tree_find (tree, +SSDFS_BTREE_ROOT_NODE_ID , + &node); +if (unlikely (err)) { +SSDFS_ERR ("fail to find root node: err %d\n" , + err); + goto finish_get_range; + }else if (!node) { + err = -ERANGE ; +SSDFS_ERR ("node is NULL\n" ); + goto finish_get_range; + } + +if (!is_ssdfs_btree_node_index_area_exist (node)) { + err = -ERANGE ; +SSDFS_WARN ("root node hasn't index area\n" ); + goto finish_get_range; + } + +if (is_ssdfs_btree_node_index_area_empty (node)) + goto finish_get_range; + +down_read (&node->full_lock); + err =__ssdfs_btree_root_node_extract_index (node, +SSDFS_ROOT_NODE_LEFT_LEAF_NODE , + &key); +up_read (&node->full_lock); + +if (unlikely (err)) { +SSDFS_ERR ("fail to get index: err %d\n" , + err); + goto finish_get_range; + } + + hash =le64_to_cpu (key.index.hash); +if (hash >=U64_MAX ) { + err = -ERANGE ; +SSDFS_ERR ("invalid hash\n" ); + goto finish_get_range; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("hash %llx\n" , hash); +#endif /* CONFIG_SSDFS_DEBUG */ + + search->request.type =SSDFS_BTREE_SEARCH_FIND_ITEM ; + search->request.flags =SSDFS_BTREE_SEARCH_HAS_VALID_HASH_RANGE | +SSDFS_BTREE_SEARCH_HAS_VALID_COUNT ; + search->request.start.hash = hash; + search->request.end.hash = hash; + search->request.count =1 ; + + err =__ssdfs_btree_find_item (tree, search); +if (unlikely (err)) { +SSDFS_ERR ("fail to find the item: " +"hash %llx, err %d\n" , + hash, err); + goto finish_get_range; + } + + buf_size = expected_len * tree->max_item_size; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (expected_len >=U16_MAX ); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_btree_node_extract_range (search->result.start_index, + (u16 )expected_len, + search); +if (unlikely (err)) { +SSDFS_ERR ("fail to extract the range: " +"start_index %u, expected_len %u, err %d\n" , + search->result.start_index, + expected_len, err); + goto finish_get_range; + } + +if (expected_len != search->result.count) { + err = -EAGAIN ; +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("expected_len %u != search->result.count %u\n" , + expected_len, search->result.count); +#endif /* CONFIG_SSDFS_DEBUG */ + } + +finish_get_range: +up_read (&tree->lock); + +return err; +} + +/* +* ssdfs_btree_extract_range() - extract range from the node +* @tree: btree object +* @start_index: start index in the node +* @count: count of items in the range +* @search: search object +* +* This method tries to extract a range of items from the found node. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +*/ +int ssdfs_btree_extract_range (struct ssdfs_btree *tree, +u16 start_index,u16 count, +struct ssdfs_btree_search *search) +{ +int tree_state; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree || !search); +#endif /* CONFIG_SSDFS_DEBUG */ + + tree_state =atomic_read (&tree->state); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("tree %p, type %#x, state %#x, " +"start_index %u, count %u\n" , + tree, tree->type, tree_state, + start_index, count); + +ssdfs_debug_btree_object (tree); +#endif /* CONFIG_SSDFS_DEBUG */ + +switch (tree_state) { +case SSDFS_BTREE_CREATED : +case SSDFS_BTREE_DIRTY : +/* expected state */ +break ; + +default : +#ifdef CONFIG_SSDFS_DEBUG +BUG (); +#else +SSDFS_WARN ("invalid tree state %#x\n" , + tree_state); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ERANGE ; + } + +down_read (&tree->lock); + + err =ssdfs_btree_node_extract_range (start_index, count, + search); +if (unlikely (err)) { +SSDFS_ERR ("fail to extract the range: " +"start_index %u, count %u, err %d\n" , + start_index, count, err); + goto finish_get_range; + } + +finish_get_range: +up_read (&tree->lock); + +return err; +} + +/* +* is_ssdfs_btree_empty() - check that btree is empty +* @tree: btree object +*/ +bool is_ssdfs_btree_empty (struct ssdfs_btree *tree) +{ +struct ssdfs_btree_node *node; +struct ssdfs_btree_index_key key1, key2; +int tree_state; +u32 node_id1, node_id2; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree); +#endif /* CONFIG_SSDFS_DEBUG */ + + tree_state =atomic_read (&tree->state); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("tree %p, type %#x, state %#x\n" , + tree, tree->type, tree_state); +#endif /* CONFIG_SSDFS_DEBUG */ + +switch (tree_state) { +case SSDFS_BTREE_CREATED : +case SSDFS_BTREE_DIRTY : +/* expected state */ +break ; + +default : +#ifdef CONFIG_SSDFS_DEBUG +BUG (); +#else +SSDFS_WARN ("invalid tree state %#x\n" , + tree_state); +#endif /* CONFIG_SSDFS_DEBUG */ +return false; + } + +down_read (&tree->lock); + + err =ssdfs_btree_radix_tree_find (tree, +SSDFS_BTREE_ROOT_NODE_ID , + &node); +if (unlikely (err)) { +SSDFS_ERR ("fail to find root node: err %d\n" , + err); + goto finish_check_tree; + }else if (!node) { + err = -ERANGE ; +SSDFS_ERR ("node is NULL\n" ); + goto finish_check_tree; + } + +if (!is_ssdfs_btree_node_index_area_exist (node)) { + err = -ERANGE ; +SSDFS_WARN ("root node hasn't index area\n" ); + goto finish_check_tree; + } + +if (is_ssdfs_btree_node_index_area_empty (node)) + goto finish_check_tree; + +down_read (&node->full_lock); + err =__ssdfs_btree_root_node_extract_index (node, +SSDFS_ROOT_NODE_LEFT_LEAF_NODE , + &key1); +up_read (&node->full_lock); + +if (unlikely (err)) { +SSDFS_ERR ("fail to get index: err %d\n" , + err); + goto finish_check_tree; + } + + node_id1 =le32_to_cpu (key1.node_id); +if (node_id1 ==SSDFS_BTREE_NODE_INVALID_ID ) { +SSDFS_WARN ("index is invalid\n" ); + goto finish_check_tree; + } + +down_read (&node->full_lock); + err =__ssdfs_btree_root_node_extract_index (node, +SSDFS_ROOT_NODE_RIGHT_LEAF_NODE , + &key2); +up_read (&node->full_lock); + +if (unlikely (err)) { +SSDFS_ERR ("fail to get index: err %d\n" , + err); + goto finish_check_tree; + } + + node_id2 =le32_to_cpu (key2.node_id); +if (node_id2 !=SSDFS_BTREE_NODE_INVALID_ID ) { + err = -EEXIST ; + goto finish_check_tree; + } + + err =ssdfs_btree_radix_tree_find (tree, node_id1, &node); +if (unlikely (err)) { +SSDFS_ERR ("fail to find node: node_id %u, err %d\n" , + node_id1, err); + goto finish_check_tree; + }else if (!node) { + err = -ERANGE ; +SSDFS_ERR ("node is NULL\n" ); + goto finish_check_tree; + } + +switch (atomic_read (&node->type)) { +case SSDFS_BTREE_LEAF_NODE : +if (!is_ssdfs_btree_node_items_area_empty (node)) { + err = -EEXIST ; + goto finish_check_tree; + }else { +/* empty node */ + goto finish_check_tree; + } +break ; + +case SSDFS_BTREE_HYBRID_NODE : +if (!is_ssdfs_btree_node_items_area_empty (node)) { + err = -EEXIST ; + goto finish_check_tree; + }else if (!is_ssdfs_btree_node_index_area_empty (node)) { + err = -EEXIST ; + goto finish_check_tree; + }else { +/* empty node */ + goto finish_check_tree; + } +break ; + +case SSDFS_BTREE_INDEX_NODE : + err = -EEXIST ; + goto finish_check_tree; + +case SSDFS_BTREE_ROOT_NODE : + err = -ERANGE ; +SSDFS_WARN ("node %u has root node type\n" , + node_id1); + goto finish_check_tree; + +default : + err = -ERANGE ; +SSDFS_ERR ("invalid node type %#x\n" , +atomic_read (&node->type)); + goto finish_check_tree; + } + +finish_check_tree: +up_read (&tree->lock); + +return err ? false : true; +} + +/* +* need_migrate_generic2inline_btree() - is it time to migrate? +* @tree: btree object +* @items_threshold: items migration threshold +*/ +bool need_migrate_generic2inline_btree (struct ssdfs_btree *tree, +int items_threshold) +{ +struct ssdfs_btree_node *node; +struct ssdfs_btree_index_key key1, key2; +int tree_state; +u32 node_id1, node_id2; +u16 items_count; +bool need_migrate = false; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree); +#endif /* CONFIG_SSDFS_DEBUG */ + + tree_state =atomic_read (&tree->state); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("tree %p, type %#x, state %#x\n" , + tree, tree->type, tree_state); +#endif /* CONFIG_SSDFS_DEBUG */ + +switch (tree_state) { +case SSDFS_BTREE_CREATED : +case SSDFS_BTREE_DIRTY : +/* expected state */ +break ; + +default : +#ifdef CONFIG_SSDFS_DEBUG +BUG (); +#else +SSDFS_WARN ("invalid tree state %#x\n" , + tree_state); +#endif /* CONFIG_SSDFS_DEBUG */ +return false; + } + +down_read (&tree->lock); + + err =ssdfs_btree_radix_tree_find (tree, +SSDFS_BTREE_ROOT_NODE_ID , + &node); +if (unlikely (err)) { +SSDFS_ERR ("fail to find root node: err %d\n" , + err); + goto finish_check_tree; + }else if (!node) { + err = -ERANGE ; +SSDFS_ERR ("node is NULL\n" ); + goto finish_check_tree; + } + +if (!is_ssdfs_btree_node_index_area_exist (node)) { + err = -ERANGE ; +SSDFS_WARN ("root node hasn't index area\n" ); + goto finish_check_tree; + } + +if (is_ssdfs_btree_node_index_area_empty (node)) + goto finish_check_tree; + +down_read (&node->full_lock); + err =__ssdfs_btree_root_node_extract_index (node, +SSDFS_ROOT_NODE_LEFT_LEAF_NODE , + &key1); +up_read (&node->full_lock); + +if (unlikely (err)) { +SSDFS_ERR ("fail to get index: err %d\n" , + err); + goto finish_check_tree; + } + + node_id1 =le32_to_cpu (key1.node_id); +if (node_id1 ==SSDFS_BTREE_NODE_INVALID_ID ) { +SSDFS_WARN ("index is invalid\n" ); + goto finish_check_tree; + } + +down_read (&node->full_lock); + err =__ssdfs_btree_root_node_extract_index (node, +SSDFS_ROOT_NODE_RIGHT_LEAF_NODE , + &key2); +up_read (&node->full_lock); + +if (unlikely (err)) { +SSDFS_ERR ("fail to get index: err %d\n" , + err); + goto finish_check_tree; + } + + node_id2 =le32_to_cpu (key2.node_id); +if (node_id2 !=SSDFS_BTREE_NODE_INVALID_ID ) { + err = -EEXIST ; + goto finish_check_tree; + } + + err =ssdfs_btree_radix_tree_find (tree, node_id1, &node); +if (unlikely (err)) { +SSDFS_ERR ("fail to find node: node_id %u, err %d\n" , + node_id1, err); + goto finish_check_tree; + }else if (!node) { + err = -ERANGE ; +SSDFS_ERR ("node is NULL\n" ); + goto finish_check_tree; + } + +switch (atomic_read (&node->type)) { +case SSDFS_BTREE_LEAF_NODE : +if (!is_ssdfs_btree_node_items_area_empty (node)) { +down_read (&node->header_lock); + items_count = node->items_area.items_count; +up_read (&node->header_lock); + +if (items_count <= items_threshold) { +/* time to migrate */ + need_migrate = true; + } + + goto finish_check_tree; + }else { +/* empty node */ + goto finish_check_tree; + } +break ; + +case SSDFS_BTREE_HYBRID_NODE : +if (is_ssdfs_btree_node_index_area_empty (node) && + !is_ssdfs_btree_node_items_area_empty (node)) { +down_read (&node->header_lock); + items_count = node->items_area.items_count; +up_read (&node->header_lock); + +if (items_count <= items_threshold) { +/* time to migrate */ + need_migrate = true; + } + + goto finish_check_tree; + }else if (!is_ssdfs_btree_node_index_area_empty (node)) { + err = -EEXIST ; + goto finish_check_tree; + }else { +/* empty node */ + goto finish_check_tree; + } +break ; + +case SSDFS_BTREE_INDEX_NODE : + err = -EEXIST ; + goto finish_check_tree; + +case SSDFS_BTREE_ROOT_NODE : + err = -ERANGE ; +SSDFS_WARN ("node %u has root node type\n" , + node_id1); + goto finish_check_tree; + +default : + err = -ERANGE ; +SSDFS_ERR ("invalid node type %#x\n" , +atomic_read (&node->type)); + goto finish_check_tree; + } + +finish_check_tree: +up_read (&tree->lock); + +return need_migrate; +} + +/* +* ssdfs_btree_synchronize_root_node() - synchronize root node state +* @tree: btree object +* @root: root node +*/ +int ssdfs_btree_synchronize_root_node (struct ssdfs_btree *tree, +struct ssdfs_btree_inline_root_node *root) +{ +int tree_state; +struct ssdfs_btree_node *node; +u16 items_count; +int height; +size_t ids_array_size =sizeof (__le32) * +SSDFS_BTREE_ROOT_NODE_INDEX_COUNT ; +size_t indexes_size =sizeof (struct ssdfs_btree_index ) * +SSDFS_BTREE_ROOT_NODE_INDEX_COUNT ; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree || !root); +#endif /* CONFIG_SSDFS_DEBUG */ + + tree_state =atomic_read (&tree->state); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("tree %p, root %p, type %#x, state %#x\n" , + tree, root, tree->type, tree_state); +#endif /* CONFIG_SSDFS_DEBUG */ + +switch (tree_state) { +case SSDFS_BTREE_CREATED : +case SSDFS_BTREE_DIRTY : +/* expected state */ +break ; + +default : +#ifdef CONFIG_SSDFS_DEBUG +BUG (); +#else +SSDFS_WARN ("invalid tree state %#x\n" , + tree_state); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ERANGE ; + } + +down_read (&tree->lock); + + err =ssdfs_btree_radix_tree_find (tree, +SSDFS_BTREE_ROOT_NODE_ID , + &node); +if (unlikely (err)) { +SSDFS_ERR ("fail to find root node: err %d\n" , + err); + goto finish_synchronize_root; + }else if (!node) { + err = -ERANGE ; +SSDFS_ERR ("node is NULL\n" ); + goto finish_synchronize_root; + } + +down_read (&node->header_lock); + height =atomic_read (&node->tree->height); + root->header.height = (u8 )height; + items_count = node->index_area.index_count; + root->header.items_count =cpu_to_le16 (items_count); + root->header.flags = (u8 )atomic_read (&node->flags); + root->header.type = (u8 )atomic_read (&node->type); +ssdfs_memcpy (root->header.node_ids, +0 , ids_array_size, + node->raw.root_node.header.node_ids, +0 , ids_array_size, + ids_array_size); +ssdfs_memcpy (root->indexes,0 , indexes_size, + node->raw.root_node.indexes,0 , indexes_size, + indexes_size); +up_read (&node->header_lock); + +spin_lock (&node->tree->nodes_lock); + root->header.upper_node_id = +cpu_to_le32 (node->tree->upper_node_id); +spin_unlock (&node->tree->nodes_lock); + +finish_synchronize_root: +up_read (&tree->lock); + +return err; +} + +/* +* ssdfs_btree_get_next_hash() - get next node's starting hash +* @tree: btree object +* @search: search object +* @next_hash: next node's starting hash [out] +*/ +int ssdfs_btree_get_next_hash (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search, +u64 *next_hash) +{ +struct ssdfs_btree_node *parent; +struct ssdfs_btree_node_index_area area; +struct ssdfs_btree_index_key index_key; +u64 old_hash =U64_MAX ; +int type; +spinlock_t *lock; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree || !search || !next_hash); +#endif /* CONFIG_SSDFS_DEBUG */ + + old_hash =le64_to_cpu (search->node.found_index.index.hash); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("search %p, next_hash %p, old (node %u, hash %llx)\n" , + search, next_hash, search->node.id, old_hash); +#endif /* CONFIG_SSDFS_DEBUG */ + + *next_hash =U64_MAX ; + + parent = search->node.parent; + +if (!parent) { +SSDFS_ERR ("node pointer is NULL\n" ); +return -ERANGE ; + } + + type =atomic_read (&parent->type); + +down_read (&tree->lock); + + do { + u16 found_pos; + + err = -ENOENT;
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.34.1
Part of the diff is not shown here. View the raw message.
RFC PATCH 50/76 ssdfs: introduce b-tree node object
In reply to Viacheslav DubeykoSSDFS file system uses a hybrid b-tree architecture with the goal to eliminate the index nodes’ side effect. The hybrid b-tree operates by three node types: (1) index node, (2) hybrid node, (3) leaf node. Generally speaking, the peculiarity of hybrid node is the mixture as index as data records into one node. Hybrid b-tree starts with root node that is capable to keep the two index records or two data records inline (if size of data record is equal or lesser than size of index record). If the b-tree needs to contain more than two items then it should be added the first hybrid node into the b-tree. The root level of b-tree is able to contain only two nodes because the root node is capable to store only two index records. Generally speaking, the initial goal of hybrid node is to store the data records in the presence of reserved index area.
--- fs/ssdfs/btree_node.c | 2176 +++++++++++++++++++++++++++++++++++++++++ fs/ssdfs/btree_node.h | 768 +++++++++++++++ 2 files changed, 2944 insertions(+) create mode 100644 fs/ssdfs/btree_node.c create mode 100644 fs/ssdfs/btree_node.h
fs/ssdfs/btree_node.c new file +2176 -0
@@ -0,0 +1,2176 @@ +// SPDX-License-Identifier: BSD-3-Clause-Clear +/* +* SSDFS -- SSD-oriented File System. +* +* fs/ssdfs/btree_node.c - generalized btree node implementation. +* +* Copyright (c) 2014-2019 HGST, a Western Digital Company. +* http://www.hgst.com/ +* Copyright (c) 2014-2023 Viacheslav Dubeyko <[email protected]> +* http://www.ssdfs.org/ +* +* (C) Copyright 2014-2019, HGST, Inc., All rights reserved. +* +* Created by HGST, San Jose Research Center, Storage Architecture Group +* +* Authors: Viacheslav Dubeyko <[email protected]> +* +* Acknowledgement: Cyril Guyot +* Zvonimir Bandic +*/ + +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/mm.h> +#include <linux/slab.h> +#include <linux/pagevec.h> + +#include "peb_mapping_queue.h" +#include "peb_mapping_table_cache.h" +#include "ssdfs.h" +#include "offset_translation_table.h" +#include "page_array.h" +#include "page_vector.h" +#include "peb_container.h" +#include "segment_bitmap.h" +#include "segment.h" +#include "extents_queue.h" +#include "btree_search.h" +#include "btree_node.h" +#include "btree.h" +#include "shared_extents_tree.h" +#include "diff_on_write.h" + +#ifdef CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING +atomic64_t ssdfs_btree_node_page_leaks; +atomic64_t ssdfs_btree_node_memory_leaks; +atomic64_t ssdfs_btree_node_cache_leaks; +#endif /* CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING */ + +/* +* void ssdfs_btree_node_cache_leaks_increment(void *kaddr) +* void ssdfs_btree_node_cache_leaks_decrement(void *kaddr) +* void *ssdfs_btree_node_kmalloc(size_t size, gfp_t flags) +* void *ssdfs_btree_node_kzalloc(size_t size, gfp_t flags) +* void *ssdfs_btree_node_kcalloc(size_t n, size_t size, gfp_t flags) +* void ssdfs_btree_node_kfree(void *kaddr) +* struct page *ssdfs_btree_node_alloc_page(gfp_t gfp_mask) +* struct page *ssdfs_btree_node_add_pagevec_page(struct pagevec *pvec) +* void ssdfs_btree_node_free_page(struct page *page) +* void ssdfs_btree_node_pagevec_release(struct pagevec *pvec) +*/ +#ifdef CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING + SSDFS_MEMORY_LEAKS_CHECKER_FNS(btree_node) +#else + SSDFS_MEMORY_ALLOCATOR_FNS(btree_node) +#endif /* CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING */ + +void ssdfs_btree_node_memory_leaks_init (void ) +{ +#ifdef CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING +atomic64_set (&ssdfs_btree_node_page_leaks,0 ); +atomic64_set (&ssdfs_btree_node_memory_leaks,0 ); +atomic64_set (&ssdfs_btree_node_cache_leaks,0 ); +#endif /* CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING */ +} + +void ssdfs_btree_node_check_memory_leaks (void ) +{ +#ifdef CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING +if (atomic64_read (&ssdfs_btree_node_page_leaks) !=0 ) { +SSDFS_ERR ("BTREE NODE: " +"memory leaks include %lld pages\n" , +atomic64_read (&ssdfs_btree_node_page_leaks)); + } + +if (atomic64_read (&ssdfs_btree_node_memory_leaks) !=0 ) { +SSDFS_ERR ("BTREE NODE: " +"memory allocator suffers from %lld leaks\n" , +atomic64_read (&ssdfs_btree_node_memory_leaks)); + } + +if (atomic64_read (&ssdfs_btree_node_cache_leaks) !=0 ) { +SSDFS_ERR ("BTREE NODE: " +"caches suffers from %lld leaks\n" , +atomic64_read (&ssdfs_btree_node_cache_leaks)); + } +#endif /* CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING */ +} + +/****************************************************************************** +* BTREE NODE CACHE * +******************************************************************************/ + +static struct kmem_cache *ssdfs_btree_node_obj_cachep; + +void ssdfs_zero_btree_node_obj_cache_ptr (void ) +{ + ssdfs_btree_node_obj_cachep =NULL ; +} + +static void ssdfs_init_btree_node_object_once (void *obj) +{ +struct ssdfs_btree_node *node_obj = obj; + +memset (node_obj,0 ,sizeof (struct ssdfs_btree_node )); +} + +void ssdfs_shrink_btree_node_obj_cache (void ) +{ +if (ssdfs_btree_node_obj_cachep) +kmem_cache_shrink (ssdfs_btree_node_obj_cachep); +} + +void ssdfs_destroy_btree_node_obj_cache (void ) +{ +if (ssdfs_btree_node_obj_cachep) +kmem_cache_destroy (ssdfs_btree_node_obj_cachep); +} + +int ssdfs_init_btree_node_obj_cache (void ) +{ + ssdfs_btree_node_obj_cachep = +kmem_cache_create ("ssdfs_btree_node_obj_cache" , +sizeof (struct ssdfs_btree_node ),0 , +SLAB_RECLAIM_ACCOUNT | +SLAB_MEM_SPREAD | +SLAB_ACCOUNT , + ssdfs_init_btree_node_object_once); +if (!ssdfs_btree_node_obj_cachep) { +SSDFS_ERR ("unable to create btree node objects cache\n" ); +return -ENOMEM ; + } + +return 0 ; +} + +/* +* ssdfs_btree_node_alloc() - allocate memory for btree node object +*/ +static +struct ssdfs_btree_node *ssdfs_btree_node_alloc (void ) +{ +struct ssdfs_btree_node *ptr; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!ssdfs_btree_node_obj_cachep); +#endif /* CONFIG_SSDFS_DEBUG */ + + ptr =kmem_cache_alloc (ssdfs_btree_node_obj_cachep,GFP_KERNEL ); +if (!ptr) { +SSDFS_ERR ("fail to allocate memory for btree node object\n" ); +return ERR_PTR (-ENOMEM ); + } + +ssdfs_btree_node_cache_leaks_increment (ptr); + +return ptr; +} + +/* +* ssdfs_btree_node_free() - free memory for btree node object +*/ +static +void ssdfs_btree_node_free (struct ssdfs_btree_node *ptr) +{ +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!ssdfs_btree_node_obj_cachep); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!ptr) +return ; + +ssdfs_btree_node_cache_leaks_decrement (ptr); +kmem_cache_free (ssdfs_btree_node_obj_cachep, ptr); +} + +/****************************************************************************** +* BTREE NODE OBJECT FUNCTIONALITY * +******************************************************************************/ + +/* +* ssdfs_btree_node_create_empty_index_area() - create empty index area +* @tree: btree object +* @node: node object +* @type: node's type +* @start_hash: starting hash of the node +* +* This method tries to create the empty index area. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +*/ +static +int ssdfs_btree_node_create_empty_index_area (struct ssdfs_btree *tree, +struct ssdfs_btree_node *node, +int type, +u64 start_hash) +{ +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree || !node); + +if (type <=SSDFS_BTREE_NODE_UNKNOWN_TYPE || + type >=SSDFS_BTREE_NODE_TYPE_MAX ) { +SSDFS_WARN ("invalid node type %#x\n" , type); +return -EINVAL ; + } + +SSDFS_DBG ("tree %p, node %p, " +"type %#x, start_hash %llx\n" , + tree, node, type, start_hash); +#endif /* CONFIG_SSDFS_DEBUG */ + +memset (&node->index_area,0xFF , +sizeof (struct ssdfs_btree_node_index_area )); + +switch (type) { +case SSDFS_BTREE_ROOT_NODE : +atomic_set (&node->index_area.state, +SSDFS_BTREE_NODE_INDEX_AREA_EXIST ); + node->index_area.offset = + offsetof(struct ssdfs_btree_inline_root_node , indexes); + node->index_area.index_size =sizeof (struct ssdfs_btree_index ); + node->index_area.index_capacity = +SSDFS_BTREE_ROOT_NODE_INDEX_COUNT ; + node->index_area.area_size = node->index_area.index_size; + node->index_area.area_size *= node->index_area.index_capacity; + node->index_area.index_count =0 ; + node->index_area.start_hash = start_hash; + node->index_area.end_hash =U64_MAX ; +break ; + +case SSDFS_BTREE_HYBRID_NODE : +case SSDFS_BTREE_INDEX_NODE : +/* +* Partial preliminary initialization. +* The final creation should be done in specialized +* tree->btree_ops->create_node() and +* tree->btree_ops->init_node() methods. +*/ +atomic_set (&node->index_area.state, +SSDFS_BTREE_NODE_INDEX_AREA_EXIST ); +atomic_or (SSDFS_BTREE_NODE_HAS_INDEX_AREA , &node->flags); + node->index_area.index_size = +sizeof (struct ssdfs_btree_index_key ); + node->index_area.index_count =0 ; + node->index_area.start_hash = start_hash; + node->index_area.end_hash =U64_MAX ; +break ; + +case SSDFS_BTREE_LEAF_NODE : +atomic_set (&node->index_area.state, +SSDFS_BTREE_NODE_AREA_ABSENT ); + node->index_area.index_size =0 ; + node->index_area.index_capacity =0 ; + node->index_area.area_size =0 ; + node->index_area.index_count =0 ; +break ; + +default : +SSDFS_WARN ("invalid node type %#x\n" , type); +return -EINVAL ; + } + +return 0 ; +} + +/* +* ssdfs_btree_node_create_empty_items_area() - create empty items area +* @tree: btree object +* @node: node object +* @type: node's type +* @start_hash: starting hash of the node +* +* This method tries to create the empty index area. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +*/ +static +int ssdfs_btree_node_create_empty_items_area (struct ssdfs_btree *tree, +struct ssdfs_btree_node *node, +int type, +u64 start_hash) +{ +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree || !node); + +if (type <=SSDFS_BTREE_NODE_UNKNOWN_TYPE || + type >=SSDFS_BTREE_NODE_TYPE_MAX ) { +SSDFS_WARN ("invalid node type %#x\n" , type); +return -EINVAL ; + } + +SSDFS_DBG ("tree %p, node %p, " +"type %#x, start_hash %llx\n" , + tree, node, type, start_hash); +#endif /* CONFIG_SSDFS_DEBUG */ + +memset (&node->items_area,0xFF , +sizeof (struct ssdfs_btree_node_items_area )); + +switch (type) { +case SSDFS_BTREE_ROOT_NODE : +case SSDFS_BTREE_INDEX_NODE : +atomic_set (&node->items_area.state, +SSDFS_BTREE_NODE_AREA_ABSENT ); + node->items_area.area_size =0 ; + node->items_area.item_size =0 ; + node->items_area.min_item_size =0 ; + node->items_area.max_item_size =0 ; + node->items_area.items_count =0 ; + node->items_area.items_capacity =0 ; +break ; + +case SSDFS_BTREE_HYBRID_NODE : +/* +* Partial preliminary initialization. +* The final creation should be done in specialized +* tree->btree_ops->create_node() and +* tree->btree_ops->init_node() methods. +*/ +atomic_set (&node->items_area.state, +SSDFS_BTREE_NODE_ITEMS_AREA_EXIST ); +atomic_or (SSDFS_BTREE_NODE_HAS_ITEMS_AREA , &node->flags); + node->items_area.item_size = tree->item_size; + node->items_area.min_item_size = tree->min_item_size; + node->items_area.max_item_size = tree->max_item_size; + node->items_area.start_hash = start_hash; + node->items_area.end_hash = start_hash; +break ; + +case SSDFS_BTREE_LEAF_NODE : +atomic_set (&node->items_area.state, +SSDFS_BTREE_NODE_ITEMS_AREA_EXIST ); +atomic_or (SSDFS_BTREE_NODE_HAS_ITEMS_AREA , &node->flags); + node->items_area.item_size = tree->item_size; + node->items_area.min_item_size = tree->min_item_size; + node->items_area.max_item_size = tree->max_item_size; + node->items_area.start_hash = start_hash; + node->items_area.end_hash = start_hash; +break ; + +default : +SSDFS_WARN ("invalid node type %#x\n" , type); +return -EINVAL ; + } + +return 0 ; +} + +/* +* ssdfs_btree_node_create_empty_lookup_table() - create empty lookup table +* @node: node object +* +* This method tries to create the empty lookup table area. +*/ +static +void ssdfs_btree_node_create_empty_lookup_table (struct ssdfs_btree_node *node) +{ +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node); + +SSDFS_DBG ("node %p\n" , node); +#endif /* CONFIG_SSDFS_DEBUG */ + +memset (&node->lookup_tbl_area,0xFF , +sizeof (struct ssdfs_btree_node_index_area )); + +atomic_set (&node->lookup_tbl_area.state, +SSDFS_BTREE_NODE_AREA_ABSENT ); + node->lookup_tbl_area.index_size =0 ; + node->lookup_tbl_area.index_capacity =0 ; + node->lookup_tbl_area.area_size =0 ; + node->lookup_tbl_area.index_count =0 ; +} + +/* +* ssdfs_btree_node_create_empty_hash_table() - create empty hash table +* @node: node object +* +* This method tries to create the empty hash table area. +*/ +static +void ssdfs_btree_node_create_empty_hash_table (struct ssdfs_btree_node *node) +{ +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node); + +SSDFS_DBG ("node %p\n" , node); +#endif /* CONFIG_SSDFS_DEBUG */ + +memset (&node->hash_tbl_area,0xFF , +sizeof (struct ssdfs_btree_node_index_area )); + +atomic_set (&node->hash_tbl_area.state, +SSDFS_BTREE_NODE_AREA_ABSENT ); + node->hash_tbl_area.index_size =0 ; + node->hash_tbl_area.index_capacity =0 ; + node->hash_tbl_area.area_size =0 ; + node->hash_tbl_area.index_count =0 ; +} + +/* +* ssdfs_btree_node_create() - create btree node object +* @tree: btree object +* @node_id: node ID number +* @parent: parent node +* @height: node's height +* @type: node's type +* @start_hash: starting hash of the node +* +* This method tries to create a btree node object. +* +* RETURN: +* [success] - pointer on created btree node object. +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ENOMEM - cannot allocate memory. +* %-ERANGE - internal error. +*/ +struct ssdfs_btree_node * +ssdfs_btree_node_create (struct ssdfs_btree *tree, +u32 node_id, +struct ssdfs_btree_node *parent, +u8 height,int type, +u64 start_hash) +{ +struct ssdfs_fs_info *fsi; +struct ssdfs_btree_node *ptr; +u8 tree_height; +int i; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!tree || !tree->fsi); + +if (type <=SSDFS_BTREE_NODE_UNKNOWN_TYPE || + type >=SSDFS_BTREE_NODE_TYPE_MAX ) { +SSDFS_WARN ("invalid node type %#x\n" , type); +return ERR_PTR (-EINVAL ); + } + +if (type !=SSDFS_BTREE_ROOT_NODE && !parent) { +SSDFS_WARN ("node %u should have parent\n" , + node_id); +return ERR_PTR (-EINVAL ); + } +#endif /* CONFIG_SSDFS_DEBUG */ + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("tree %p, parent %p, node_id %u, " +"height %u, type %#x, start_hash %llx\n" , + tree, parent, node_id, height, + type, start_hash); +#else +SSDFS_DBG ("tree %p, parent %p, node_id %u, " +"height %u, type %#x, start_hash %llx\n" , + tree, parent, node_id, height, + type, start_hash); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + + fsi = tree->fsi; + + ptr =ssdfs_btree_node_alloc (); +if (!ptr) { +SSDFS_ERR ("fail to allocate btree node object\n" ); +return ERR_PTR (-ENOMEM ); + } + + ptr->parent_node = parent; + ptr->tree = tree; + +if (node_id ==SSDFS_BTREE_NODE_INVALID_ID ) { + err = -EINVAL ; +SSDFS_WARN ("invalid node_id\n" ); + goto fail_create_node; + } + ptr->node_id = node_id; + + tree_height =atomic_read (&tree->height); +if (height > tree_height) { + err = -EINVAL ; +SSDFS_WARN ("height %u > tree->height %u\n" , + height, tree_height); + goto fail_create_node; + } + +atomic_set (&ptr->height, height); + +#ifdef CONFIG_SSDFS_DEBUG +if (tree->node_size < fsi->pagesize || + tree->node_size > fsi->erasesize) { + err = -EINVAL ; +SSDFS_WARN ("invalid node_size %u, " +"pagesize %u, erasesize %u\n" , + tree->node_size, + fsi->pagesize, + fsi->erasesize); + goto fail_create_node; + } +#endif /* CONFIG_SSDFS_DEBUG */ + ptr->node_size = tree->node_size; + +#ifdef CONFIG_SSDFS_DEBUG +if (tree->pages_per_node != (ptr->node_size / fsi->pagesize)) { + err = -EINVAL ; +SSDFS_WARN ("invalid pages_per_node %u, " +"node_size %u, pagesize %u\n" , + tree->pages_per_node, + ptr->node_size, + fsi->pagesize); + goto fail_create_node; + } +#endif /* CONFIG_SSDFS_DEBUG */ + ptr->pages_per_node = tree->pages_per_node; + + ptr->create_cno =ssdfs_current_cno (fsi->sb); + ptr->node_ops =NULL ; + +atomic_set (&ptr->refs_count,0 ); +atomic_set (&ptr->flags,0 ); +atomic_set (&ptr->type, type); + +init_rwsem (&ptr->header_lock); +memset (&ptr->raw,0xFF ,sizeof (ptr->raw)); + + err =ssdfs_btree_node_create_empty_index_area (tree, ptr, + type, + start_hash); +if (unlikely (err)) { +SSDFS_ERR ("fail to create empty index area: err %d\n" , + err); + goto fail_create_node; + } + + err =ssdfs_btree_node_create_empty_items_area (tree, ptr, + type, + start_hash); +if (unlikely (err)) { +SSDFS_ERR ("fail to create empty items area: err %d\n" , + err); + goto fail_create_node; + } + +ssdfs_btree_node_create_empty_lookup_table (ptr); +ssdfs_btree_node_create_empty_hash_table (ptr); + +spin_lock_init (&ptr->descriptor_lock); + ptr->update_cno = ptr->create_cno; + +/* +* Partial preliminary initialization. +* The final creation should be done in specialized +* tree->btree_ops->create_node() and +* tree->btree_ops->init_node() methods. +*/ +memset (&ptr->extent,0xFF ,sizeof (struct ssdfs_raw_extent )); + ptr->seg =NULL ; + + ptr->node_index.node_id =cpu_to_le32 (node_id); + ptr->node_index.node_type = (u8 )type; + ptr->node_index.height = height; + ptr->node_index.flags =cpu_to_le16 (SSDFS_BTREE_INDEX_SHOW_EMPTY_NODE ); + ptr->node_index.index.hash =cpu_to_le64 (start_hash); + +init_completion (&ptr->init_end); + +/* +* Partial preliminary initialization. +* The final creation should be done in specialized +* tree->btree_ops->create_node() and +* tree->btree_ops->init_node() methods. +*/ +init_rwsem (&ptr->bmap_array.lock); + ptr->bmap_array.bits_count =0 ; + ptr->bmap_array.bmap_bytes =0 ; + ptr->bmap_array.index_start_bit =ULONG_MAX ; + ptr->bmap_array.item_start_bit =ULONG_MAX ; +for (i =0 ; i <SSDFS_BTREE_NODE_BMAP_COUNT ; i++) { +spin_lock_init (&ptr->bmap_array.bmap[i].lock); + ptr->bmap_array.bmap[i].flags =0 ; + ptr->bmap_array.bmap[i].ptr =NULL ; + } + +init_waitqueue_head (&ptr->wait_queue); +init_rwsem (&ptr->full_lock); + +atomic_set (&ptr->state,SSDFS_BTREE_NODE_CREATED ); + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("finished\n" ); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + +return ptr; + +fail_create_node: + ptr->parent_node =NULL ; + ptr->tree =NULL ; +ssdfs_btree_node_free (ptr); +return ERR_PTR (err); +} + +/* +* ssdfs_btree_create_root_node() - create root node +* @node: node object +* @root_node: pointer on the on-disk root node object +* +* This method tries to create the root node of the btree. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EIO - corrupted root node object. +*/ +int ssdfs_btree_create_root_node (struct ssdfs_btree_node *node, +struct ssdfs_btree_inline_root_node *root_node) +{ +struct ssdfs_btree_root_node_header *hdr; +struct ssdfs_btree_index *index1, *index2; +size_t rnode_size =sizeof (struct ssdfs_btree_inline_root_node ); +u8 height; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !node->tree || !root_node); + +SSDFS_DBG ("node %p, root_node %p\n" , + node, root_node); +#endif /* CONFIG_SSDFS_DEBUG */ + + hdr = &root_node->header; + +if (hdr->type !=SSDFS_BTREE_ROOT_NODE ) { +SSDFS_ERR ("invalid node type %#x\n" , + hdr->type); +return -EIO ; + } + +if (hdr->items_count >SSDFS_BTREE_ROOT_NODE_INDEX_COUNT ) { +SSDFS_ERR ("invalid items_count %u\n" , + hdr->items_count); +return -EIO ; + } + + height = hdr->height; + +if (height >=U8_MAX ) { +SSDFS_ERR ("invalid height %u\n" , + height); +return -EIO ; + } + +if (le32_to_cpu (hdr->upper_node_id) ==0 ) { + height =1 ; +atomic_set (&node->tree->height, height); +atomic_set (&node->height, height -1 ); + }else { +if (height ==0 ) { +SSDFS_ERR ("invalid height %u\n" , + height); +return -EIO ; + } + +atomic_set (&node->tree->height, height); +atomic_set (&node->height, height -1 ); + } + + node->node_size = rnode_size; + node->pages_per_node =0 ; + node->create_cno =le64_to_cpu (0 ); + node->tree->create_cno = node->create_cno; + node->node_id =SSDFS_BTREE_ROOT_NODE_ID ; + + node->parent_node =NULL ; + node->node_ops =NULL ; + +atomic_set (&node->flags, hdr->flags); +atomic_set (&node->type, hdr->type); + +down_write (&node->header_lock); +ssdfs_memcpy (&node->raw.root_node,0 , rnode_size, + root_node,0 , rnode_size, + rnode_size); + node->index_area.index_count = hdr->items_count; + node->index_area.start_hash =U64_MAX ; + node->index_area.end_hash =U64_MAX ; +if (hdr->items_count >0 ) { + index1 = &root_node->indexes[SSDFS_ROOT_NODE_LEFT_LEAF_NODE ]; + node->index_area.start_hash =le64_to_cpu (index1->hash); + } +if (hdr->items_count >1 ) { + index2 = &root_node->indexes[SSDFS_ROOT_NODE_RIGHT_LEAF_NODE ]; + node->index_area.end_hash =le64_to_cpu (index2->hash); + } +up_write (&node->header_lock); + +spin_lock (&node->tree->nodes_lock); + node->tree->upper_node_id = +le32_to_cpu (root_node->header.upper_node_id); +spin_unlock (&node->tree->nodes_lock); + +atomic_set (&node->state,SSDFS_BTREE_NODE_INITIALIZED ); +return 0 ; +} + +/* +* ssdfs_btree_node_allocate_content_space() - allocate content space +* @node: node object +* @node_size: node size +*/ +int ssdfs_btree_node_allocate_content_space (struct ssdfs_btree_node *node, +u32 node_size) +{ +struct page *page; +u32 pages_count; +int i; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !node->tree); + +SSDFS_DBG ("node_id %u, node_size %u\n" , + node->node_id, node_size); +#endif /* CONFIG_SSDFS_DEBUG */ + + pages_count = node_size /PAGE_SIZE ; + +if (pages_count ==0 || pages_count >PAGEVEC_SIZE ) { +SSDFS_ERR ("invalid pages_count %u\n" , + pages_count); +return -ERANGE ; + } + +down_write (&node->full_lock); + +pagevec_init (&node->content.pvec); +for (i =0 ; i < pages_count; i++) { + page =ssdfs_btree_node_alloc_page (GFP_KERNEL | __GFP_ZERO); +if (IS_ERR_OR_NULL (page)) { + err = (page ==NULL ? -ENOMEM :PTR_ERR (page)); +SSDFS_ERR ("unable to allocate memory page\n" ); + goto finish_init_pvec; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("page %p, count %d\n" , + page,page_ref_count (page)); +#endif /* CONFIG_SSDFS_DEBUG */ + +pagevec_add (&node->content.pvec, page); + } + +finish_init_pvec: +up_write (&node->full_lock); + +return err; +} + +/* +* ssdfs_btree_node_allocate_bmaps() - allocate node's bitmaps +* @addr: array of pointers +* @bmap_bytes: size of the bitmap in bytes +*/ +int ssdfs_btree_node_allocate_bmaps (void *addr[SSDFS_BTREE_NODE_BMAP_COUNT ], +size_t bmap_bytes) +{ +int i; + +for (i =0 ; i <SSDFS_BTREE_NODE_BMAP_COUNT ; i++) { + addr[i] =ssdfs_btree_node_kzalloc (bmap_bytes,GFP_KERNEL ); +if (!addr[i]) { +SSDFS_ERR ("fail to allocate node's bmap: index %d\n" , + i); +for (; i >=0 ; i--) { +ssdfs_btree_node_kfree (addr[i]); + addr[i] =NULL ; + } +return -ENOMEM ; + } + } + +return 0 ; +} + +/* +* ssdfs_btree_node_init_bmaps() - init node's bitmaps +* @node: node object +* @addr: array of pointers +*/ +void ssdfs_btree_node_init_bmaps (struct ssdfs_btree_node *node, +void *addr[SSDFS_BTREE_NODE_BMAP_COUNT ]) +{ +int i; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node); +BUG_ON (!rwsem_is_locked (&node->bmap_array.lock)); +#endif /* CONFIG_SSDFS_DEBUG */ + +for (i =0 ; i <SSDFS_BTREE_NODE_BMAP_COUNT ; i++) { +void *tmp_addr =NULL ; + +spin_lock (&node->bmap_array.bmap[i].lock); + tmp_addr = node->bmap_array.bmap[i].ptr; + node->bmap_array.bmap[i].ptr = addr[i]; + addr[i] =NULL ; +spin_unlock (&node->bmap_array.bmap[i].lock); + +if (tmp_addr) +ssdfs_btree_node_kfree (tmp_addr); + } +} + +/* +* ssdfs_btree_node_destroy() - destroy the btree node +* @node: node object +*/ +void ssdfs_btree_node_destroy (struct ssdfs_btree_node *node) +{ +int i; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node); +#endif /* CONFIG_SSDFS_DEBUG */ + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("node_id %u, height %u, type %#x\n" , + node->node_id,atomic_read (&node->height), +atomic_read (&node->type)); +#else +SSDFS_DBG ("node_id %u, height %u, type %#x\n" , + node->node_id,atomic_read (&node->height), +atomic_read (&node->type)); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + +switch (atomic_read (&node->state)) { +case SSDFS_BTREE_NODE_DIRTY : +switch (atomic_read (&node->type)) { +case SSDFS_BTREE_ROOT_NODE : +/* ignore root node dirty state */ +break ; + +default : +SSDFS_WARN ("node %u is dirty\n" , node->node_id); +break ; + } +/* FALLTHRU */ + fallthrough; +case SSDFS_BTREE_NODE_CREATED : +/* FALLTHRU */ + fallthrough; +case SSDFS_BTREE_NODE_INITIALIZED : +atomic_set (&node->state,SSDFS_BTREE_NODE_UNKNOWN_STATE ); +wake_up_all (&node->wait_queue); +complete_all (&node->init_end); + +spin_lock (&node->descriptor_lock); +if (node->seg) { +ssdfs_segment_put_object (node->seg); + node->seg =NULL ; + } +spin_unlock (&node->descriptor_lock); + +if (rwsem_is_locked (&node->bmap_array.lock)) { +/* inform about possible trouble */ +SSDFS_WARN ("node is locked under destruction\n" ); + } + + node->bmap_array.bits_count =0 ; + node->bmap_array.bmap_bytes =0 ; + node->bmap_array.index_start_bit =ULONG_MAX ; + node->bmap_array.item_start_bit =ULONG_MAX ; +for (i =0 ; i <SSDFS_BTREE_NODE_BMAP_COUNT ; i++) { +spin_lock (&node->bmap_array.bmap[i].lock); +ssdfs_btree_node_kfree (node->bmap_array.bmap[i].ptr); + node->bmap_array.bmap[i].ptr =NULL ; +spin_unlock (&node->bmap_array.bmap[i].lock); + } + +if (rwsem_is_locked (&node->full_lock)) { +/* inform about possible trouble */ +SSDFS_WARN ("node is locked under destruction\n" ); + } + +if (atomic_read (&node->type) !=SSDFS_BTREE_ROOT_NODE ) { +struct pagevec *pvec = &node->content.pvec; +ssdfs_btree_node_pagevec_release (pvec); + } +break ; + +default : +SSDFS_WARN ("invalid node state: " +"node %u, state %#x\n" , + node->node_id, +atomic_read (&node->state)); +break ; + } + +ssdfs_btree_node_free (node); + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("finished\n" ); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ +} + +/* +* __ssdfs_btree_node_prepare_content() - prepare the btree node's content +* @fsi: pointer on shared file system object +* @ptr: btree node's index +* @node_size: size of the node +* @owner_ino: owner inode ID +* @si: segment object [out] +* @pvec: pagevec with node's content [out] +* +* This method tries to read the raw node from the volume. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +* %-ENOMEM - fail to allocate memory. +*/ +int __ssdfs_btree_node_prepare_content (struct ssdfs_fs_info *fsi, +struct ssdfs_btree_index_key *ptr, +u32 node_size, +u64 owner_ino, +struct ssdfs_segment_info **si, +struct pagevec *pvec) +{ +struct ssdfs_segment_request *req; +struct ssdfs_peb_container *pebc; +struct ssdfs_blk2off_table *table; +struct ssdfs_offset_position pos; +u32 node_id; +u8 node_type; +u8 height; +u64 seg_id; +u32 logical_blk; +u32 len; +u32 pvec_size; +u64 logical_offset; +u32 data_bytes; +struct completion *end; +int i; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!fsi || !ptr || !si || !pvec); +#endif /* CONFIG_SSDFS_DEBUG */ + + node_id =le32_to_cpu (ptr->node_id); + node_type = ptr->node_type; + height = ptr->height; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node_id %u, node_size %u, height %u, type %#x\n" , + node_id, node_size, height, node_type); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (node_type <=SSDFS_BTREE_NODE_UNKNOWN_TYPE || + node_type >=SSDFS_BTREE_NODE_TYPE_MAX ) { +SSDFS_WARN ("invalid node type %#x\n" , + node_type); +return -ERANGE ; + } + +if (node_type ==SSDFS_BTREE_ROOT_NODE ) { +SSDFS_WARN ("root node should be initialize during creation\n" ); +return -ERANGE ; + } + + seg_id =le64_to_cpu (ptr->index.extent.seg_id); + logical_blk =le32_to_cpu (ptr->index.extent.logical_blk); + len =le32_to_cpu (ptr->index.extent.len); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("seg_id %llu, logical_blk %u, len %u\n" , + seg_id, logical_blk, len); + +BUG_ON (seg_id ==U64_MAX ); +#endif /* CONFIG_SSDFS_DEBUG */ + + *si =ssdfs_grab_segment (fsi,NODE2SEG_TYPE (node_type), + seg_id,U64_MAX ); +if (unlikely (IS_ERR_OR_NULL (*si))) { + err = !*si ? -ENOMEM :PTR_ERR (*si); +if (err == -EINTR ) { +/* +* Ignore this error. +*/ + }else { +SSDFS_ERR ("fail to grab segment object: " +"seg %llu, err %d\n" , + seg_id, err); + } + goto fail_get_segment; + } + + pvec_size = node_size >>PAGE_SHIFT ; + +if (pvec_size ==0 || pvec_size >PAGEVEC_SIZE ) { + err = -ERANGE ; +SSDFS_WARN ("invalid memory pages count: " +"node_size %u, pvec_size %u\n" , + node_size, pvec_size); + goto finish_prepare_content; + } + + req =ssdfs_request_alloc (); +if (IS_ERR_OR_NULL (req)) { + err = (req ==NULL ? -ENOMEM :PTR_ERR (req)); +SSDFS_ERR ("fail to allocate segment request: err %d\n" , + err); + goto finish_prepare_content; + } + +ssdfs_request_init (req); +ssdfs_get_request (req); + + logical_offset = (u64 )node_id * node_size; + data_bytes = node_size; +ssdfs_request_prepare_logical_extent (owner_ino, + (u64 )logical_offset, + (u32 )data_bytes, +0 ,0 , req); + +for (i =0 ; i < pvec_size; i++) { + err =ssdfs_request_add_allocated_page_locked (req); +if (unlikely (err)) { +SSDFS_ERR ("fail to add page into request: " +"err %d\n" , + err); + goto fail_read_node; + } + } + +ssdfs_request_define_segment (seg_id, req); + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (logical_blk >=U16_MAX ); +BUG_ON (len >=U16_MAX ); +#endif /* CONFIG_SSDFS_DEBUG */ +ssdfs_request_define_volume_extent ((u16 )logical_blk, (u16 )len, req); + +ssdfs_request_prepare_internal_data (SSDFS_PEB_READ_REQ , +SSDFS_READ_PAGES_READAHEAD , +SSDFS_REQ_SYNC , + req); + + table = (*si)->blk2off_table; + + err =ssdfs_blk2off_table_get_offset_position (table, logical_blk, &pos); +if (err == -EAGAIN ) { + end = &table->full_init_end; + + err =SSDFS_WAIT_COMPLETION (end); +if (unlikely (err)) { +SSDFS_ERR ("blk2off init failed: " +"seg_id %llu, logical_blk %u, " +"len %u, err %d\n" , + seg_id, logical_blk, len, err); + +for (i =0 ; i < (*si)->pebs_count; i++) { +u64 peb_id1 =U64_MAX ; +u64 peb_id2 =U64_MAX ; + + pebc = &(*si)->peb_array[i]; + +if (pebc->src_peb) + peb_id1 = pebc->src_peb->peb_id; + +if (pebc->dst_peb) + peb_id2 = pebc->dst_peb->peb_id; + +SSDFS_ERR ("seg_id %llu, peb_index %u, " +"src_peb %llu, dst_peb %llu\n" , + seg_id, i, peb_id1, peb_id2); + } + +#ifdef CONFIG_SSDFS_DEBUG +BUG (); +#endif /* CONFIG_SSDFS_DEBUG */ + + goto fail_read_node; + } + + err =ssdfs_blk2off_table_get_offset_position (table, + logical_blk, + &pos); + } + +if (unlikely (err)) { +SSDFS_ERR ("fail to convert: " +"seg_id %llu, logical_blk %u, len %u, err %d\n" , + seg_id, logical_blk, len, err); + goto fail_read_node; + } + + pebc = &(*si)->peb_array[pos.peb_index]; + + err =ssdfs_peb_readahead_pages (pebc, req, &end); +if (err == -EAGAIN ) { + err =SSDFS_WAIT_COMPLETION (end); +if (unlikely (err)) { +SSDFS_ERR ("PEB init failed: " +"err %d\n" , err); + goto fail_read_node; + } + + err =ssdfs_peb_readahead_pages (pebc, req, &end); + } + +if (unlikely (err)) { +SSDFS_ERR ("fail to read page: err %d\n" , + err); + goto fail_read_node; + } + +for (i =0 ; i < req->result.processed_blks; i++) +ssdfs_peb_mark_request_block_uptodate (pebc, req, i); + +#ifdef CONFIG_SSDFS_DEBUG +for (i =0 ; i <pagevec_count (&req->result.pvec); i++) { +void *kaddr; +struct page *page = req->result.pvec.pages[i]; + + kaddr =kmap_local_page (page); +SSDFS_DBG ("PAGE DUMP: index %d\n" , + i); +print_hex_dump_bytes ("" ,DUMP_PREFIX_OFFSET , + kaddr, +PAGE_SIZE ); +SSDFS_DBG ("\n" ); +kunmap_local (kaddr); + +WARN_ON (!PageLocked (page)); + } +#endif /* CONFIG_SSDFS_DEBUG */ + +ssdfs_btree_node_pagevec_release (pvec); + +for (i =0 ; i <pagevec_count (&req->result.pvec); i++) { +pagevec_add (pvec, req->result.pvec.pages[i]); +ssdfs_btree_node_account_page (req->result.pvec.pages[i]); +ssdfs_request_unlock_and_remove_page (req, i); + } +pagevec_reinit (&req->result.pvec); + +ssdfs_request_unlock_and_remove_diffs (req); + +ssdfs_put_request (req); +ssdfs_request_free (req); + +return 0 ; + +fail_read_node: +ssdfs_request_unlock_and_remove_pages (req); +ssdfs_put_request (req); +ssdfs_request_free (req); + +finish_prepare_content: +ssdfs_segment_put_object (*si); + +fail_get_segment: +return err; +} + +/* +* ssdfs_btree_node_prepare_content() - prepare the btree node's content +* @node: node object +* @ptr: btree node's index +* +* This method tries to read the raw node from the volume. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +*/ +int ssdfs_btree_node_prepare_content (struct ssdfs_btree_node *node, +struct ssdfs_btree_index_key *ptr) +{ +struct ssdfs_segment_info *si =NULL ; +size_t extent_size =sizeof (struct ssdfs_raw_extent ); +u32 node_id; +u8 node_type; +u8 height; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !ptr); + +SSDFS_DBG ("node_id %u, height %u, type %#x\n" , + node->node_id,atomic_read (&node->height), +atomic_read (&node->type)); +#endif /* CONFIG_SSDFS_DEBUG */ + + node_id =le32_to_cpu (ptr->node_id); + node_type = ptr->node_type; + height = ptr->height; + +#ifdef CONFIG_SSDFS_DEBUG +if (node->node_id != node_id) { +SSDFS_WARN ("node->node_id %u != node_id %u\n" , + node->node_id, node_id); +return -EINVAL ; + } + +if (atomic_read (&node->type) != node_type) { +SSDFS_WARN ("node->type %#x != node_type %#x\n" , +atomic_read (&node->type), node_type); +return -EINVAL ; + } + +if (atomic_read (&node->height) != height) { +SSDFS_WARN ("node->height %u != height %u\n" , +atomic_read (&node->height), height); +return -EINVAL ; + } +#endif /* CONFIG_SSDFS_DEBUG */ + +down_write (&node->full_lock); + err =__ssdfs_btree_node_prepare_content (node->tree->fsi, ptr, + node->node_size, + node->tree->owner_ino, + &si, + &node->content.pvec); +up_write (&node->full_lock); + +if (err == -EINTR ) { +/* +* Ignore this error. +*/ + goto finish_prepare_node_content; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to prepare node's content: " +"node_id %u, err %d\n" , + node->node_id, err); + goto finish_prepare_node_content; + } + +spin_lock (&node->descriptor_lock); +ssdfs_memcpy (&node->extent,0 , extent_size, + &ptr->index.extent,0 , extent_size, + extent_size); + node->seg = si; +spin_unlock (&node->descriptor_lock); + +atomic_set (&node->state,SSDFS_BTREE_NODE_CONTENT_PREPARED ); + +finish_prepare_node_content: +return err; +} + +/* +* __ssdfs_define_memory_page() - define memory page for the position +* @area_offset: area offset from the node's beginning +* @area_size: size of the area +* @node_size: node size in bytes +* @item_size: size of the item in bytes +* @position: position of index record in the node +* @page_index: index of memory page in the node [out] +* @page_off: offset from the memory page's beginning in bytes [out] +* +* This method tries to define a memory page's index and byte +* offset to the index record. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +*/ +int __ssdfs_define_memory_page (u32 area_offset,u32 area_size, +u32 node_size,size_t item_size, +u16 position, +u32 *page_index,u32 *page_off) +{ +u32 item_offset; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!page_index || !page_off); + +SSDFS_DBG ("area_offset %u, area_size %u, " +"node_size %u, item_size %zu, position %u\n" , + area_offset, area_size, + node_size, item_size, position); +#endif /* CONFIG_SSDFS_DEBUG */ + + *page_index =U32_MAX ; + *page_off =U32_MAX ; + + item_offset = position * item_size; +if (item_offset >= area_size) { +SSDFS_ERR ("item_offset %u >= area_size %u\n" , + item_offset, area_size); +return -ERANGE ; + } + + item_offset += area_offset; + +if (item_offset >= (area_offset + area_size)) { +SSDFS_ERR ("invalid index offset: " +"item_offset %u, area_offset %u, " +"area_size %u\n" , + item_offset, area_offset, area_size); +return -ERANGE ; + } + + *page_index = item_offset >>PAGE_SHIFT ; + *page_off = item_offset %PAGE_SIZE ; + +if ((*page_off + item_size) >PAGE_SIZE ) { +SSDFS_ERR ("invalid request: " +"page_off %u, item_size %zu\n" , + *page_off, item_size); +return -ERANGE ; + } + +return 0 ; +} + +/* +* ssdfs_define_memory_page() - define memory page for the position +* @node: node object +* @area: pointer on index area descriptor +* @position: position of index record in the node +* @page_index: index of memory page in the node [out] +* @page_off: offset from the memory page's beginning in bytes [out] +* +* This method tries to define a memory page's index and byte +* offset to the index record. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +*/ +static +int ssdfs_define_memory_page (struct ssdfs_btree_node *node, +struct ssdfs_btree_node_index_area *area, +u16 position, +u32 *page_index,u32 *page_off) +{ +size_t index_size =sizeof (struct ssdfs_btree_index_key ); +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !area || !page_index || !page_off); +BUG_ON (!rwsem_is_locked (&node->full_lock)); +BUG_ON (!rwsem_is_locked (&node->tree->lock)); + +SSDFS_DBG ("node_id %u, node_type %#x, position %u\n" , + node->node_id,atomic_read (&node->type), + position); +#endif /* CONFIG_SSDFS_DEBUG */ + + *page_index =U32_MAX ; + *page_off =U32_MAX ; + +if (atomic_read (&area->state) !=SSDFS_BTREE_NODE_INDEX_AREA_EXIST ) { +SSDFS_ERR ("invalid area state %#x\n" , +atomic_read (&area->state)); +return -ERANGE ; + } + +if (area->index_capacity ==0 || + area->index_count > area->index_capacity) { +SSDFS_ERR ("invalid area: " +"index_count %u, index_capacity %u\n" , + area->index_count, + area->index_capacity); +return -ERANGE ; + } + +if (position > area->index_count) { +SSDFS_ERR ("position %u > index_count %u\n" , + position, area->index_count); +return -ERANGE ; + } + +if ((area->offset + area->area_size) > node->node_size) { +SSDFS_ERR ("invalid area: " +"offset %u, area_size %u, node_size %u\n" , + area->offset, + area->area_size, + node->node_size); +return -ERANGE ; + } + +if (area->index_size != index_size) { +SSDFS_ERR ("invalid index size %u\n" , + area->index_size); +return -ERANGE ; + } + + err =__ssdfs_define_memory_page (area->offset, area->area_size, + node->node_size, index_size, + position, page_index, page_off); +if (unlikely (err)) { +SSDFS_ERR ("fail to define page index: err %d\n" , + err); +return err; + } + +if ((*page_off + area->index_size) >PAGE_SIZE ) { +SSDFS_ERR ("invalid offset into the page: " +"offset %u, index_size %u\n" , + *page_off, area->index_size); +return -ERANGE ; + } + +if (*page_index >=pagevec_count (&node->content.pvec)) { +SSDFS_ERR ("invalid page index: " +"page_index %u, pagevec_count %u\n" , + *page_index, +pagevec_count (&node->content.pvec)); +return -ERANGE ; + } + +return 0 ; +} + +/* +* __ssdfs_init_index_area_hash_range() - extract hash range of index area +* @node: node object +* @index_count: count of indexes in the node +* @start_hash: starting hash of index area [out] +* @end_hash: ending hash of index area [out] +* +* This method tries to extract start and end hash from +* the raw index area. +* + +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +*/ +static +int __ssdfs_init_index_area_hash_range (struct ssdfs_btree_node *node, +u16 index_count, +u64 *start_hash,u64 *end_hash) +{ +struct ssdfs_btree_index_key *ptr; +struct page *page; +void *kaddr; +u32 page_index; +u32 page_off; +u16 position; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !node->tree); +BUG_ON (!start_hash || !end_hash); +BUG_ON (!rwsem_is_locked (&node->full_lock)); +BUG_ON (!rwsem_is_locked (&node->header_lock)); + +SSDFS_DBG ("node_id %u, height %u\n" , + node->node_id, +atomic_read (&node->height)); +#endif /* CONFIG_SSDFS_DEBUG */ + + *start_hash =U64_MAX ; + *end_hash =U64_MAX ; + +if (index_count ==0 ) +return 0 ; + + position =0 ; + + err =ssdfs_define_memory_page (node, &node->index_area, + position, + &page_index, &page_off); +if (unlikely (err)) { +SSDFS_ERR ("fail to define memory page: " +"node_id %u, position %u, err %d\n" , + node->node_id, position, err); +return err; + } + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (page_index >=U32_MAX ); +BUG_ON (page_off >=U32_MAX ); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (page_index >=pagevec_count (&node->content.pvec)) { +SSDFS_ERR ("page_index %u > pvec_size %u\n" , + page_index, +pagevec_count (&node->content.pvec)); +return -ERANGE ; + } + + page = node->content.pvec.pages[page_index]; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!page); +#endif /* CONFIG_SSDFS_DEBUG */ + +ssdfs_lock_page (page); + kaddr =kmap_local_page (page); + ptr = (struct ssdfs_btree_index_key *)((u8 *)kaddr + page_off); + *start_hash =le64_to_cpu (ptr->index.hash); +kunmap_local (kaddr); +ssdfs_unlock_page (page); + + position = index_count -1 ; + +if (position ==0 ) { + *end_hash = *start_hash; +return 0 ; + } + + err =ssdfs_define_memory_page (node, &node->index_area, + position, + &page_index, &page_off); +if (unlikely (err)) { +SSDFS_ERR ("fail to define memory page: " +"node_id %u, position %u, err %d\n" , + node->node_id, position, err); +return err; + } + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (page_index >=U32_MAX ); +BUG_ON (page_off >=U32_MAX ); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (page_index >=pagevec_count (&node->content.pvec)) { +SSDFS_ERR ("page_index %u > pvec_size %u\n" , + page_index, +pagevec_count (&node->content.pvec)); +return -ERANGE ; + } + + page = node->content.pvec.pages[page_index]; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!page); +#endif /* CONFIG_SSDFS_DEBUG */ + +ssdfs_lock_page (page); + kaddr =kmap_local_page (page); + ptr = (struct ssdfs_btree_index_key *)((u8 *)kaddr + page_off); + *end_hash =le64_to_cpu (ptr->index.hash); +kunmap_local (kaddr); +ssdfs_unlock_page (page); + +return 0 ; +} + +/* +* ssdfs_init_index_area_hash_range() - extract hash range of index area +* @node: node object +* @hdr: node's header +* @start_hash: starting hash of index area [out] +* @end_hash: ending hash of index area [out] +* +* This method tries to extract start and end hash from +* the raw index area. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +*/ +static +int ssdfs_init_index_area_hash_range (struct ssdfs_btree_node *node, +struct ssdfs_btree_node_header *hdr, +u64 *start_hash,u64 *end_hash) +{ +u16 flags; +u16 index_count; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !node->tree || !hdr); +BUG_ON (!start_hash || !end_hash); +BUG_ON (!rwsem_is_locked (&node->full_lock)); +BUG_ON (!rwsem_is_locked (&node->header_lock)); + +SSDFS_DBG ("node_id %u, height %u\n" , + node->node_id, +atomic_read (&node->height)); +#endif /* CONFIG_SSDFS_DEBUG */ + + *start_hash =U64_MAX ; + *end_hash =U64_MAX ; + + flags =le16_to_cpu (hdr->flags); +if (!(flags &SSDFS_BTREE_NODE_HAS_INDEX_AREA )) +return 0 ; + + index_count =le16_to_cpu (hdr->index_count); +if (index_count ==0 ) +return 0 ; + +return __ssdfs_init_index_area_hash_range (node, index_count, + start_hash, end_hash); +} + +/* +* ssdfs_btree_init_node_index_area() - init the node's index area +* @node: node object +* @hdr: node's header +* @hdr_size: size of the header +* +* This method tries to init the node's index area. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EIO - header is corrupted. +* %-ERANGE - internal error. +*/ +static +int ssdfs_btree_init_node_index_area (struct ssdfs_btree_node *node, +struct ssdfs_btree_node_header *hdr, +size_t hdr_size) +{ +u16 flags; +u32 index_area_size; +u8 index_size; +u16 index_count; +u16 index_capacity; +u32 offset; +u64 start_hash =U64_MAX ; +u64 end_hash =U64_MAX ; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !node->tree || !hdr); +BUG_ON (hdr_size <=sizeof (struct ssdfs_btree_node_header )); +BUG_ON (!rwsem_is_locked (&node->full_lock)); +BUG_ON (!rwsem_is_locked (&node->header_lock)); + +SSDFS_DBG ("node_id %u, height %u\n" , + node->node_id, +atomic_read (&node->height)); +#endif /* CONFIG_SSDFS_DEBUG */ + + flags =le16_to_cpu (hdr->flags); + index_area_size =0 ; + +if (flags &SSDFS_BTREE_NODE_HAS_INDEX_AREA ) { + index_area_size =1 << hdr->log_index_area_size; + +if (index_area_size ==0 || + index_area_size > node->node_size) { +SSDFS_ERR ("invalid index area size %u\n" , + index_area_size); +return -EIO ; + } + +switch (hdr->type) { +case SSDFS_BTREE_INDEX_NODE : +if (index_area_size != node->node_size) { +SSDFS_ERR ("invalid index area's size: " +"index_area_size %u, node_size %u\n" , + index_area_size, + node->node_size); +return -EIO ; + } + + index_area_size -= hdr_size; +break ; + +case SSDFS_BTREE_HYBRID_NODE : +/* expected state */ +break ; + +default : +SSDFS_ERR ("invalid node type %#x\n" , + hdr->type); +return -EIO ; + } + }else { +if (index_area_size !=0 ) { +SSDFS_ERR ("invalid index area size %u\n" , + index_area_size); +return -EIO ; + } + +switch (hdr->type) { +case SSDFS_BTREE_LEAF_NODE : +/* expected state */ +break ; + +default : +SSDFS_ERR ("invalid node type %#x\n" , + hdr->type); +return -EIO ; + } + } + + index_size = hdr->index_size; + index_count =le16_to_cpu (hdr->index_count); + +if (index_area_size < ((u32 )index_count * index_size)) { +SSDFS_ERR ("index area is corrupted: " +"index_area_size %u, index_count %u, " +"index_size %u\n" , + index_area_size, + index_count, + index_size); +return -EIO ; + } + + index_capacity = index_area_size / index_size; +if (index_capacity < index_count) { +SSDFS_ERR ("index_capacity %u < index_count %u\n" , + index_capacity, index_count); +return -ERANGE ; + } + +if (flags &SSDFS_BTREE_NODE_HAS_INDEX_AREA ) { +atomic_set (&node->index_area.state, +SSDFS_BTREE_NODE_INDEX_AREA_EXIST ); + + offset =le16_to_cpu (hdr->index_area_offset); + +if (offset != hdr_size) { +SSDFS_ERR ("invalid index_area_offset %u\n" , + offset); +return -EIO ; + } + +if ((offset + index_area_size) > node->node_size) { +SSDFS_ERR ("offset %u + area_size %u > node_size %u\n" , + offset, index_area_size, node->node_size); +return -ERANGE ; + } + + node->index_area.offset = offset; + node->index_area.area_size = index_area_size; + node->index_area.index_size = index_size; + node->index_area.index_count = index_count; + node->index_area.index_capacity = index_capacity; + + err =ssdfs_init_index_area_hash_range (node, hdr, + &start_hash, + &end_hash); +if (unlikely (err)) { +SSDFS_ERR ("fail to retrieve index area hash range: " +"err %d\n" , + err); +return err; + } + + node->index_area.start_hash = start_hash; + node->index_area.end_hash = end_hash; + +if (start_hash > end_hash) { +SSDFS_WARN ("node_id %u, height %u, " +"start_hash %llx, end_hash %llx\n" , + node->node_id, +atomic_read (&node->height), + start_hash, + end_hash); +#ifdef CONFIG_SSDFS_DEBUG +BUG (); +#else +return -EIO ; +#endif /* CONFIG_SSDFS_DEBUG */ + } + }else { +atomic_set (&node->index_area.state, +SSDFS_BTREE_NODE_AREA_ABSENT ); + node->index_area.offset =U32_MAX ; +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (index_area_size !=0 ); +#endif /* CONFIG_SSDFS_DEBUG */ + node->index_area.area_size = index_area_size; + node->index_area.index_size = index_size; +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (index_count !=0 ); +#endif /* CONFIG_SSDFS_DEBUG */ + node->index_area.index_count = index_count; +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (index_capacity !=0 ); +#endif /* CONFIG_SSDFS_DEBUG */ + node->index_area.index_capacity = index_capacity; +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (start_hash !=U64_MAX ); +BUG_ON (end_hash !=U64_MAX ); +#endif /* CONFIG_SSDFS_DEBUG */ + node->index_area.start_hash = start_hash; + node->index_area.end_hash = end_hash; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("start_hash %llx, end_hash %llx, " +"index_count %u, index_capacity %u\n" , + start_hash, end_hash, + index_count, index_capacity); +#endif /* CONFIG_SSDFS_DEBUG */ + +return 0 ; +} + +/* +* ssdfs_btree_init_node_items_area() - init the node's items area +* @node: node object +* @hdr: node's header +* @hdr_size: size of the header +* +* This method tries to init the node's items area. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EIO - header is corrupted. +* %-ERANGE - internal error. +*/ +static +int ssdfs_btree_init_node_items_area (struct ssdfs_btree_node *node, +struct ssdfs_btree_node_header *hdr, +size_t hdr_size) +{ +u16 flags; +u32 index_area_size; +u32 items_area_size; +u8 min_item_size; +u16 max_item_size; +u32 offset; +u64 start_hash; +u64 end_hash; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !node->tree || !hdr); +BUG_ON (hdr_size <=sizeof (struct ssdfs_btree_node_header )); +BUG_ON (!rwsem_is_locked (&node->full_lock)); +BUG_ON (!rwsem_is_locked (&node->header_lock)); + +SSDFS_DBG ("node_id %u, height %u\n" , + node->node_id, +atomic_read (&node->height)); +#endif /* CONFIG_SSDFS_DEBUG */ + + flags =le16_to_cpu (hdr->flags); + +if (hdr->log_index_area_size >0 ) { + index_area_size =1 << hdr->log_index_area_size; + +switch (hdr->type) { +case SSDFS_BTREE_INDEX_NODE : +if (index_area_size != node->node_size) { +SSDFS_ERR ("invalid index area's size: " +"index_area_size %u, node_size %u\n" , + index_area_size, + node->node_size); +return -EIO ; + } + + index_area_size -= hdr_size; +break ; + +case SSDFS_BTREE_HYBRID_NODE : +/* expected state */ +break ; + +default : +SSDFS_ERR ("invalid node type %#x\n" , + hdr->type); +return -EIO ; + } + }else + index_area_size =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON ((index_area_size + hdr_size) > node->node_size); +#endif /* CONFIG_SSDFS_DEBUG */ + + items_area_size = node->node_size; + items_area_size -= index_area_size; + items_area_size -= hdr_size; + +if (flags &SSDFS_BTREE_NODE_HAS_ITEMS_AREA ) { +if (items_area_size ==0 ) { +SSDFS_ERR ("invalid items area size %u\n" , + items_area_size); +return -EIO ; + } + +switch (hdr->type) { +case SSDFS_BTREE_HYBRID_NODE : +case SSDFS_BTREE_LEAF_NODE : +/* expected state */ +break ; + +default : +SSDFS_ERR ("invalid node type %#x\n" , + hdr->type); +return -EIO ; + } + }else { +if (items_area_size !=0 ) { +SSDFS_ERR ("invalid items area size %u\n" , + items_area_size); +return -EIO ; + } + +switch (hdr->type) { +case SSDFS_BTREE_INDEX_NODE : +/* expected state */ +break ; + +default : +SSDFS_ERR ("invalid node type %#x\n" , + hdr->type); +return -EIO ; + } + } + + offset = hdr_size + index_area_size; + +switch (hdr->type) { +case SSDFS_BTREE_HYBRID_NODE : +case SSDFS_BTREE_LEAF_NODE : +if (offset !=le32_to_cpu (hdr->item_area_offset)) { +SSDFS_ERR ("invalid item_area_offset %u\n" , +le32_to_cpu (hdr->item_area_offset)); +return -EIO ; + } +break ; + } + +if ((offset + items_area_size) > node->node_size) { +SSDFS_ERR ("offset %u + items_area_size %u > node_size %u\n" , + offset, items_area_size, node->node_size); +return -ERANGE ; + } + + min_item_size = hdr->min_item_size; + max_item_size =le16_to_cpu (hdr->max_item_size); + +if (max_item_size < min_item_size) { +SSDFS_ERR ("invalid item size: " +"min size %u, max size %u\n" , + min_item_size, max_item_size); +return -EIO ; + } + + start_hash =le64_to_cpu (hdr->start_hash); + end_hash =le64_to_cpu (hdr->end_hash); + +if (start_hash > end_hash) { +SSDFS_WARN ("start_hash %llx > end_hash %llx\n" , + start_hash, end_hash); +#ifdef CONFIG_SSDFS_DEBUG +BUG (); +#else +return -EIO ; +#endif /* CONFIG_SSDFS_DEBUG */ + } + +if (flags &SSDFS_BTREE_NODE_HAS_ITEMS_AREA ) { +atomic_set (&node->items_area.state, +SSDFS_BTREE_NODE_ITEMS_AREA_EXIST ); + node->items_area.offset = offset; + node->items_area.area_size = items_area_size; + node->items_area.min_item_size = node->tree->item_size; + node->items_area.min_item_size = min_item_size; + node->items_area.max_item_size = max_item_size; + node->items_area.items_count =U16_MAX ; + node->items_area.items_capacity =U16_MAX ; + node->items_area.start_hash = start_hash; + node->items_area.end_hash = end_hash; + }else { +atomic_set (&node->items_area.state, +SSDFS_BTREE_NODE_AREA_ABSENT ); + node->items_area.offset =U32_MAX ; +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (items_area_size !=0 ); +#endif /* CONFIG_SSDFS_DEBUG */ + node->items_area.area_size = items_area_size; + node->items_area.min_item_size = node->tree->item_size; + node->items_area.min_item_size = min_item_size; + node->items_area.max_item_size = max_item_size; + node->items_area.items_count =0 ; + node->items_area.items_capacity =0 ; + node->items_area.start_hash =U64_MAX ; + node->items_area.end_hash =U64_MAX ; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("start_hash %llx, end_hash %llx, " +"items_count %u, items_capacity %u\n" , + start_hash, end_hash, + node->items_area.items_count, + node->items_area.items_capacity); +#endif /* CONFIG_SSDFS_DEBUG */ + +return 0 ; +} + +/* +* ssdfs_btree_init_node() - init node object +* @node: node object +* @hdr: node's header +* @hdr_size: size of the header +* +* This method tries to init the node object. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EIO - header is corrupted. +* %-ERANGE - internal error. +*/ +int ssdfs_btree_init_node (struct ssdfs_btree_node *node, +struct ssdfs_btree_node_header *hdr, +size_t hdr_size) +{ +u8 tree_height; +u64 create_cno; +u16 flags; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !node->tree || !hdr); +BUG_ON (hdr_size <=sizeof (struct ssdfs_btree_node_header )); +BUG_ON (!rwsem_is_locked (&node->full_lock)); +BUG_ON (!rwsem_is_locked (&node->header_lock)); +#endif /* CONFIG_SSDFS_DEBUG */ + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("node %p, hdr %p, node_id %u\n" , + node, hdr, node->node_id); +#else +SSDFS_DBG ("node %p, hdr %p, node_id %u\n" , + node, hdr, node->node_id); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + + tree_height =atomic_read (&node->tree->height); +if (hdr->height >= tree_height) { +SSDFS_ERR ("invalid height: " +"tree_height %u, node_height %u\n" , + tree_height, hdr->height); +return -EIO ; + } +atomic_set (&node->height, hdr->height); + +if (node->node_size != (1 << hdr->log_node_size)) { +SSDFS_ERR ("invalid node size: " +"node_size %u != node_size %u\n" , + node->node_size, + (1 << hdr->log_node_size)); +return -EIO ; + } + +if (le32_to_cpu (hdr->node_id) != node->node_id) { +SSDFS_WARN ("node->node_id %u != hdr->node_id %u\n" , + node->node_id, +le32_to_cpu (hdr->node_id)); +return -EIO ; + } + + create_cno =le64_to_cpu (hdr->create_cno); +if (create_cno < node->tree->create_cno) { +SSDFS_ERR ("create_cno %llu < node->tree->create_cno %llu\n" , + create_cno, + node->tree->create_cno); +return -EIO ; + } + node->create_cno = create_cno; + + flags =le16_to_cpu (hdr->flags); +if (flags & ~SSDFS_BTREE_NODE_FLAGS_MASK ) { +SSDFS_ERR ("invalid flags %#x\n" , + flags); +return -EIO ; + } +atomic_set (&node->flags, flags); + +if (hdr->type <=SSDFS_BTREE_ROOT_NODE || + hdr->type >=SSDFS_BTREE_NODE_TYPE_MAX ) { +SSDFS_ERR ("invalid type %#x\n" , + hdr->type); +return -EIO ; + } +atomic_set (&node->type, hdr->type); + +switch (hdr->type) { +case SSDFS_BTREE_INDEX_NODE : +if (flags &SSDFS_BTREE_NODE_HAS_INDEX_AREA && + !(flags &SSDFS_BTREE_NODE_HAS_ITEMS_AREA )) { +/* +* expected set of flags +*/ + }else { +SSDFS_ERR ("invalid set of flags %#x for index node\n" , + flags); +return -EIO ; + } +break ; + +case SSDFS_BTREE_HYBRID_NODE : +if (flags &SSDFS_BTREE_NODE_HAS_INDEX_AREA && + flags &SSDFS_BTREE_NODE_HAS_ITEMS_AREA ) { +/* +* expected set of flags +*/ + }else { +SSDFS_ERR ("invalid set of flags %#x for hybrid node\n" , + flags); +return -EIO ; + } +break ; + +case SSDFS_BTREE_LEAF_NODE : +if (!(flags &SSDFS_BTREE_NODE_HAS_INDEX_AREA ) && + flags &SSDFS_BTREE_NODE_HAS_ITEMS_AREA ) { +/* +* expected set of flags +*/ + }else { +SSDFS_ERR ("invalid set of flags %#x for leaf node\n" , + flags); +return -EIO ; + } +break ; + +default : +SSDFS_ERR ("invalid node type %#x\n" , hdr->type); +return -ERANGE ; + }; + + err =ssdfs_btree_init_node_index_area (node, hdr, hdr_size); +if (unlikely (err)) { +SSDFS_ERR ("fail to init index area: " +"node_id %u, err %d\n" , + node->node_id, err); +return err; + } + + err =ssdfs_btree_init_node_items_area (node, hdr, hdr_size); +if (unlikely (err)) { +SSDFS_ERR ("fail to init items area: " +"node_id %u, err %d\n" , + node->node_id, err); +return err; + } + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("finished\n" ); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + +return 0 ; +}
fs/ssdfs/btree_node.h new file +768 -0
768 lines not on this page · raw message
Signature · 1 line
2.34.1
RFC PATCH 51/76 ssdfs: flush b-tree node object
In reply to Viacheslav DubeykoDirty b-tree implies the presence of one or several dirty b-tree node. B-tree flush logic detects the dirty b-tree nodes and request the flush operation for every dirty b-tree node. B-tree node can include several memory pages (8K, for example). It means that one b-tree node can be located in one or several logical blocks. Finally, flush operation means that b-tree node's flush logic has to issue update request(s) for all logical blocks that contain the b-tree node content. Every b-tree node is described by index record (or key) that
includes: (1) node ID, (2) node type, (3) node height,
(4) starting hash value, (5) raw extent. The raw extent describes the segment ID, logical block ID, and length. As a result, flush logic needs to add update request into an update queue of particular PEB for segment ID. Also, flush logic has to request the log commit operation because b-tree node has to be stored peristently right now. Flush thread(s) of particular PEB(s) executes the update requests. Finally, b-tree flush logic requires to wait the finish of update operations for all dirty b-tree node(s).
--- fs/ssdfs/btree_node.c | 3048 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 3048 insertions(+)
fs/ssdfs/btree_node.c +3048 -0
@@ -2174,3 +2174,3051 @@ int ssdfs_btree_init_node(struct ssdfs_btree_node *node,return 0 ; } + +/* +* ssdfs_btree_pre_flush_root_node() - pre-flush the dirty root node +* @node: node object +* +* This method tries to pre-flush the dirty root node. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +*/ +int ssdfs_btree_pre_flush_root_node (struct ssdfs_btree_node *node) +{ +struct ssdfs_btree_inline_root_node *root_node; +size_t root_node_size =sizeof (struct ssdfs_btree_inline_root_node ); +int height, tree_height; +int type; +u32 area_size, calculated_area_size; +u32 area_offset; +u16 index_count; +u16 index_size; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !node->tree); +BUG_ON (!rwsem_is_locked (&node->full_lock)); +BUG_ON (!rwsem_is_locked (&node->header_lock)); + +SSDFS_DBG ("node_id %u, state %#x\n" , + node->node_id,atomic_read (&node->state)); +#endif /* CONFIG_SSDFS_DEBUG */ + + root_node = &node->raw.root_node; + + height =atomic_read (&node->height); +if (height >=U8_MAX || height <=0 ) { +SSDFS_ERR ("invalid height %d\n" , height); +return -ERANGE ; + } + + tree_height =atomic_read (&node->tree->height); +if (tree_height >=U8_MAX || tree_height <=0 ) { +SSDFS_ERR ("invalid tree's height %d\n" , + tree_height); +return -ERANGE ; + } + +if ((tree_height -1 ) != height) { +SSDFS_ERR ("tree_height %d, root node's height %d\n" , + tree_height, height); +return -ERANGE ; + } + + root_node->header.height = (u8 )height; + +if (node->node_size != root_node_size) { +SSDFS_ERR ("corrupted root node size %u\n" , + node->node_size); +return -ERANGE ; + } + + calculated_area_size =sizeof (struct ssdfs_btree_index ); + calculated_area_size *=SSDFS_BTREE_ROOT_NODE_INDEX_COUNT ; + + area_size = node->index_area.area_size; +if (area_size != calculated_area_size) { +SSDFS_ERR ("corrupted index area size %u\n" , + area_size); +return -ERANGE ; + } + + type =atomic_read (&node->type); +if (type !=SSDFS_BTREE_ROOT_NODE ) { +SSDFS_ERR ("invalid node type %#x\n" , + type); +return -ERANGE ; + } + + root_node->header.type = (u8 )type; + + area_offset = node->index_area.offset; +if (area_offset <sizeof (struct ssdfs_btree_root_node_header ) || + area_offset >= node->node_size) { +SSDFS_ERR ("corrupted index area offset %u\n" , + area_offset); +return -ERANGE ; + } + +if (node->index_area.index_count > node->index_area.index_capacity) { +SSDFS_ERR ("corrupted index area descriptor: " +"index_count %u, index_capacity %u\n" , + node->index_area.index_count, + node->index_area.index_capacity); +return -ERANGE ; + } + + index_count = node->index_area.index_count; + +if (index_count >SSDFS_BTREE_ROOT_NODE_INDEX_COUNT ) { +SSDFS_ERR ("invalid index count %u\n" , + index_count); +return -ERANGE ; + } + + root_node->header.items_count = (u8 )index_count; + + index_size = node->index_area.index_size; + +if (index_size !=sizeof (struct ssdfs_btree_index )) { +SSDFS_ERR ("invalid index size %u\n" , index_size); +return -ERANGE ; + } + +if (((u32 )index_count * index_size) > area_size) { +SSDFS_ERR ("corrupted index area: " +"index_count %u, index_size %u, area_size %u\n" , + index_count, + index_size, + area_size); +return -ERANGE ; + } + + root_node->header.upper_node_id = +cpu_to_le32 (node->tree->upper_node_id); + +return 0 ; +} + +/* +* ssdfs_btree_node_pre_flush_header() - pre-flush node's header +* @node: node object +* @hdr: node's header +* +* This method tries to pre-flush the node's header. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +*/ +int ssdfs_btree_node_pre_flush_header (struct ssdfs_btree_node *node, +struct ssdfs_btree_node_header *hdr) +{ +int height; +int type; +int flags; +u32 area_size; +u32 area_offset; +u8 index_size; +u16 index_count; +u16 index_capacity; +u16 items_capacity; +u16 item_size; +u8 min_item_size; +u16 max_item_size; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !hdr); +BUG_ON (!rwsem_is_locked (&node->header_lock)); + +SSDFS_DBG ("node_id %u, state %#x\n" , + node->node_id,atomic_read (&node->state)); +#endif /* CONFIG_SSDFS_DEBUG */ + + height =atomic_read (&node->height); +if (height >=U8_MAX || height <0 ) { +SSDFS_ERR ("invalid height %d\n" , height); +return -ERANGE ; + } + + hdr->height = (u8 )height; + +if ((1 <<ilog2 (node->node_size)) != node->node_size) { +SSDFS_ERR ("corrupted node size %u\n" , + node->node_size); +return -ERANGE ; + } + + hdr->log_node_size = (u8 )ilog2 (node->node_size); + + type =atomic_read (&node->type); +if (type <=SSDFS_BTREE_NODE_UNKNOWN_TYPE || + type >=SSDFS_BTREE_NODE_TYPE_MAX ) { +SSDFS_ERR ("invalid node type %#x\n" , + type); +return -ERANGE ; + } + + hdr->type = (u8 )type; + + flags =atomic_read (&node->flags); +if (flags & ~SSDFS_BTREE_NODE_FLAGS_MASK ) { +SSDFS_ERR ("corrupted set of flags %#x\n" , + flags); +return -ERANGE ; + } + +/* +* Flag SSDFS_BTREE_NODE_PRE_ALLOCATED needs to be excluded. +* The pre-allocated node will be created during the flush +* operation. This flag needs only on kernel side. +*/ + flags &= ~SSDFS_BTREE_NODE_PRE_ALLOCATED ; + + hdr->flags =cpu_to_le16 ((u16 )flags); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node_id %u, type %#x, flags %#x\n" , + node->node_id, type, flags); +#endif /* CONFIG_SSDFS_DEBUG */ + +switch (atomic_read (&node->index_area.state)) { +case SSDFS_BTREE_NODE_INDEX_AREA_EXIST : +switch (atomic_read (&node->type)) { +case SSDFS_BTREE_INDEX_NODE : +/* +* Initialization code expect the node size. +* As a result, the index area's size is calculated +* by means of subtraction the header size +* from the node size. +*/ + area_size = node->node_size; +if ((1 <<ilog2 (area_size)) != area_size) { +SSDFS_ERR ("corrupted index area size %u\n" , + area_size); +return -ERANGE ; + } + + hdr->log_index_area_size = (u8 )ilog2 (area_size); + +/* +* Real area size is used for checking +* the rest of fields. +*/ + area_size = node->index_area.area_size; +break ; + +default : + area_size = node->index_area.area_size; +if ((1 <<ilog2 (area_size)) != area_size) { +SSDFS_ERR ("corrupted index area size %u\n" , + area_size); +return -ERANGE ; + } + + hdr->log_index_area_size = (u8 )ilog2 (area_size); +break ; + } + + area_offset = node->index_area.offset; +if (area_offset <=sizeof (struct ssdfs_btree_node_header ) || + area_offset >= node->node_size || + area_offset >= node->items_area.offset) { +SSDFS_ERR ("corrupted index area offset %u\n" , + area_offset); +return -ERANGE ; + } + + hdr->index_area_offset =cpu_to_le16 ((u16 )area_offset); + + index_count = node->index_area.index_count; + index_capacity = node->index_area.index_capacity; + +if (index_count > index_capacity) { +SSDFS_ERR ("corrupted index area descriptor: " +"index_count %u, index_capacity %u\n" , + index_count, index_capacity); +return -ERANGE ; + } + + hdr->index_count =cpu_to_le16 (index_count); + + index_size = node->index_area.index_size; + +if (((u32 )index_count * index_size) > area_size) { +SSDFS_ERR ("corrupted index area: " +"index_count %u, index_size %u, " +"area_size %u\n" , + index_count, index_size, area_size); +return -ERANGE ; + } + + hdr->index_size = index_size; +break ; + +default : + hdr->log_index_area_size = (u8 )ilog2 (0 ); + hdr->index_area_offset =cpu_to_le16 (U16_MAX ); + hdr->index_count =cpu_to_le16 (0 ); + hdr->index_size =U8_MAX ; +break ; + } + +switch (atomic_read (&node->items_area.state)) { +case SSDFS_BTREE_NODE_ITEMS_AREA_EXIST : + item_size = node->items_area.item_size; + min_item_size = node->items_area.min_item_size; + max_item_size = node->items_area.max_item_size; + items_capacity = node->items_area.items_capacity; + area_size = node->items_area.area_size; +break ; + +default : + item_size =U16_MAX ; + min_item_size =0 ; + max_item_size =0 ; + items_capacity =0 ; + area_size =0 ; +break ; + } + +switch (type) { +case SSDFS_BTREE_LEAF_NODE : +if (item_size ==0 ) { +SSDFS_ERR ("corrupted items area: " +"item_size %u\n" , + item_size); +return -ERANGE ; + }else if (min_item_size > item_size) { +SSDFS_ERR ("corrupted items area: " +"min_item_size %u, " +"item_size %u\n" , + min_item_size, item_size); +return -ERANGE ; + }else if (item_size > max_item_size) { +SSDFS_ERR ("corrupted items area: " +"item_size %u, " +"max_item_size %u\n" , + item_size, max_item_size); +return -ERANGE ; + }else if (item_size > area_size) { +SSDFS_ERR ("corrupted items area: " +"item_size %u, " +"area_size %u\n" , + item_size, area_size); +return -ERANGE ; + }else + hdr->min_item_size = min_item_size; + +if (max_item_size ==0 ) { +SSDFS_ERR ("corrupted items area: " +"max_item_size %u\n" , + max_item_size); +return -ERANGE ; + }else if (max_item_size > area_size) { +SSDFS_ERR ("corrupted items area: " +"max_item_size %u, " +"area_size %u\n" , + max_item_size, area_size); +return -ERANGE ; + }else + hdr->max_item_size =cpu_to_le16 (max_item_size); + +if (items_capacity ==0 ) { +SSDFS_ERR ("corrupted items area's state\n" ); +return -ERANGE ; + }else if (((u32 )items_capacity * item_size) > area_size) { +SSDFS_ERR ("corrupted items area's state: " +"items_capacity %u, item_size %u, " +"area_size %u\n" , + items_capacity, + item_size, + area_size); +return -ERANGE ; + }else + hdr->items_capacity =cpu_to_le16 (items_capacity); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node_id %u, node_type %#x, " +"start_hash %llx, end_hash %llx\n" , + node->node_id, +atomic_read (&node->type), + node->items_area.start_hash, + node->items_area.end_hash); +#endif /* CONFIG_SSDFS_DEBUG */ + + hdr->start_hash =cpu_to_le64 (node->items_area.start_hash); + hdr->end_hash =cpu_to_le64 (node->items_area.end_hash); + + area_offset = node->items_area.offset; + area_size = node->items_area.area_size; +if ((area_offset + area_size) > node->node_size) { +SSDFS_ERR ("corrupted items area offset %u\n" , + area_offset); +return -ERANGE ; + } + + hdr->item_area_offset =cpu_to_le32 (area_offset); +break ; + +case SSDFS_BTREE_HYBRID_NODE : +if (item_size ==0 ) { +SSDFS_ERR ("corrupted items area: " +"item_size %u\n" , + item_size); +return -ERANGE ; + }else if (min_item_size > item_size) { +SSDFS_ERR ("corrupted items area: " +"min_item_size %u, " +"item_size %u\n" , + min_item_size, item_size); +return -ERANGE ; + }else if (item_size > max_item_size) { +SSDFS_ERR ("corrupted items area: " +"item_size %u, " +"max_item_size %u\n" , + item_size, max_item_size); +return -ERANGE ; + }else if (item_size > area_size) { +SSDFS_ERR ("corrupted items area: " +"item_size %u, " +"area_size %u\n" , + item_size, area_size); +return -ERANGE ; + }else + hdr->min_item_size = min_item_size; + +if (max_item_size ==0 ) { +SSDFS_ERR ("corrupted items area: " +"max_item_size %u\n" , + max_item_size); +return -ERANGE ; + }else if (max_item_size > area_size) { +SSDFS_ERR ("corrupted items area: " +"max_item_size %u, " +"area_size %u\n" , + max_item_size, area_size); +return -ERANGE ; + }else + hdr->max_item_size =cpu_to_le16 (max_item_size); + +if (items_capacity ==0 ) { +SSDFS_ERR ("corrupted items area's state\n" ); +return -ERANGE ; + }else if (((u32 )items_capacity * min_item_size) > area_size) { +SSDFS_ERR ("corrupted items area's state: " +"items_capacity %u, min_item_szie %u, " +"area_size %u\n" , + items_capacity, + min_item_size, + area_size); +return -ERANGE ; + }else + hdr->items_capacity =cpu_to_le16 (items_capacity); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node_id %u, node_type %#x, " +"start_hash %llx, end_hash %llx\n" , + node->node_id, +atomic_read (&node->type), + node->items_area.start_hash, + node->items_area.end_hash); +#endif /* CONFIG_SSDFS_DEBUG */ + + hdr->start_hash =cpu_to_le64 (node->items_area.start_hash); + hdr->end_hash =cpu_to_le64 (node->items_area.end_hash); + + area_offset = node->items_area.offset; + area_size = node->items_area.area_size; +if ((area_offset + area_size) > node->node_size) { +SSDFS_ERR ("corrupted items area offset %u\n" , + area_offset); +return -ERANGE ; + } + + hdr->item_area_offset =cpu_to_le32 (area_offset); + + area_offset = node->index_area.offset; + area_size = node->index_area.area_size; +if ((area_offset + area_size) > node->node_size) { +SSDFS_ERR ("corrupted index area offset %u\n" , + area_offset); +return -ERANGE ; + }else if ((area_offset + area_size) > node->items_area.offset) { +SSDFS_ERR ("corrupted index area offset %u\n" , + area_offset); +return -ERANGE ; + } + + hdr->index_area_offset =cpu_to_le32 (area_offset); +break ; + +case SSDFS_BTREE_INDEX_NODE : +if (min_item_size !=0 ) { +SSDFS_ERR ("corrupted items area: " +"min_item_size %u\n" , + min_item_size); +return -ERANGE ; + }else + hdr->min_item_size = min_item_size; + +if (max_item_size !=0 ) { +SSDFS_ERR ("corrupted items area: " +"max_item_size %u\n" , + max_item_size); +return -ERANGE ; + }else + hdr->max_item_size =cpu_to_le16 (max_item_size); + +if (items_capacity !=0 ) { +SSDFS_ERR ("corrupted items area's state\n" ); +return -ERANGE ; + }else + hdr->items_capacity =cpu_to_le16 (items_capacity); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node_id %u, node_type %#x, " +"start_hash %llx, end_hash %llx\n" , + node->node_id, +atomic_read (&node->type), + node->index_area.start_hash, + node->index_area.end_hash); +#endif /* CONFIG_SSDFS_DEBUG */ + + hdr->start_hash =cpu_to_le64 (node->index_area.start_hash); + hdr->end_hash =cpu_to_le64 (node->index_area.end_hash); + + area_offset = node->index_area.offset; + area_size = node->index_area.area_size; +if ((area_offset + area_size) > node->node_size) { +SSDFS_ERR ("corrupted index area offset %u\n" , + area_offset); +return -ERANGE ; + } + + hdr->index_area_offset =cpu_to_le32 (area_offset); +break ; + +default : +SSDFS_ERR ("invalid node type %#x\n" , type); +return -ERANGE ; + } + + hdr->create_cno =cpu_to_le64 (node->create_cno); + hdr->node_id =cpu_to_le32 (node->node_id); + +return 0 ; +} + +/* +* ssdfs_btree_node_pre_flush() - pre-flush the dirty btree node +* @node: node object +* +* This method tries to pre-flush the dirty btree node. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +*/ +int ssdfs_btree_node_pre_flush (struct ssdfs_btree_node *node) +{ +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !node->tree); + +SSDFS_DBG ("node_id %u, height %u, type %#x, state %#x\n" , + node->node_id,atomic_read (&node->height), +atomic_read (&node->type), +atomic_read (&node->state)); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!is_ssdfs_btree_node_dirty (node)) +return 0 ; + +switch (atomic_read (&node->type)) { +case SSDFS_BTREE_ROOT_NODE : +if (!node->tree->btree_ops || + !node->tree->btree_ops->pre_flush_root_node) { +SSDFS_WARN ("unable to pre-flush the root node\n" ); +return -EOPNOTSUPP ; + } + + err = node->tree->btree_ops->pre_flush_root_node (node); +if (unlikely (err)) { +SSDFS_ERR ("fail to pre-flush root node: " +"node_id %u, height %u, err %d\n" , + node->node_id, +atomic_read (&node->height), + err); + } +break ; + +case SSDFS_BTREE_INDEX_NODE : +case SSDFS_BTREE_HYBRID_NODE : +case SSDFS_BTREE_LEAF_NODE : +if (!node->tree->btree_ops || + !node->tree->btree_ops->pre_flush_node) { +SSDFS_WARN ("unable to pre-flush common node\n" ); +return -EOPNOTSUPP ; + } + + err = node->tree->btree_ops->pre_flush_node (node); +if (unlikely (err)) { +SSDFS_ERR ("fail to pre-flush common node: " +"node_id %u, height %u, err %d\n" , + node->node_id, +atomic_read (&node->height), + err); + } +break ; + +default : + err = -ERANGE ; +SSDFS_WARN ("invalid node type %#x\n" , +atomic_read (&node->type)); +break ; + } + +return err; +} + +/* +* ssdfs_btree_flush_root_node() - flush root node +* @node: node object +* @root_node: pointer on the on-disk root node object +*/ +void ssdfs_btree_flush_root_node (struct ssdfs_btree_node *node, +struct ssdfs_btree_inline_root_node *root_node) +{ +size_t node_ids_len =sizeof (__le32) * +SSDFS_BTREE_ROOT_NODE_INDEX_COUNT ; +size_t indexes_len =sizeof (struct ssdfs_btree_index ) * +SSDFS_BTREE_ROOT_NODE_INDEX_COUNT ; +u16 items_count; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !node->tree || !root_node); +BUG_ON (!rwsem_is_locked (&node->tree->lock)); + +SSDFS_DBG ("node_id %u, height %u, type %#x\n" , + node->node_id,atomic_read (&node->height), +atomic_read (&node->type)); +#endif /* CONFIG_SSDFS_DEBUG */ + +down_write (&node->header_lock); + + items_count = node->index_area.index_count; + root_node->header.height = (u8 )atomic_read (&node->tree->height); + root_node->header.items_count =cpu_to_le16 (items_count); + root_node->header.flags = (u8 )atomic_read (&node->flags); + root_node->header.type = (u8 )atomic_read (&node->type); +ssdfs_memcpy (root_node->header.node_ids,0 , node_ids_len, + node->raw.root_node.header.node_ids,0 , node_ids_len, + node_ids_len); +ssdfs_memcpy (root_node->indexes,0 , indexes_len, + node->raw.root_node.indexes,0 , indexes_len, + indexes_len); +clear_ssdfs_btree_node_dirty (node); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("left index (node_id %u, hash %llx), " +"right index (node_id %u, hash %llx)\n" , +le32_to_cpu (root_node->header.node_ids[0 ]), +le64_to_cpu (root_node->indexes[0 ].hash), +le32_to_cpu (root_node->header.node_ids[1 ]), +le64_to_cpu (root_node->indexes[1 ].hash)); +#endif /* CONFIG_SSDFS_DEBUG */ + +up_write (&node->header_lock); + +spin_lock (&node->tree->nodes_lock); + root_node->header.upper_node_id = +cpu_to_le32 (node->tree->upper_node_id); +spin_unlock (&node->tree->nodes_lock); + +ssdfs_request_init (&node->flush_req); +atomic_set (&node->flush_req.result.state,SSDFS_REQ_FINISHED ); +} + +/* +* ssdfs_btree_node_copy_header_nolock() - copy btree node's header +* @node: node object +* @page: memory page to store the metadata [out] +* @write_offset: current write offset [out] +* +* This method tries to save the btree node's header. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +*/ +int ssdfs_btree_node_copy_header_nolock (struct ssdfs_btree_node *node, +struct page *page, +u32 *write_offset) +{ +size_t hdr_size; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !page); +BUG_ON (!rwsem_is_locked (&node->header_lock)); +BUG_ON (!rwsem_is_locked (&node->full_lock)); + +SSDFS_DBG ("node_id %u, height %u, type %#x, write_offset %u\n" , + node->node_id,atomic_read (&node->height), +atomic_read (&node->type), *write_offset); +#endif /* CONFIG_SSDFS_DEBUG */ + + hdr_size =sizeof (node->raw); + +if (*write_offset >=PAGE_SIZE ) { +SSDFS_ERR ("invalid write_offset %u\n" , + *write_offset); +return -EINVAL ; + } + +/* all btrees have the same node's header size */ + err =ssdfs_memcpy_to_page (page, *write_offset,PAGE_SIZE , + &node->raw,0 , hdr_size, + hdr_size); +if (unlikely (err)) { +SSDFS_ERR ("fail to copy node's header: " +"write_offset %u, size %zu, err %d\n" , + *write_offset, hdr_size, err); +return err; + } + + *write_offset += hdr_size; + +if (*write_offset >=PAGE_SIZE ) { +SSDFS_ERR ("invalid write_offset %u\n" , + *write_offset); +return -ERANGE ; + } + +return 0 ; +} + +/* +* ssdfs_btree_node_prepare_flush_request() - prepare node's content for flush +* @node: node object +* +* This method tries to prepare the node's content +* for flush operation. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +*/ +static +int ssdfs_btree_node_prepare_flush_request (struct ssdfs_btree_node *node) +{ +struct ssdfs_fs_info *fsi; +struct ssdfs_segment_info *si; +struct ssdfs_state_bitmap *bmap; +struct page *page; +u64 logical_offset; +u32 data_bytes; +u64 seg_id; +u32 logical_blk; +u32 len; +u32 pvec_size; +int node_flags; +u32 write_offset =0 ; +int i; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !node->tree || !node->tree->fsi); + +switch (atomic_read (&node->type)) { +case SSDFS_BTREE_INDEX_NODE : +case SSDFS_BTREE_HYBRID_NODE : +case SSDFS_BTREE_LEAF_NODE : +/* expected state */ +break ; + +default : +BUG (); + }; + +SSDFS_DBG ("node_id %u, height %u, type %#x\n" , + node->node_id,atomic_read (&node->height), +atomic_read (&node->type)); +#endif /* CONFIG_SSDFS_DEBUG */ + + fsi = node->tree->fsi; + pvec_size = node->node_size >>PAGE_SHIFT ; + +if (pvec_size ==0 || pvec_size >PAGEVEC_SIZE ) { +SSDFS_WARN ("invalid memory pages count: " +"node_size %u, pvec_size %u\n" , + node->node_size, pvec_size); +return -ERANGE ; + } + +if (pagevec_count (&node->content.pvec) != pvec_size) { +SSDFS_ERR ("invalid pvec_size: " +"pvec_size1 %u != pvec_size2 %u\n" , +pagevec_count (&node->content.pvec), + pvec_size); +return -ERANGE ; + } + +ssdfs_request_init (&node->flush_req); +ssdfs_get_request (&node->flush_req); + + logical_offset = (u64 )node->node_id * node->node_size; + data_bytes = node->node_size; +ssdfs_request_prepare_logical_extent (node->tree->owner_ino, + (u64 )logical_offset, + (u32 )data_bytes, +0 ,0 , &node->flush_req); + +for (i =0 ; i < pvec_size; i++) { + err =ssdfs_request_add_allocated_page_locked (&node->flush_req); +if (unlikely (err)) { +SSDFS_ERR ("fail to add page into request: " +"err %d\n" , + err); + goto fail_prepare_flush_request; + } + + page = node->flush_req.result.pvec.pages[i]; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!page); +#endif /* CONFIG_SSDFS_DEBUG */ + +set_page_writeback (page); + } + +down_write (&node->full_lock); +down_write (&node->header_lock); + +ssdfs_lock_page (node->content.pvec.pages[0 ]); +ssdfs_btree_node_copy_header_nolock (node, + node->content.pvec.pages[0 ], + &write_offset); +ssdfs_unlock_page (node->content.pvec.pages[0 ]); + +spin_lock (&node->descriptor_lock); + si = node->seg; + seg_id =le64_to_cpu (node->extent.seg_id); + logical_blk =le32_to_cpu (node->extent.logical_blk); + len =le32_to_cpu (node->extent.len); +spin_unlock (&node->descriptor_lock); + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!si); +BUG_ON (seg_id != si->seg_id); +#endif /* CONFIG_SSDFS_DEBUG */ + +ssdfs_request_define_segment (seg_id, &node->flush_req); + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (logical_blk >=U16_MAX ); +BUG_ON (len >=U16_MAX ); +#endif /* CONFIG_SSDFS_DEBUG */ +ssdfs_request_define_volume_extent ((u16 )logical_blk, (u16 )len, + &node->flush_req); + +for (i =0 ; i < pvec_size; i++) { +struct page *page; + +ssdfs_lock_page (node->content.pvec.pages[i]); + + page = node->flush_req.result.pvec.pages[i]; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("REQUEST: page %p, count %d, " +"flags %#lx, page_index %lu\n" , + page,page_ref_count (page), + page->flags,page_index (page)); +#endif /* CONFIG_SSDFS_DEBUG */ + + page = node->content.pvec.pages[i]; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("NODE CONTENT: page %p, count %d, " +"flags %#lx, page_index %lu\n" , + page,page_ref_count (page), + page->flags,page_index (page)); +#endif /* CONFIG_SSDFS_DEBUG */ + +ssdfs_memmove_page (node->flush_req.result.pvec.pages[i], +0 ,PAGE_SIZE , + node->content.pvec.pages[i], +0 ,PAGE_SIZE , +PAGE_SIZE ); + +ssdfs_unlock_page (node->content.pvec.pages[i]); + } + + node_flags =atomic_read (&node->flags); + +if (node_flags &SSDFS_BTREE_NODE_PRE_ALLOCATED ) { +/* update pre-allocated extent */ + err =ssdfs_segment_update_pre_alloc_extent_async (si, +SSDFS_REQ_ASYNC_NO_FREE , + &node->flush_req); + }else { +/* update extent */ + err =ssdfs_segment_update_extent_async (si, +SSDFS_REQ_ASYNC_NO_FREE , + &node->flush_req); + } + +if (!err) { +down_read (&node->bmap_array.lock); + bmap = &node->bmap_array.bmap[SSDFS_BTREE_NODE_DIRTY_BMAP ]; +spin_lock (&bmap->lock); +bitmap_clear (bmap->ptr,0 , node->bmap_array.bits_count); +spin_unlock (&bmap->lock); +up_read (&node->bmap_array.lock); +clear_ssdfs_btree_node_dirty (node); + } + +up_write (&node->header_lock); +up_write (&node->full_lock); + +if (unlikely (err)) { +SSDFS_ERR ("update request failed: " +"ino %llu, logical_offset %llu, size %u, err %d\n" , + node->flush_req.extent.ino, + node->flush_req.extent.logical_offset, + node->flush_req.extent.data_bytes, + err); +return err; + } + +return 0 ; + +fail_prepare_flush_request: +for (i =0 ; i <pagevec_count (&node->flush_req.result.pvec); i++) { + page = node->flush_req.result.pvec.pages[i]; + +if (!page) +continue ; + +SetPageError (page); +end_page_writeback (page); + } + +ssdfs_request_unlock_and_remove_pages (&node->flush_req); +ssdfs_put_request (&node->flush_req); + +return err; +} + +/* +* ssdfs_btree_common_node_flush() - common method of node flushing +* @node: node object +* +* This method tries to flush the node in general way. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +*/ +int ssdfs_btree_common_node_flush (struct ssdfs_btree_node *node) +{ +struct ssdfs_fs_info *fsi; +struct page *page; +size_t index_key_size =sizeof (struct ssdfs_btree_index_key ); +u32 pvec_size; +int node_flags; +int i; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !node->tree || !node->tree->fsi); + +switch (atomic_read (&node->type)) { +case SSDFS_BTREE_INDEX_NODE : +case SSDFS_BTREE_HYBRID_NODE : +case SSDFS_BTREE_LEAF_NODE : +/* expected state */ +break ; + +default : +BUG (); + }; + +SSDFS_DBG ("node_id %u, height %u, type %#x\n" , + node->node_id,atomic_read (&node->height), +atomic_read (&node->type)); +#endif /* CONFIG_SSDFS_DEBUG */ + + fsi = node->tree->fsi; + + pvec_size = node->node_size >>PAGE_SHIFT ; + +if (pvec_size ==0 || pvec_size >PAGEVEC_SIZE ) { +SSDFS_WARN ("invalid memory pages count: " +"node_size %u, pvec_size %u\n" , + node->node_size, pvec_size); +return -ERANGE ; + } + +if (pagevec_count (&node->content.pvec) != pvec_size) { +SSDFS_ERR ("invalid pvec_size: " +"pvec_size1 %u != pvec_size2 %u\n" , +pagevec_count (&node->content.pvec), + pvec_size); +return -ERANGE ; + } + + node_flags =atomic_read (&node->flags); + +if (can_diff_on_write_metadata_be_used (node)) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node_id %u, height %u, type %#x\n" , + node->node_id,atomic_read (&node->height), +atomic_read (&node->type)); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_btree_node_prepare_diff (node); +if (err == -EAGAIN ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node %u is not ready for diff: " +"ino %llu, logical_offset %llu, size %u\n" , + node->node_id, + node->flush_req.extent.ino, + node->flush_req.extent.logical_offset, + node->flush_req.extent.data_bytes); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_btree_node_prepare_flush_request (node); + } + }else { + err =ssdfs_btree_node_prepare_flush_request (node); + } + +if (unlikely (err)) { +SSDFS_ERR ("update request failed: " +"ino %llu, logical_offset %llu, size %u, err %d\n" , + node->flush_req.extent.ino, + node->flush_req.extent.logical_offset, + node->flush_req.extent.data_bytes, + err); + goto fail_flush_node; + }else if (node_flags &SSDFS_BTREE_NODE_PRE_ALLOCATED ) { +struct ssdfs_btree_node *parent; +struct ssdfs_btree_index_key old_key, new_key; +u16 flags; + +spin_lock (&node->descriptor_lock); +ssdfs_memcpy (&old_key,0 , index_key_size, + &node->node_index,0 , index_key_size, + index_key_size); +spin_unlock (&node->descriptor_lock); + +ssdfs_memcpy (&new_key,0 , index_key_size, + &old_key,0 , index_key_size, + index_key_size); + + flags =le16_to_cpu (old_key.flags); + flags &= ~SSDFS_BTREE_INDEX_SHOW_PREALLOCATED_CHILD ; + new_key.flags =le16_to_cpu (flags); + +spin_lock (&node->descriptor_lock); + parent = node->parent_node; +spin_unlock (&node->descriptor_lock); + + err =ssdfs_btree_node_change_index (parent, + &old_key, + &new_key); +if (!err) { +spin_lock (&node->descriptor_lock); +ssdfs_memcpy (&node->node_index,0 , index_key_size, + &new_key,0 , index_key_size, + index_key_size); +spin_unlock (&node->descriptor_lock); + +atomic_and (~SSDFS_BTREE_NODE_PRE_ALLOCATED , + &node->flags); + } + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("index_start_bit %lu, item_start_bit %lu, " +"bits_count %lu\n" , + node->bmap_array.index_start_bit, + node->bmap_array.item_start_bit, + node->bmap_array.bits_count); +#endif /* CONFIG_SSDFS_DEBUG */ + +return 0 ; + +fail_flush_node: +for (i =0 ; i <pagevec_count (&node->flush_req.result.pvec); i++) { + page = node->flush_req.result.pvec.pages[i]; + +if (!page) +continue ; + +SetPageError (page); +end_page_writeback (page); + } + +ssdfs_request_unlock_and_remove_pages (&node->flush_req); +ssdfs_put_request (&node->flush_req); + +return err; +} + +/* +* ssdfs_btree_node_flush() - flush the dirty btree node +* @node: node object +* +* This method tries to flush the dirty btree node. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +*/ +int ssdfs_btree_node_flush (struct ssdfs_btree_node *node) +{ +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !node->tree); +#endif /* CONFIG_SSDFS_DEBUG */ + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("node_id %u, height %u, type %#x, state %#x\n" , + node->node_id,atomic_read (&node->height), +atomic_read (&node->type), +atomic_read (&node->state)); +#else +SSDFS_DBG ("node_id %u, height %u, type %#x, state %#x\n" , + node->node_id,atomic_read (&node->height), +atomic_read (&node->type), +atomic_read (&node->state)); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + +if (!is_ssdfs_btree_node_dirty (node)) +return 0 ; + +switch (atomic_read (&node->type)) { +case SSDFS_BTREE_ROOT_NODE : +if (!node->tree->btree_ops || + !node->tree->btree_ops->flush_root_node) { +SSDFS_WARN ("unable to flush the root node\n" ); +return -EOPNOTSUPP ; + } + + err = node->tree->btree_ops->flush_root_node (node); +if (unlikely (err)) { +SSDFS_ERR ("fail to flush root node: " +"node_id %u, height %u, err %d\n" , + node->node_id, +atomic_read (&node->height), + err); + } +break ; + +case SSDFS_BTREE_INDEX_NODE : +case SSDFS_BTREE_HYBRID_NODE : +case SSDFS_BTREE_LEAF_NODE : +if (!node->tree->btree_ops || + !node->tree->btree_ops->flush_node) { +SSDFS_WARN ("unable to flush the common node\n" ); +return -EOPNOTSUPP ; + } + + err = node->tree->btree_ops->flush_node (node); +if (unlikely (err)) { +SSDFS_ERR ("fail to flush common node: " +"node_id %u, height %u, err %d\n" , + node->node_id, +atomic_read (&node->height), + err); + } +break ; + +default : + err = -ERANGE ; +SSDFS_WARN ("invalid node type %#x\n" , +atomic_read (&node->type)); +break ; + } + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("finished\n" ); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + +ssdfs_debug_btree_node_object (node); + +return err; +} + +/* +* ssdfs_btree_node_commit_log() - request the log commit for the node +* @node: node object +* +* This method tries to request the log commit for the node. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +*/ +int ssdfs_btree_node_commit_log (struct ssdfs_btree_node *node) +{ +struct ssdfs_fs_info *fsi; +struct ssdfs_segment_info *si; +u64 logical_offset; +u64 seg_id; +u32 logical_blk; +u32 len; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !node->tree || !node->tree->fsi); + +SSDFS_DBG ("node_id %u, height %u, type %#x\n" , + node->node_id,atomic_read (&node->height), +atomic_read (&node->type)); + +switch (atomic_read (&node->type)) { +case SSDFS_BTREE_INDEX_NODE : +case SSDFS_BTREE_HYBRID_NODE : +case SSDFS_BTREE_LEAF_NODE : +/* expected state */ +break ; + +default : +BUG (); + }; +#endif /* CONFIG_SSDFS_DEBUG */ + + fsi = node->tree->fsi; + +ssdfs_request_init (&node->flush_req); +ssdfs_get_request (&node->flush_req); + + logical_offset = (u64 )node->node_id * node->node_size; +ssdfs_request_prepare_logical_extent (node->tree->owner_ino, + (u64 )logical_offset, +0 ,0 ,0 , &node->flush_req); + +spin_lock (&node->descriptor_lock); + si = node->seg; + seg_id =le64_to_cpu (node->extent.seg_id); + logical_blk =le32_to_cpu (node->extent.logical_blk); + len =le32_to_cpu (node->extent.len); +spin_unlock (&node->descriptor_lock); + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!si); +BUG_ON (seg_id != si->seg_id); +#endif /* CONFIG_SSDFS_DEBUG */ + +ssdfs_request_define_segment (seg_id, &node->flush_req); + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (logical_blk >=U16_MAX ); +BUG_ON (len >=U16_MAX ); +#endif /* CONFIG_SSDFS_DEBUG */ +ssdfs_request_define_volume_extent ((u16 )logical_blk, (u16 )len, + &node->flush_req); + + err =ssdfs_segment_commit_log_async (si,SSDFS_REQ_ASYNC_NO_FREE , + &node->flush_req); +if (unlikely (err)) { +SSDFS_ERR ("commit log request failed: " +"ino %llu, logical_offset %llu, err %d\n" , + node->flush_req.extent.ino, + node->flush_req.extent.logical_offset, + err); +ssdfs_put_request (&node->flush_req); + } + +return err; +} + +/* +* ssdfs_btree_deleted_node_commit_log() - request the log commit (deleted node) +* @node: node object +* +* This method tries to request the log commit for the deleted node. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +*/ +int ssdfs_btree_deleted_node_commit_log (struct ssdfs_btree_node *node) +{ +struct ssdfs_fs_info *fsi; +struct ssdfs_segment_info *si; +u64 seg_id; +int i; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !node->tree || !node->tree->fsi); + +SSDFS_DBG ("node_id %u, height %u, type %#x\n" , + node->node_id,atomic_read (&node->height), +atomic_read (&node->type)); + +switch (atomic_read (&node->type)) { +case SSDFS_BTREE_INDEX_NODE : +case SSDFS_BTREE_HYBRID_NODE : +case SSDFS_BTREE_LEAF_NODE : +/* expected state */ +break ; + +default : +BUG (); + }; +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!is_ssdfs_btree_node_pre_deleted (node)) { +SSDFS_ERR ("node %u is not pre-deleted\n" , + node->node_id); +return -ERANGE ; + } + + fsi = node->tree->fsi; + +spin_lock (&node->descriptor_lock); + si = node->seg; + seg_id =le64_to_cpu (node->extent.seg_id); +spin_unlock (&node->descriptor_lock); + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!si); +BUG_ON (seg_id != si->seg_id); +#endif /* CONFIG_SSDFS_DEBUG */ + +for (i =0 ; i < si->pebs_count; i++) { +struct ssdfs_segment_request *req; +struct ssdfs_peb_container *pebc; +struct ssdfs_requests_queue *rq; +wait_queue_head_t *wait; + + pebc = &si->peb_array[i]; + + req =ssdfs_request_alloc (); +if (IS_ERR_OR_NULL (req)) { + err = (req ==NULL ? -ENOMEM :PTR_ERR (req)); +SSDFS_ERR ("fail to allocate segment request: err %d\n" , + err); +return err; + } + +ssdfs_request_init (req); +ssdfs_get_request (req); + +ssdfs_request_prepare_internal_data (SSDFS_PEB_UPDATE_REQ , +SSDFS_COMMIT_LOG_NOW , +SSDFS_REQ_ASYNC , req); +ssdfs_request_define_segment (si->seg_id, req); + +ssdfs_segment_create_request_cno (si); + + rq = &pebc->update_rq; +ssdfs_requests_queue_add_tail_inc (si->fsi, rq, req); + + wait = &si->wait_queue[SSDFS_PEB_FLUSH_THREAD ]; +wake_up_all (wait); + } + +return 0 ; +} + +/* +* is_ssdfs_btree_node_dirty() - check that btree node is dirty +* @node: node object +*/ +bool is_ssdfs_btree_node_dirty (struct ssdfs_btree_node *node) +{ +int state; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node); +#endif /* CONFIG_SSDFS_DEBUG */ + + state =atomic_read (&node->state); + +switch (state) { +case SSDFS_BTREE_NODE_DIRTY : +case SSDFS_BTREE_NODE_PRE_DELETED : +return true; + +case SSDFS_BTREE_NODE_INITIALIZED : +return false; + +default : +SSDFS_WARN ("invalid node state %#x\n" , + state); +/* FALLTHRU */ + }; + +return false; +} + +/* +* set_ssdfs_btree_node_dirty() - set btree node in dirty state +* @node: node object +*/ +void set_ssdfs_btree_node_dirty (struct ssdfs_btree_node *node) +{ +int state; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !node->tree); +#endif /* CONFIG_SSDFS_DEBUG */ + + state =atomic_read (&node->state); + +switch (state) { +case SSDFS_BTREE_NODE_DIRTY : +case SSDFS_BTREE_NODE_INITIALIZED : +case SSDFS_BTREE_NODE_CREATED : +atomic_set (&node->state,SSDFS_BTREE_NODE_DIRTY ); +spin_lock (&node->tree->nodes_lock); +radix_tree_tag_set (&node->tree->nodes, node->node_id, +SSDFS_BTREE_NODE_DIRTY_TAG ); +spin_unlock (&node->tree->nodes_lock); +break ; + +default : +SSDFS_WARN ("invalid node state %#x\n" , + state); +/* FALLTHRU */ + }; +} + +/* +* clear_ssdfs_btree_node_dirty() - clear dirty state of btree node +* @node: node object +*/ +void clear_ssdfs_btree_node_dirty (struct ssdfs_btree_node *node) +{ +int state; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !node->tree); +BUG_ON (!rwsem_is_locked (&node->tree->lock)); +#endif /* CONFIG_SSDFS_DEBUG */ + + state =atomic_read (&node->state); + +switch (state) { +case SSDFS_BTREE_NODE_DIRTY : +atomic_set (&node->state,SSDFS_BTREE_NODE_INITIALIZED ); +spin_lock (&node->tree->nodes_lock); +radix_tree_tag_clear (&node->tree->nodes, node->node_id, +SSDFS_BTREE_NODE_DIRTY_TAG ); +spin_unlock (&node->tree->nodes_lock); +break ; + +case SSDFS_BTREE_NODE_CORRUPTED : +spin_lock (&node->tree->nodes_lock); +radix_tree_tag_clear (&node->tree->nodes, node->node_id, +SSDFS_BTREE_NODE_DIRTY_TAG ); +spin_unlock (&node->tree->nodes_lock); +break ; + +case SSDFS_BTREE_NODE_INITIALIZED : +/* do nothing */ +break ; + +default : +SSDFS_WARN ("invalid node state %#x\n" , + state); +/* FALLTHRU */ + }; +} + +/* +* is_ssdfs_btree_node_pre_deleted() - check that btree node is pre-deleted +* @node: node object +*/ +bool is_ssdfs_btree_node_pre_deleted (struct ssdfs_btree_node *node) +{ +int state; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node); +#endif /* CONFIG_SSDFS_DEBUG */ + + state =atomic_read (&node->state); + +switch (state) { +case SSDFS_BTREE_NODE_PRE_DELETED : +return true; + +case SSDFS_BTREE_NODE_INITIALIZED : +case SSDFS_BTREE_NODE_DIRTY : +return false; + +default : +SSDFS_WARN ("invalid node state %#x\n" , + state); +/* FALLTHRU */ + }; + +return false; +} + +/* +* set_ssdfs_btree_node_pre_deleted() - set btree node in pre-deleted state +* @node: node object +*/ +void set_ssdfs_btree_node_pre_deleted (struct ssdfs_btree_node *node) +{ +int state; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !node->tree); +#endif /* CONFIG_SSDFS_DEBUG */ + + state =atomic_read (&node->state); + +switch (state) { +case SSDFS_BTREE_NODE_PRE_DELETED : +case SSDFS_BTREE_NODE_DIRTY : +case SSDFS_BTREE_NODE_INITIALIZED : +case SSDFS_BTREE_NODE_CREATED : +atomic_set (&node->state,SSDFS_BTREE_NODE_PRE_DELETED ); +spin_lock (&node->tree->nodes_lock); +radix_tree_tag_set (&node->tree->nodes, node->node_id, +SSDFS_BTREE_NODE_DIRTY_TAG ); +spin_unlock (&node->tree->nodes_lock); +break ; + +default : +SSDFS_WARN ("invalid node state %#x\n" , + state); +/* FALLTHRU */ + }; +} + +/* +* clear_ssdfs_btree_node_pre_deleted() - clear pre-deleted state of btree node +* @node: node object +*/ +void clear_ssdfs_btree_node_pre_deleted (struct ssdfs_btree_node *node) +{ +int state; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !node->tree); +BUG_ON (!rwsem_is_locked (&node->tree->lock)); +#endif /* CONFIG_SSDFS_DEBUG */ + + state =atomic_read (&node->state); + +switch (state) { +case SSDFS_BTREE_NODE_PRE_DELETED : +atomic_set (&node->state,SSDFS_BTREE_NODE_INITIALIZED ); +spin_lock (&node->tree->nodes_lock); +radix_tree_tag_clear (&node->tree->nodes, node->node_id, +SSDFS_BTREE_NODE_DIRTY_TAG ); +spin_unlock (&node->tree->nodes_lock); +break ; + +case SSDFS_BTREE_NODE_CORRUPTED : +spin_lock (&node->tree->nodes_lock); +radix_tree_tag_clear (&node->tree->nodes, node->node_id, +SSDFS_BTREE_NODE_DIRTY_TAG ); +spin_unlock (&node->tree->nodes_lock); +break ; + +case SSDFS_BTREE_NODE_INITIALIZED : +/* do nothing */ +break ; + +default : +SSDFS_WARN ("invalid node state %#x\n" , + state); +/* FALLTHRU */ + }; +} + +/* +* is_ssdfs_btree_node_index_area_exist() - check that node has index area +* @node: node object +*/ +bool is_ssdfs_btree_node_index_area_exist (struct ssdfs_btree_node *node) +{ +u16 flags; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node); + +SSDFS_DBG ("node_id %u\n" , node->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ + +switch (atomic_read (&node->state)) { +case SSDFS_BTREE_NODE_CREATED : +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node %u is not initialized\n" , + node->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ +break ; + +case SSDFS_BTREE_NODE_INITIALIZED : +case SSDFS_BTREE_NODE_DIRTY : +case SSDFS_BTREE_NODE_PRE_DELETED : +/* expected state */ +break ; + +default : +BUG (); + } + +switch (atomic_read (&node->type)) { +case SSDFS_BTREE_ROOT_NODE : +return true; + +case SSDFS_BTREE_INDEX_NODE : + flags =atomic_read (&node->flags); +if (flags &SSDFS_BTREE_NODE_HAS_INDEX_AREA ) +return true; +else { +SSDFS_WARN ("index node %u hasn't index area\n" , + node->node_id); + } +break ; + +case SSDFS_BTREE_HYBRID_NODE : + flags =atomic_read (&node->flags); +if (flags &SSDFS_BTREE_NODE_HAS_INDEX_AREA ) +return true; +break ; + +case SSDFS_BTREE_LEAF_NODE : +/* do nothing */ +break ; + +default : +BUG (); + } + +return false; +} + +/* +* is_ssdfs_btree_node_index_area_empty() - check that index area is empty +* @node: node object +*/ +bool is_ssdfs_btree_node_index_area_empty (struct ssdfs_btree_node *node) +{ +bool is_empty = false; +int state; +int flags; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node); + +SSDFS_DBG ("node_id %u\n" , node->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ + +switch (atomic_read (&node->state)) { +case SSDFS_BTREE_NODE_CREATED : +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node %u is not initialized\n" , + node->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ +break ; + +case SSDFS_BTREE_NODE_INITIALIZED : +case SSDFS_BTREE_NODE_DIRTY : +case SSDFS_BTREE_NODE_PRE_DELETED : +/* expected state */ +break ; + +default : +BUG (); + } + +switch (atomic_read (&node->type)) { +case SSDFS_BTREE_ROOT_NODE : +/* need to check the index area */ +break ; + +case SSDFS_BTREE_INDEX_NODE : + flags =atomic_read (&node->flags); +if (flags &SSDFS_BTREE_NODE_HAS_INDEX_AREA ) { +/* +* need to check the index area +*/ + }else { +SSDFS_WARN ("index node %u hasn't index area\n" , + node->node_id); +return false; + } +break ; + +case SSDFS_BTREE_HYBRID_NODE : + flags =atomic_read (&node->flags); +if (flags &SSDFS_BTREE_NODE_HAS_INDEX_AREA ) { +/* +* need to check the index area +*/ + }else { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node %u hasn't index area\n" , + node->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ +return true; + } +break ; + +case SSDFS_BTREE_LEAF_NODE : +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node %u is leaf node\n" , + node->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ +return true; + +default : +BUG (); + } + +down_read (&node->header_lock); + state =atomic_read (&node->index_area.state); +if (state !=SSDFS_BTREE_NODE_INDEX_AREA_EXIST ) + err = -ERANGE ; +else if (node->index_area.index_capacity ==0 ) + err = -ERANGE ; +else + is_empty = node->index_area.index_count ==0 ; +up_read (&node->header_lock); + +if (unlikely (err)) { +SSDFS_WARN ("node %u is corrupted\n" , node->node_id); +return false; + } + +return is_empty; +} + +/* +* is_ssdfs_btree_node_items_area_exist() - check that node has items area +* @node: node object +*/ +bool is_ssdfs_btree_node_items_area_exist (struct ssdfs_btree_node *node) +{ +u16 flags; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node); + +SSDFS_DBG ("node_id %u\n" , node->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ + +switch (atomic_read (&node->state)) { +case SSDFS_BTREE_NODE_CREATED : +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node %u is under initialization\n" , + node->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ +return false; + +case SSDFS_BTREE_NODE_INITIALIZED : +case SSDFS_BTREE_NODE_DIRTY : +case SSDFS_BTREE_NODE_PRE_DELETED : +/* expected state */ +break ; + +default : +BUG (); + } + +switch (atomic_read (&node->type)) { +case SSDFS_BTREE_ROOT_NODE : +case SSDFS_BTREE_INDEX_NODE : +return false; + +case SSDFS_BTREE_HYBRID_NODE : + flags =atomic_read (&node->flags); +if (flags &SSDFS_BTREE_NODE_HAS_ITEMS_AREA ) +return true; +break ; + +case SSDFS_BTREE_LEAF_NODE : + flags =atomic_read (&node->flags); +if (flags &SSDFS_BTREE_NODE_HAS_ITEMS_AREA ) +return true; +else { +SSDFS_WARN ("corrupted leaf node %u\n" , + node->node_id); + } +break ; + +default : +BUG (); + } + +return false; +} + +/* +* is_ssdfs_btree_node_items_area_empty() - check that items area is empty +* @node: node object +*/ +bool is_ssdfs_btree_node_items_area_empty (struct ssdfs_btree_node *node) +{ +bool is_empty = false; +int state; +int flags; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node); + +SSDFS_DBG ("node_id %u\n" , node->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ + +switch (atomic_read (&node->state)) { +case SSDFS_BTREE_NODE_CREATED : +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node %u is under initialization\n" , + node->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ +return false; + +case SSDFS_BTREE_NODE_INITIALIZED : +case SSDFS_BTREE_NODE_DIRTY : +case SSDFS_BTREE_NODE_PRE_DELETED : +/* expected state */ +break ; + +default : +BUG (); + } + +switch (atomic_read (&node->type)) { +case SSDFS_BTREE_ROOT_NODE : +case SSDFS_BTREE_INDEX_NODE : +return true; + +case SSDFS_BTREE_HYBRID_NODE : + flags =atomic_read (&node->flags); +if (flags &SSDFS_BTREE_NODE_HAS_ITEMS_AREA ) { +/* +* need to check the items area +*/ + }else { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node %u hasn't items area\n" , + node->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ +return true; + } +break ; + +case SSDFS_BTREE_LEAF_NODE : + flags =atomic_read (&node->flags); +if (flags &SSDFS_BTREE_NODE_HAS_ITEMS_AREA ) { +/* +* need to check the items area +*/ + }else { +SSDFS_WARN ("leaf node %u hasn't items area\n" , + node->node_id); +return false; + } +break ; + +default : +BUG (); + } + +down_read (&node->header_lock); + state =atomic_read (&node->items_area.state); +if (state !=SSDFS_BTREE_NODE_ITEMS_AREA_EXIST ) + err = -ERANGE ; +else if (node->items_area.items_capacity ==0 ) + err = -ERANGE ; +else + is_empty = node->items_area.items_count ==0 ; +up_read (&node->header_lock); + +if (unlikely (err)) { +SSDFS_WARN ("node %u is corrupted\n" , node->node_id); +return false; + } + +return is_empty; +} + +/* +* ssdfs_btree_node_shrink_index_area() - shrink the index area +* @node: node object +* @new_size: the new size of index area in bytes +* +* This method tries to shrink the index area in size. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +* %-EOPNOTSUPP - requsted action is not supported. +*/ +static +int ssdfs_btree_node_shrink_index_area (struct ssdfs_btree_node *node, +u32 new_size) +{ +u8 index_size; +u16 index_count; +u16 index_capacity; +u32 area_size; +u32 cur_size; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node); +BUG_ON (!rwsem_is_locked (&node->full_lock)); +BUG_ON (!rwsem_is_locked (&node->header_lock)); + +SSDFS_DBG ("node_id %u, new_size %u\n" , + node->node_id, new_size); +#endif /* CONFIG_SSDFS_DEBUG */ + + index_size = node->index_area.index_size; + index_count = node->index_area.index_count; + index_capacity = node->index_area.index_capacity; + area_size = node->index_area.area_size; + + cur_size = (u32 )index_size * index_count; + +if (area_size <= new_size) { +SSDFS_ERR ("cannot grow index area: " +"area_size %u, new_size %u\n" , + area_size, new_size); +return -EOPNOTSUPP ; + } + +if (new_size % index_size) { +SSDFS_ERR ("unaligned new_size: " +"index_size %u, new_size %u\n" , + index_size, new_size); +return -ERANGE ; + } + +if (cur_size > area_size) { +SSDFS_WARN ("invalid cur_size: " +"cur_size %u, area_size %u\n" , + cur_size, area_size); +return -ERANGE ; + } + +if (cur_size == area_size || cur_size > new_size) { +SSDFS_ERR ("unable to shrink index area: " +"cur_size %u, new_size %u, area_size %u\n" , + cur_size, new_size, area_size); +return -ERANGE ; + } + + node->index_area.area_size = new_size; + node->index_area.index_capacity = new_size / index_size; + +return 0 ; +} + +/* +* ssdfs_btree_node_grow_index_area() - grow the index area +* @node: node object +* @new_size: the new size of index area in bytes +* +* This method tries to increase the size of index area. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +* %-EOPNOTSUPP - requsted action is not supported. +*/ +static +int ssdfs_btree_node_grow_index_area (struct ssdfs_btree_node *node, +u32 new_size) +{ +u8 index_size; +u16 index_count; +u16 index_capacity; +u32 area_size; +u32 cur_size; +unsigned long offset1, offset2; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node); +BUG_ON (!rwsem_is_locked (&node->full_lock)); +BUG_ON (!rwsem_is_locked (&node->header_lock)); + +SSDFS_DBG ("node_id %u, new_size %u\n" , + node->node_id, new_size); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (new_size > node->node_size) { +SSDFS_ERR ("invalid new size: " +"new_size %u, node_size %u\n" , + new_size, node->node_size); +return -ERANGE ; + } + + index_size = node->index_area.index_size; + index_count = node->index_area.index_count; + index_capacity = node->index_area.index_capacity; + area_size = node->index_area.area_size; + + cur_size = (u32 )index_size * index_count; + +if (area_size > new_size) { +SSDFS_ERR ("cannot shrink index area: " +"area_size %u, new_size %u\n" , + area_size, new_size); +return -EOPNOTSUPP ; + } + +if (new_size % index_size) { +SSDFS_ERR ("unaligned new_size: " +"index_size %u, new_size %u\n" , + index_size, new_size); +return -ERANGE ; + } + +if (cur_size > area_size) { +SSDFS_WARN ("invalid cur_size: " +"cur_size %u, area_size %u\n" , + cur_size, area_size); +return -ERANGE ; + } + + offset1 = node->items_area.offset; + offset2 = node->index_area.offset; + +if (new_size == node->node_size) { + node->index_area.index_capacity = + (new_size - node->index_area.offset) / index_size; + }else if ((offset1 - offset2) != new_size) { +SSDFS_ERR ("unable to resize the index area: " +"items_area.offset %u, index_area.offset %u, " +"new_size %u\n" , + node->items_area.offset, + node->index_area.offset, + new_size); +return -ERANGE ; + }else + node->index_area.index_capacity = new_size / index_size; + +down_read (&node->bmap_array.lock); + offset1 = node->bmap_array.item_start_bit; + offset2 = node->bmap_array.index_start_bit; +if ((offset1 - offset2) < node->index_area.index_capacity) + err = -ERANGE ; +up_read (&node->bmap_array.lock); + +if (unlikely (err)) { +SSDFS_ERR ("unable to resize the index area: " +"items_start_bit %lu, index_start_bit %lu, " +"new_index_capacity %u\n" , + node->bmap_array.item_start_bit, + node->bmap_array.index_start_bit, + new_size / index_size); +return -ERANGE ; + } + +if (new_size == node->node_size) + node->index_area.area_size = new_size - node->index_area.offset; +else + node->index_area.area_size = new_size; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("items_area: offset %u, area_size %u, " +"free_space %u, capacity %u; " +"index_area: offset %u, area_size %u, " +"capacity %u\n" , + node->items_area.offset, + node->items_area.area_size, + node->items_area.free_space, + node->items_area.items_capacity, + node->index_area.offset, + node->index_area.area_size, + node->index_area.index_capacity); +#endif /* CONFIG_SSDFS_DEBUG */ + +return 0 ; +} + +/* +* ssdfs_check_btree_node_after_resize() - check btree node's consistency +* @node: node object +* +* This method tries to check the consistency of btree node +* after resize. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - btree node is inconsistent. +*/ +#ifdef CONFIG_SSDFS_DEBUG +static +int ssdfs_check_btree_node_after_resize (struct ssdfs_btree_node *node) +{ +u32 offset; +u32 area_size; +u8 index_size; +u16 index_count; +u16 index_capacity; +u16 items_count; +u16 items_capacity; +u32 average_item_size; +unsigned long bits_count; +unsigned long index_start_bit, item_start_bit; +int err =0 ; + +BUG_ON (!node); +BUG_ON (!rwsem_is_locked (&node->full_lock)); +BUG_ON (!rwsem_is_locked (&node->header_lock)); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node_id %u\n" , node->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ + + offset = node->index_area.offset; + area_size = node->index_area.area_size; + +if ((offset + area_size) == node->node_size) { +/* +* Continue logic +*/ + }else if ((offset + area_size) != node->items_area.offset) { +SSDFS_ERR ("invalid index area: " +"index_area.offset %u, " +"index_area.area_size %u, " +"items_area.offset %u\n" , + node->index_area.offset, + node->index_area.area_size, + node->items_area.offset); +return -ERANGE ; + } + + index_size = node->index_area.index_size; + index_count = node->index_area.index_count; + index_capacity = node->index_area.index_capacity; + +if (index_count > index_capacity) { +SSDFS_ERR ("index_count %u > index_capacity %u\n" , + node->index_area.index_count, + node->index_area.index_capacity); +return -ERANGE ; + } + +if (((u32 )index_size * index_capacity) > area_size) { +SSDFS_ERR ("invalid index area: " +"index_size %u, index_capacity %u, " +"area_size %u\n" , + node->index_area.index_size, + node->index_area.index_capacity, + node->index_area.area_size); +return -ERANGE ; + } + + offset = node->items_area.offset; + area_size = node->items_area.area_size; + +if (area_size >0 ) { +if ((offset + area_size) != node->node_size) { +SSDFS_ERR ("invalid items area: " +"items_area.offset %u, " +"items_area.area_size %u, " +"node_size %u\n" , + node->items_area.offset, + node->items_area.area_size, + node->node_size); +return -ERANGE ; + } + } + + items_count = node->items_area.items_count; + items_capacity = node->items_area.items_capacity; + +if (items_count > items_capacity) { +SSDFS_ERR ("invalid items area: " +"items_area.items_count %u, " +"items_area.items_capacity %u\n" , + node->items_area.items_count, + node->items_area.items_capacity); +return -ERANGE ; + } + +if (items_capacity >0 ) { + average_item_size = area_size / items_capacity; +if (average_item_size < node->items_area.item_size || + average_item_size > node->items_area.max_item_size) { +SSDFS_ERR ("invalid items area: " +"average_item_size %u, " +"item_size %u, max_item_size %u\n" , + average_item_size, + node->items_area.item_size, + node->items_area.max_item_size); +return -ERANGE ; + } + } + +down_read (&node->bmap_array.lock); + bits_count = node->bmap_array.bits_count; + index_start_bit = node->bmap_array.index_start_bit; + item_start_bit = node->bmap_array.item_start_bit; +if ((index_capacity + items_capacity +1 ) > bits_count) + err = -ERANGE ; +if ((item_start_bit - index_start_bit) < index_capacity) + err = -ERANGE ; +if ((bits_count - item_start_bit) < items_capacity) + err = -ERANGE ; +up_read (&node->bmap_array.lock); + +if (unlikely (err)) { +SSDFS_ERR ("invalid bmap_array: " +"bits_count %lu, index_start_bit %lu, " +"item_start_bit %lu, index_capacity %u, " +"items_capacity %u\n" , + bits_count, index_start_bit, + item_start_bit, index_capacity, + items_capacity); +return err; + } + +return 0 ; +} +#endif /* CONFIG_SSDFS_DEBUG */ + +static inline +void ssdfs_set_node_update_cno (struct ssdfs_btree_node *node) +{ +u64 current_cno =ssdfs_current_cno (node->tree->fsi->sb); + +spin_lock (&node->descriptor_lock); + node->update_cno = current_cno; +spin_unlock (&node->descriptor_lock); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("current_cno %llu\n" , current_cno); +#endif /* CONFIG_SSDFS_DEBUG */ +} + +/* +* ssdfs_btree_node_resize_index_area() - resize the node's index area +* @node: node object +* @new_size: new size of node's index area +* +* This method tries to resize the index area of btree node. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +* %-EACCES - node is under initialization yet. +* %-ENOENT - index area is absent. +* %-ENOSPC - index area cannot be resized. +* %-EOPNOTSUPP - resize operation is not supported. +*/ +int ssdfs_btree_node_resize_index_area (struct ssdfs_btree_node *node, +u32 new_size) +{ +struct ssdfs_fs_info *fsi; +u16 flags; +u8 index_size; +u16 index_count; +u16 index_capacity; +u32 area_size; +u32 cur_size; +u32 new_items_area_size; +int err =0 , err2; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !node->tree || !node->tree->fsi); + +SSDFS_DBG ("node_id %u, new_size %u\n" , + node->node_id, new_size); +#endif /* CONFIG_SSDFS_DEBUG */ + + fsi = node->tree->fsi; + +switch (atomic_read (&node->state)) { +case SSDFS_BTREE_NODE_CREATED : +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node %u is under initialization\n" , + node->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ +return -EACCES ; + +case SSDFS_BTREE_NODE_INITIALIZED : +case SSDFS_BTREE_NODE_DIRTY : +/* expected state */ +break ; + +default : +SSDFS_ERR ("invalid node state %#x\n" , +atomic_read (&node->state)); +return -ERANGE ; + } + +switch (atomic_read (&node->type)) { +case SSDFS_BTREE_ROOT_NODE : +case SSDFS_BTREE_INDEX_NODE : +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("resize operation is unavailable: " +"node_id %u\n" , + node->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ENOSPC ; + +case SSDFS_BTREE_LEAF_NODE : +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("index area is absent: " +"node_id %u\n" , + node->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ENOENT ; + +case SSDFS_BTREE_HYBRID_NODE : +/* expected node type */ +break ; + +default : +BUG (); + } + +if (!is_ssdfs_btree_node_index_area_exist (node)) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("index area is absent: " +"node_id %u\n" , + node->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ENOENT ; + } + + flags =atomic_read (&node->tree->flags); +if (!(flags &SSDFS_BTREE_DESC_INDEX_AREA_RESIZABLE )) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to resize the index area: " +"node_id %u\n" , + node->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ENOSPC ; + } + +if (new_size < node->tree->index_area_min_size || + new_size > node->node_size) { +SSDFS_ERR ("invalid new_size %u\n" , + new_size); +return -ERANGE ; + } + +if (!node->node_ops || !node->node_ops->resize_items_area) { +SSDFS_DBG ("unable to resize items area\n" ); +return -EOPNOTSUPP ; + } + +down_write (&node->full_lock); +down_write (&node->header_lock); + + index_size = node->index_area.index_size; + index_count = node->index_area.index_count; + index_capacity = node->index_area.index_capacity; + area_size = node->index_area.area_size; + +if (index_count > index_capacity) { + err = -ERANGE ; +SSDFS_ERR ("index_count %u > index_capacity %u\n" , + index_count, index_capacity); + goto finish_resize_operation; + } + +if (new_size % index_size) { + err = -ERANGE ; +SSDFS_ERR ("unaligned new_size: " +"new_size %u, index_size %u\n" , + new_size, index_size); + goto finish_resize_operation; + } + +if ((index_size * index_capacity) != area_size) { + err = -ERANGE ; +SSDFS_ERR ("invalid index area descriptor: " +"index_size %u, index_capacity %u, " +"area_size %u\n" , + index_size, index_capacity, area_size); + goto finish_resize_operation; + } + + cur_size = (u32 )index_size * index_count; + +if (cur_size > area_size) { + err = -ERANGE ; +SSDFS_ERR ("cur_size %u > area_size %u\n" , + cur_size, area_size); + goto finish_resize_operation; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("index_size %u, index_count %u, " +"index_capacity %u, index_area_size %u, " +"cur_size %u, new_size %u\n" , + index_size, index_count, + index_capacity, area_size, + cur_size, new_size); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (new_size < node->index_area.area_size) { +/* shrink index area */ + +if (cur_size > new_size) { + err = -ENOSPC ; +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to resize: " +"cur_size %u, new_size %u\n" , + cur_size, new_size); +#endif /* CONFIG_SSDFS_DEBUG */ + goto finish_resize_operation; + } + + err =ssdfs_btree_node_shrink_index_area (node, new_size); +if (err == -ENOSPC ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to shrink index area: " +"new_size %u\n" , + new_size); +#endif /* CONFIG_SSDFS_DEBUG */ + goto finish_resize_operation; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to shrink index area: " +"new_size %u, err %d\n" , + new_size, err); + goto finish_resize_operation; + } + + new_items_area_size = node->items_area.area_size; + new_items_area_size += area_size - new_size; + + err = node->node_ops->resize_items_area (node, + new_items_area_size); +if (err) { + err2 =ssdfs_btree_node_grow_index_area (node, + cur_size); +if (err == -EOPNOTSUPP || err == -ENOSPC ) { + err = err2; +SSDFS_ERR ("fail to recover node state: " +"err %d\n" , err); + goto finish_resize_operation; + } + } + +if (err == -EOPNOTSUPP ) { + err = -ENOSPC ; +SSDFS_DBG ("resize operation is unavailable\n" ); + goto finish_resize_operation; + }else if (err == -ENOSPC ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to resize items area: " +"new_size %u\n" , + new_items_area_size); +#endif /* CONFIG_SSDFS_DEBUG */ + goto finish_resize_operation; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to resize items area: " +"new_size %u, err %d\n" , + new_items_area_size, err); + goto finish_resize_operation; + } + }else if (new_size > node->index_area.area_size) { +/* grow index area */ + +if (new_size == node->node_size) { +/* eliminate items area */ + new_items_area_size =0 ; + }else if ((new_size - area_size) > node->items_area.area_size) { + err = -ENOSPC ; +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to resize items area: " +"new_size %u, index_area_size %u, " +"items_area_size %u\n" , + new_size, + node->index_area.area_size, + node->items_area.area_size); +#endif /* CONFIG_SSDFS_DEBUG */ + goto finish_resize_operation; + }else { + new_items_area_size = node->items_area.area_size; + new_items_area_size -= new_size - area_size; + } + + err = node->node_ops->resize_items_area (node, + new_items_area_size); +if (err == -EOPNOTSUPP ) { + err = -ENOSPC ; +SSDFS_DBG ("resize operation is unavailable\n" ); + goto finish_resize_operation; + }else if (err == -ENOSPC ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to resize items area: " +"new_size %u\n" , + new_items_area_size); +#endif /* CONFIG_SSDFS_DEBUG */ + goto finish_resize_operation; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to resize items area: " +"new_size %u, err %d\n" , + new_items_area_size, err); + goto finish_resize_operation; + } + + err =ssdfs_btree_node_grow_index_area (node, new_size); +if (err == -ENOSPC ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to grow index area: " +"new_size %u\n" , + new_size); +#endif /* CONFIG_SSDFS_DEBUG */ + goto finish_resize_operation; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to grow index area: " +"new_size %u, err %d\n" , + new_size, err); + goto finish_resize_operation; + } + }else { + err = -EOPNOTSUPP ; +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("resize is not necessary: " +"old_size %u, new_size %u\n" , + node->index_area.area_size, + new_size); +#endif /* CONFIG_SSDFS_DEBUG */ + goto finish_resize_operation; + } + +#ifdef CONFIG_SSDFS_DEBUG + err =ssdfs_check_btree_node_after_resize (node); +if (unlikely (err)) { +SSDFS_ERR ("node %u is corrupted after resize\n" , + node->node_id); + goto finish_resize_operation; + } +#endif /* CONFIG_SSDFS_DEBUG */ + +finish_resize_operation: +up_write (&node->header_lock); +up_write (&node->full_lock); + +if (err == -EOPNOTSUPP ) +return 0 ; +else if (unlikely (err)) +return err; + +ssdfs_set_node_update_cno (node); +set_ssdfs_btree_node_dirty (node); + +return 0 ; +} + +/* +* ssdfs_set_dirty_index_range() - set index range as dirty +* @node: node object +* @start_index: starting index +* @count: count of indexes in the range +* +* This method tries to mark an index range as dirty. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +* %-EEXIST - area is dirty already. +*/ +static +int ssdfs_set_dirty_index_range (struct ssdfs_btree_node *node, +u16 start_index,u16 count) +{ +struct ssdfs_state_bitmap *bmap; +unsigned long found =ULONG_MAX ; +unsigned long start_area; +u16 capacity =0 ; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node); +BUG_ON (!rwsem_is_locked (&node->full_lock)); + +SSDFS_DBG ("capacity %u, start_index %u, count %u\n" , + capacity, start_index, count); +#endif /* CONFIG_SSDFS_DEBUG */ + +down_read (&node->bmap_array.lock); + + start_area = node->bmap_array.index_start_bit; +if (start_area ==ULONG_MAX ) { + err = -ERANGE ; +SSDFS_ERR ("invalid items_area_start\n" ); + goto finish_set_dirty_index; + } + +if (node->bmap_array.item_start_bit ==ULONG_MAX ) + capacity = node->bmap_array.bits_count; +else + capacity = node->bmap_array.item_start_bit - start_area; + + bmap = &node->bmap_array.bmap[SSDFS_BTREE_NODE_DIRTY_BMAP ]; +if (!bmap->ptr) { + err = -ERANGE ; +SSDFS_WARN ("dirty bitmap is empty\n" ); + goto finish_set_dirty_index; + } + +spin_lock (&bmap->lock); + + found =bitmap_find_next_zero_area (bmap->ptr, capacity, + start_area + start_index, + count,0 ); +if (found != (start_area + start_index)) { +/* area is dirty already */ + err = -EEXIST ; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("set bit: start_area %lu, start_index %u, len %u\n" , + start_area, start_index, count); +#endif /* CONFIG_SSDFS_DEBUG */ + +bitmap_set (bmap->ptr, start_area + start_index, count); + +spin_unlock (&bmap->lock); + +if (unlikely (err)) { + err =0 ; +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("found %lu != start %lu\n" , + found, start_area + start_index); +#endif /* CONFIG_SSDFS_DEBUG */ + } + +finish_set_dirty_index: +up_read (&node->bmap_array.lock); + +if (!err) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node %u, tree_type %#x, " +"start_index %u, count %u\n" , + node->node_id, node->tree->type, + start_index, count); +#endif /* CONFIG_SSDFS_DEBUG */ + } + +return err; +} + +/* +* ssdfs_clear_dirty_index_range_state() - clear an index range as dirty +* @node: node object +* @start_index: starting index +* @count: count of indexes in the range +* +* This method tries to clear the state of index range as dirty. +*/ +#ifdef CONFIG_SSDFS_UNDER_DEVELOPMENT_FUNC +static +void ssdfs_clear_dirty_index_range_state (struct ssdfs_btree_node *node, +u16 start_index,u16 count) +{ +struct ssdfs_state_bitmap *bmap; +unsigned long start_area; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node); +BUG_ON (!rwsem_is_locked (&node->full_lock)); + +SSDFS_DBG ("start_index %u, count %u\n" , + start_index, count); +#endif /* CONFIG_SSDFS_DEBUG */ + +down_read (&node->bmap_array.lock); + + start_area = node->bmap_array.index_start_bit; +BUG_ON (start_area ==ULONG_MAX ); + + bmap = &node->bmap_array.bmap[SSDFS_BTREE_NODE_DIRTY_BMAP ]; +BUG_ON (!bmap->ptr); + +spin_lock (&bmap->lock); +bitmap_clear (bmap->ptr, start_area + start_index, count); +spin_unlock (&bmap->lock); + +up_read (&node->bmap_array.lock); +} +#endif /* CONFIG_SSDFS_UNDER_DEVELOPMENT_FUNC */ + +/* +* __ssdfs_lock_index_range() - lock index range +* @node: node object +* @start_index: starting index +* @count: count of indexes in the range +* +* This method tries to lock index range without semaphore protection. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +* %-ENODATA - unable to lock the index range. +*/ +static +int __ssdfs_lock_index_range (struct ssdfs_btree_node *node, +u16 start_index,u16 count) +{ +DEFINE_WAIT (wait); +struct ssdfs_state_bitmap *bmap; +unsigned long start_area; +unsigned long upper_bound; +int i =0 ; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node); +BUG_ON (!rwsem_is_locked (&node->full_lock)); +BUG_ON (!rwsem_is_locked (&node->bmap_array.lock)); + +SSDFS_DBG ("start_index %u, count %u\n" , + start_index, count); +#endif /* CONFIG_SSDFS_DEBUG */ + + start_area = node->bmap_array.index_start_bit; +if (start_area ==ULONG_MAX ) { +SSDFS_ERR ("invalid items_area_start\n" ); +return -ERANGE ; + } + + upper_bound = start_area + start_index + count; +if (upper_bound > node->bmap_array.item_start_bit) { +SSDFS_ERR ("invalid request: " +"start_area %lu, start_index %u, " +"count %u, item_start_bit %lu\n" , + start_area, start_index, count, + node->bmap_array.item_start_bit); +return -ERANGE ; + } + + bmap = &node->bmap_array.bmap[SSDFS_BTREE_NODE_LOCK_BMAP ]; +if (!bmap->ptr) { +SSDFS_WARN ("lock bitmap is empty\n" ); +return -ERANGE ; + } + +try_lock_area: +spin_lock (&bmap->lock); + +for (; i < count; i++) { + err =bitmap_allocate_region (bmap->ptr, + start_area + start_index + i,0 ); +if (err) +break ; + } + +if (err == -EBUSY ) { + err =0 ; +bitmap_clear (bmap->ptr, start_area + start_index, i); +prepare_to_wait (&node->wait_queue, &wait, +TASK_UNINTERRUPTIBLE ); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("waiting unlocked state of item %u\n" , + start_index + i); +#endif /* CONFIG_SSDFS_DEBUG */ + +spin_unlock (&bmap->lock); + +schedule (); +finish_wait (&node->wait_queue, &wait); + goto try_lock_area; + } + +spin_unlock (&bmap->lock); + +return err; +} + +/* +* ssdfs_lock_index_range() - lock index range +* @node: node object +* @start_index: starting index +* @count: count of indexes in the range +* +* This method tries to lock index range. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +* %-ENODATA - unable to lock the index range. +*/ +static inline +int ssdfs_lock_index_range (struct ssdfs_btree_node *node, +u16 start_index,u16 count) +{ +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node); +BUG_ON (!rwsem_is_locked (&node->full_lock)); + +SSDFS_DBG ("start_index %u, count %u\n" , + start_index, count); +#endif /* CONFIG_SSDFS_DEBUG */ + +down_read (&node->bmap_array.lock); + err =__ssdfs_lock_index_range (node, start_index, count); +up_read (&node->bmap_array.lock); + +if (err) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to lock range: " +"start %u, count %u, err %d\n" , + start_index, count, err); +#endif /* CONFIG_SSDFS_DEBUG */ + } + +return err; +} + +/* +* ssdfs_lock_whole_index_area() - lock the whole index area +* @node: node object +* @start_index: starting index +* @count: count of indexes in the range +* +* This method tries to lock the whole index area. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +* %-ENODATA - unable to lock the index range. +*/ +int ssdfs_lock_whole_index_area (struct ssdfs_btree_node *node) +{ +unsigned long start, count; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node); +BUG_ON (!rwsem_is_locked (&node->full_lock)); + +SSDFS_DBG ("node_id %u\n" , node->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ + +down_read (&node->bmap_array.lock); + start = node->bmap_array.index_start_bit; + count = node->bmap_array.item_start_bit - start; +#ifdef CONFIG_SSDFS_DEBUG +if (start >=U16_MAX || count >=U16_MAX ) { +SSDFS_ERR ("start %lu, count %lu\n" , + start, count); + } + +BUG_ON (start >=U16_MAX ); +BUG_ON (count >=U16_MAX ); +#endif /* CONFIG_SSDFS_DEBUG */ + err =__ssdfs_lock_index_range (node,0 , (u16 )count); +up_read (&node->bmap_array.lock); + +if (err) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to lock range: " +"start %lu, count %lu, err %d\n" , + start, count, err); +#endif /* CONFIG_SSDFS_DEBUG */ + } + +return err; +} + +/* +* __ssdfs_unlock_index_range() - unlock an index range +* @node: node object +* @start_index: starting index +* @count: count of indexes in the range +* +* This method tries to unlock an index range without node's +* semaphore protection. +*/ +static +void __ssdfs_unlock_index_range (struct ssdfs_btree_node *node, +u16 start_index,u16 count) +{ +struct ssdfs_state_bitmap *bmap; +unsigned long upper_bound; +unsigned long start_area; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node); +BUG_ON (!rwsem_is_locked (&node->full_lock)); +BUG_ON (!rwsem_is_locked (&node->bmap_array.lock)); + +SSDFS_DBG ("start_index %u, count %u\n" , + start_index, count); +#endif /* CONFIG_SSDFS_DEBUG */ + + bmap = &node->bmap_array.bmap[SSDFS_BTREE_NODE_LOCK_BMAP ]; + start_area = node->bmap_array.index_start_bit; + upper_bound = start_area + start_index + count; +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!bmap->ptr); +BUG_ON (start_area ==ULONG_MAX ); +BUG_ON (upper_bound > node->bmap_array.item_start_bit); +#endif /* CONFIG_SSDFS_DEBUG */ + +spin_lock (&bmap->lock); +bitmap_clear (bmap->ptr, start_area + start_index, count); +spin_unlock (&bmap->lock); +} + +/* +* ssdfs_unlock_index_range() - unlock an index range +* @node: node object +* @start_index: starting index +* @count: count of indexes in the range +* +* This method tries to unlock an index range. +*/ +static inline +void ssdfs_unlock_index_range (struct ssdfs_btree_node *node, +u16 start_index,u16 count) +{ +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node); +BUG_ON (!rwsem_is_locked (&node->full_lock)); + +SSDFS_DBG ("start_index %u, count %u\n" , + start_index, count); +#endif /* CONFIG_SSDFS_DEBUG */ + +down_read (&node->bmap_array.lock); +__ssdfs_unlock_index_range (node, start_index, count); +up_read (&node->bmap_array.lock); +wake_up_all (&node->wait_queue); +} + +/* +* ssdfs_unlock_whole_index_area() - unlock the whole index area +* @node: node object +* @start_index: starting index +* @count: count of indexes in the range +* +* This method tries to unlock the whole index area. +*/ +void ssdfs_unlock_whole_index_area (struct ssdfs_btree_node *node) +{ +unsigned long start, count; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node); +BUG_ON (!rwsem_is_locked (&node->full_lock)); + +SSDFS_DBG ("node_id %u\n" , node->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ + +down_read (&node->bmap_array.lock); + start = node->bmap_array.index_start_bit; + count = node->bmap_array.item_start_bit - start; +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (start >=U16_MAX ); +BUG_ON (count >=U16_MAX ); +#endif /* CONFIG_SSDFS_DEBUG */ +__ssdfs_unlock_index_range (node,0 , (u16 )count); +up_read (&node->bmap_array.lock); +wake_up_all (&node->wait_queue);
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.34.1
Part of the diff is not shown here. View the raw message.
RFC PATCH 52/76 ssdfs: b-tree node index operations
In reply to Viacheslav DubeykoEvery b-tree node is described by index record (or key) that
includes: (1) node ID, (2) node type, (3) node height,
(4) starting hash value, (5) raw extent. The raw extent describes the segment ID, logical block ID, and length. Index records are stored into index and hybrid b-tree nodes. These records implement mechanism of lookup and traverse operations in b-tree.
Index operations include:
(1) resize_index_area - operation of increasing size of index area in hybrid b-tree node by means of redistribution of free space between index and item area and shift of item area
(2) find_index - find index in hybrid or index b-tree node
(3) add_index - add index in hybrid of index b-tree node
(4) change_index - change index record in hybrid of index b-tree node
(5) delete_index - delete index record from hybrid of index b-tree node
--- fs/ssdfs/btree_node.c | 2985 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 2985 insertions(+)
fs/ssdfs/btree_node.c +2985 -0
@@ -5222,3 +5222,2988 @@ bool is_ssdfs_node_shared(struct ssdfs_btree_node *node)return atomic_read (&node->refs_count) >1 ; } + +/* +* ssdfs_btree_root_node_find_index() - find index record in root node +* @node: node object +* search_hash: hash for search in the index area +* @found_index: identification number of found index [out] +* +* This method tries to find the index record for the requested hash. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ENODATA - unable to find the node's index. +* %-EEXIST - search hash has been found. +*/ +static +int ssdfs_btree_root_node_find_index (struct ssdfs_btree_node *node, +u64 search_hash, +u16 *found_index) +{ +int i; +int err = -ENODATA ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !found_index); +BUG_ON (!rwsem_is_locked (&node->header_lock)); + +SSDFS_DBG ("node_id %u, node_type %#x, search_hash %llx\n" , + node->node_id,atomic_read (&node->type), + search_hash); +#endif /* CONFIG_SSDFS_DEBUG */ + + *found_index =U16_MAX ; + +for (i =0 ; i <SSDFS_BTREE_ROOT_NODE_INDEX_COUNT ; i++) { +struct ssdfs_btree_index *ptr = &node->raw.root_node.indexes[i]; +u64 hash =le64_to_cpu (ptr->hash); + +if (hash ==U64_MAX ) +break ; + +if (search_hash < hash) +break ; + + err =0 ; + *found_index = i; + +if (search_hash == hash) { + err = -EEXIST ; +break ; + } + } + +return err; +} + +#define CUR_INDEX (kaddr, page_off, index) \ + ((struct ssdfs_btree_index_key *)((u8 *)kaddr + \ + page_off + (index * sizeof(struct ssdfs_btree_index_key)))) + +/* +* ssdfs_get_index_key_hash() - get hash from a range +* @node: node object +* @kaddr: pointer on starting address in the page +* @page_off: offset from page's beginning in bytes +* @index: requested starting index in the range +* @upper_index: last index in the available range +* @hash_index: available (not locked) index in the range [out] +* @hash: hash value of found index [out] +* +* This method tries to find any unlocked index in suggested +* range. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ENODATA - unable to find the node's index. +*/ +static +int ssdfs_get_index_key_hash (struct ssdfs_btree_node *node, +void *kaddr,u32 page_off, +u32 index,u32 upper_index, +u32 *hash_index,u64 *hash) +{ +struct ssdfs_btree_index_key *ptr; +int err = -ENODATA ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !kaddr || !hash_index || !hash); +BUG_ON (!rwsem_is_locked (&node->full_lock)); + +SSDFS_DBG ("kaddr %p, page_off %u, " +"index %u, upper_index %u\n" , + kaddr, page_off, index, upper_index); +#endif /* CONFIG_SSDFS_DEBUG */ + + *hash =U64_MAX ; + +for (*hash_index = index; *hash_index <= upper_index; ++(*hash_index)) { + err =ssdfs_lock_index_range (node, *hash_index,1 ); +if (unlikely (err)) { +SSDFS_ERR ("fail to lock index %u, err %d\n" , + *hash_index, err); +break ; + } + + err = -EEXIST ; + ptr =CUR_INDEX (kaddr, page_off, *hash_index); + *hash =le64_to_cpu (ptr->index.hash); + +ssdfs_unlock_index_range (node, *hash_index,1 ); + +if (err == -EEXIST ) { + err =0 ; +break ; + }else if (err == -ENODATA ) +continue ; +else if (unlikely (err)) +break ; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("hash_index %u, hash %llx\n" , + *hash_index, *hash); +#endif /* CONFIG_SSDFS_DEBUG */ + +return err; +} + +/* +* ssdfs_check_last_index() - check last index in the search +* @node: node object +* @kaddr: pointer on starting address in the page +* @page_off: offset from page's beginning in bytes +* @index: requested index for the check +* @search_hash: hash for search +* @range_start: first index in the index area +* @range_end: last index in the index area +* @prev_found: processed index on previous iteration +* @found_index: value of found index [out] +* +* This method tries to check the index for the case when +* range has only one index. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +* %-ENOENT - requested hash is located in previous range. +* %-ENODATA - unable to find the node's index. +*/ +static +int ssdfs_check_last_index (struct ssdfs_btree_node *node, +void *kaddr,u32 page_off, +u32 index,u64 search_hash, +u32 range_start,u32 range_end, +u32 prev_found,u16 *found_index) +{ +u32 hash_index; +u64 hash; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !kaddr || !found_index); +BUG_ON (!rwsem_is_locked (&node->full_lock)); + +SSDFS_DBG ("kaddr %p, page_off %u, " +"index %u, search_hash %llx, " +"range_start %u, range_end %u, " +"prev_found %u\n" , + kaddr, page_off, index, search_hash, + range_start, range_end, prev_found); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_get_index_key_hash (node, kaddr, page_off, + index, index, + &hash_index, &hash); +if (unlikely (err)) { +SSDFS_ERR ("fail to get hash: " +"index %u, err %d\n" , + index, err); +return err; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("hash_index %u, hash %llx\n" , + hash_index, hash); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (hash_index != index) { +SSDFS_ERR ("hash_index %u != index %u\n" , + hash_index, index); +return -ERANGE ; + } + +if (search_hash < hash) { + err = -ENOENT ; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to find index: " +"index %u, search_hash %llx, " +"hash %llx\n" , + hash_index, search_hash, + hash); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (prev_found <U16_MAX ) + *found_index = prev_found; +else + *found_index = hash_index; + }else if (search_hash == hash) { + err =0 ; + *found_index = hash_index; + }else { + err = -ENODATA ; + *found_index = hash_index; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("prev_found %u, found_index %u\n" , + prev_found, *found_index); +#endif /* CONFIG_SSDFS_DEBUG */ + +return err; +} + +/* +* ssdfs_check_last_index_pair() - check last pair of indexes in the search +* @node: node object +* @kaddr: pointer on starting address in the page +* @page_off: offset from page's beginning in bytes +* @lower_index: starting index in the search +* @upper_index: ending index in the search +* @search_hash: hash for search +* @range_start: first index in the index area +* @range_end: last index in the index area +* @prev_found: processed index on previous iteration +* @found_index: value of found index [out] +* +* This method tries to find an index for the case when +* range has only two indexes. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +* %-ENOENT - requested hash is located in previous range. +* %-ENODATA - unable to find the node's index. +*/ +static +int ssdfs_check_last_index_pair (struct ssdfs_btree_node *node, +void *kaddr,u32 page_off, +u32 lower_index,u32 upper_index, +u64 search_hash, +u32 range_start,u32 range_end, +u32 prev_found,u16 *found_index) +{ +u32 hash_index; +u64 hash; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !kaddr || !found_index); +BUG_ON (!rwsem_is_locked (&node->full_lock)); + +SSDFS_DBG ("kaddr %p, page_off %u, " +"lower_index %u, upper_index %u, " +"search_hash %llx, range_start %u, prev_found %u\n" , + kaddr, page_off, lower_index, upper_index, + search_hash, range_start, prev_found); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_get_index_key_hash (node, kaddr, page_off, + lower_index, upper_index, + &hash_index, &hash); +if (unlikely (err)) { +SSDFS_ERR ("fail to get hash: " +"lower_index %u, upper_index %u, err %d\n" , + lower_index, upper_index, err); +return err; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("hash_index %u, hash %llx\n" , + hash_index, hash); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (hash_index == lower_index) { +if (search_hash < hash) { + err = -ENOENT ; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to find index: " +"index %u, search_hash %llx, " +"hash %llx\n" , + hash_index, search_hash, + hash); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (prev_found <U16_MAX ) + *found_index = prev_found; +else + *found_index = hash_index; + }else if (search_hash == hash) { + err =0 ; + *found_index = hash_index; + }else { + prev_found = hash_index; + err =ssdfs_check_last_index (node, kaddr, page_off, + upper_index, search_hash, + range_start, range_end, + prev_found, found_index); +if (err == -ENOENT ) { + err =0 ; + *found_index = prev_found; + } + } + }else if (hash_index == upper_index) { +if (search_hash > hash) { + err = -ENODATA ; + *found_index = upper_index; + }else if (search_hash == hash) { + err =0 ; + *found_index = upper_index; + }else { + prev_found = hash_index; + err =ssdfs_check_last_index (node, kaddr, page_off, + lower_index, search_hash, + range_start, range_end, + prev_found, found_index); +if (err == -ENOENT ) { + err =0 ; + *found_index = prev_found; + } + } + }else { +SSDFS_ERR ("invalid index: hash_index %u, " +"lower_index %u, upper_index %u\n" , + hash_index, lower_index, upper_index); +return -ERANGE ; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("prev_found %u, found_index %u, err %d\n" , + prev_found, *found_index, err); +#endif /* CONFIG_SSDFS_DEBUG */ + +return err; +} + +/* +* ssdfs_find_index_in_memory_page() - find index record in memory page +* @node: node object +* @area: description of index area +* @start_offset: offset in the index area of the node +* search_hash: hash for search in the index area +* @found_index: identification number of found index [out] +* @processed_bytes: amount of processed bytes into index area [out] +* +* This method tries to find the index record for the requested hash. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +* %-ENODATA - unable to find the node's index. +* %-ENOENT - index record is outside of this memory page. +*/ +static +int ssdfs_find_index_in_memory_page (struct ssdfs_btree_node *node, +struct ssdfs_btree_node_index_area *area, +u32 start_offset, +u64 search_hash, +u16 *found_index, +u32 *processed_bytes) +{ +struct page *page; +void *kaddr; +u32 page_index; +u32 page_off; +u32 search_bytes; +u32 index_count; +u32 cur_index, upper_index, lower_index; +u32 range_start, range_end; +u32 prev_found; +u64 hash; +u32 processed_indexes =0 ; +int err = -ENODATA ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !area || !processed_bytes || !found_index); +BUG_ON (!rwsem_is_locked (&node->full_lock)); + +SSDFS_DBG ("node_id %u, node_type %#x, " +"start_offset %u, search_hash %llx\n" , + node->node_id,atomic_read (&node->type), + start_offset, search_hash); +#endif /* CONFIG_SSDFS_DEBUG */ + + *found_index =U16_MAX ; + *processed_bytes =0 ; + +if (start_offset >= (area->offset + area->area_size)) { +SSDFS_ERR ("invalid start_offset: " +"offset %u, area_start %u, area_size %u\n" , + start_offset, area->offset, area->area_size); +return -ERANGE ; + } + +if (area->index_size !=sizeof (struct ssdfs_btree_index_key )) { +SSDFS_ERR ("invalid index size %u\n" , + area->index_size); +return -ERANGE ; + } + + page_index = start_offset >>PAGE_SHIFT ; + page_off = start_offset %PAGE_SIZE ; + +if ((page_off + area->index_size) >PAGE_SIZE ) { +SSDFS_ERR ("invalid offset into the page: " +"offset %u, index_size %u\n" , + page_off, area->index_size); +return -ERANGE ; + } + +if (page_index ==0 && page_off < area->offset) { +SSDFS_ERR ("page_off %u < area->offset %u\n" , + page_off, area->offset); +return -ERANGE ; + } + +if (page_off % area->index_size) { +SSDFS_ERR ("offset is not aligned: " +"page_off %u, index_size %u\n" , + page_off, area->index_size); +return -ERANGE ; + } + +if (page_index >=pagevec_count (&node->content.pvec)) { +SSDFS_ERR ("invalid page index: " +"page_index %u, pagevec_count %u\n" , + page_index, +pagevec_count (&node->content.pvec)); +return -ERANGE ; + } + + search_bytes =PAGE_SIZE - page_off; + search_bytes =min_t (u32, search_bytes, + (area->offset + area->area_size) - start_offset); + + index_count = search_bytes / area->index_size; +if (index_count ==0 ) { +SSDFS_ERR ("invalid index_count %u\n" , + index_count); +return -ERANGE ; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("search_bytes %u, offset %u, area_size %u\n" , + search_bytes, area->offset, area->area_size); +#endif /* CONFIG_SSDFS_DEBUG */ + + processed_indexes = (start_offset - area->offset); + processed_indexes /= area->index_size; + +if (processed_indexes >= area->index_capacity) { +SSDFS_ERR ("processed_indexes %u >= area->index_capacity %u\n" , + processed_indexes, + area->index_capacity); +return -ERANGE ; + }else if (processed_indexes >= area->index_count) { + err = -ENOENT ; + *processed_bytes = search_bytes; +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to find an index: " +"processed_indexes %u, area->index_count %u\n" , + processed_indexes, + area->index_count); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ENOENT ; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("area->index_count %u, area->index_capacity %u\n" , + area->index_count, area->index_capacity); +#endif /* CONFIG_SSDFS_DEBUG */ + + index_count =min_t (u32, index_count, + area->index_count - processed_indexes); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("area->index_count %u, processed_indexes %u, " +"index_count %u\n" , + area->index_count, processed_indexes, index_count); +#endif /* CONFIG_SSDFS_DEBUG */ + + cur_index =0 ; + range_start = lower_index =0 ; + range_end = upper_index = index_count -1 ; + + page = node->content.pvec.pages[page_index]; + kaddr =kmap_local_page (page); + + prev_found = *found_index; +while (lower_index <= upper_index) { +int diff = upper_index - lower_index; +u32 hash_index; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("lower_index %u, upper_index %u, diff %d\n" , + lower_index, upper_index, diff); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (diff <0 ) { + err = -ERANGE ; +SSDFS_ERR ("invalid diff: " +"diff %d, lower_index %u, " +"upper_index %u\n" , + diff, lower_index, upper_index); + goto finish_search; + } + +if (diff ==0 ) { + err =ssdfs_check_last_index (node, kaddr, page_off, + lower_index, search_hash, + range_start, range_end, + prev_found, found_index); +if (err == -ENOENT ) { +if (prev_found <U16_MAX ) + *found_index = prev_found; + }else if (err == -ENODATA ) { +/* +* Nothing was found +*/ + }else if (unlikely (err)) { +SSDFS_ERR ("fail to check the last index: " +"index %u, err %d\n" , + lower_index, err); + } + + *processed_bytes = search_bytes; + goto finish_search; + }else if (diff ==1 ) { + err =ssdfs_check_last_index_pair (node, kaddr, + page_off, + lower_index, + upper_index, + search_hash, + range_start, + range_end, + prev_found, + found_index); +if (err == -ENOENT ) { +if (prev_found <U16_MAX ) + *found_index = prev_found; + }else if (err == -ENODATA ) { +/* +* Nothing was found +*/ + }else if (unlikely (err)) { +SSDFS_ERR ("fail to check the last index pair: " +"lower_index %u, upper_index %u, " +"err %d\n" , + lower_index, upper_index, err); + } + + *processed_bytes = search_bytes; + goto finish_search; + }else + cur_index = lower_index + (diff /2 ); + + + err =ssdfs_get_index_key_hash (node, kaddr, page_off, + cur_index, upper_index, + &hash_index, &hash); +if (unlikely (err)) { +SSDFS_WARN ("fail to get hash: " +"cur_index %u, upper_index %u, err %d\n" , + cur_index, upper_index, err); + goto finish_search; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("search_hash %llx, hash %llx, " +"hash_index %u, range_start %u, range_end %u\n" , + search_hash, hash, hash_index, + range_start, range_end); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (search_hash < hash) { +if (hash_index == range_start) { + err = -ENOENT ; + *found_index = hash_index; +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to find index: " +"index %u, search_hash %llx, " +"hash %llx\n" , + hash_index, search_hash, + hash); +#endif /* CONFIG_SSDFS_DEBUG */ + goto finish_search; + }else { + prev_found = lower_index; + upper_index = cur_index; + } + }else if (search_hash == hash) { + err = -EEXIST ; + *found_index = cur_index; + *processed_bytes = search_bytes; + goto finish_search; + }else { +if (hash_index == range_end) { + err = -ENODATA ; + *found_index = hash_index; +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to find index: " +"index %u, search_hash %llx, " +"hash %llx\n" , + hash_index, search_hash, + hash); +#endif /* CONFIG_SSDFS_DEBUG */ + goto finish_search; + }else { + prev_found = lower_index; + lower_index = cur_index; + } + } + }; + +finish_search: +kunmap_local (kaddr); + +if (!err || err == -EEXIST ) { + *found_index += processed_indexes; +if (*found_index >= area->index_capacity) { +SSDFS_ERR ("found_index %u >= capacity %u\n" , + *found_index, + area->index_capacity); +return -ERANGE ; + } + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("prev_found %u, found_index %u\n" , + prev_found, *found_index); +#endif /* CONFIG_SSDFS_DEBUG */ + +return err; +} + +/* +* ssdfs_btree_common_node_find_index() - find index record +* @node: node object +* @area: description of index area +* search_hash: hash for search in the index area +* @found_index: identification number of found index [out] +* +* This method tries to find the index record for the requested hash. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +* %-ENODATA - unable to find the node's index. +* %-EEXIST - search hash has been found. +*/ +static +int ssdfs_btree_common_node_find_index (struct ssdfs_btree_node *node, +struct ssdfs_btree_node_index_area *area, +u64 search_hash, +u16 *found_index) +{ +u32 start_offset, end_offset; +u32 processed_bytes =0 ; +u16 prev_found =U16_MAX ; +int err = -ENODATA ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !area || !found_index); +BUG_ON (!rwsem_is_locked (&node->full_lock)); + +SSDFS_DBG ("node_id %u, node_type %#x, search_hash %llx\n" , + node->node_id,atomic_read (&node->type), + search_hash); +#endif /* CONFIG_SSDFS_DEBUG */ + + *found_index =U16_MAX ; + +if (atomic_read (&area->state) !=SSDFS_BTREE_NODE_INDEX_AREA_EXIST ) { +SSDFS_ERR ("invalid area state %#x\n" , +atomic_read (&area->state)); +return -ERANGE ; + } + +if (area->index_count ==0 ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node %u hasn't any index\n" , + node->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ENODATA ; + } + +if (area->index_count > area->index_capacity) { +SSDFS_ERR ("invalid area: " +"index_count %u, index_capacity %u\n" , + area->index_count, + area->index_capacity); +return -ERANGE ; + } + +if ((area->offset + area->area_size) > node->node_size) { +SSDFS_ERR ("invalid area: " +"offset %u, area_size %u, node_size %u\n" , + area->offset, + area->area_size, + node->node_size); +return -ERANGE ; + } + +if (area->index_size !=sizeof (struct ssdfs_btree_index_key )) { +SSDFS_ERR ("invalid index size %u\n" , + area->index_size); +return -ERANGE ; + } + + start_offset = area->offset; + end_offset = area->offset + area->area_size; + +while (start_offset < end_offset) { + prev_found = *found_index; + err =ssdfs_find_index_in_memory_page (node, area, + start_offset, + search_hash, + found_index, + &processed_bytes); +if (err == -ENODATA ) { + err =0 ; + +if (*found_index >=U16_MAX ) { +/* +* continue to search +*/ + }else if ((*found_index +1 ) >= area->index_count) { +/* +* index has been found +*/ +break ; + } + }else if (err == -ENOENT ) { + err =0 ; + +if (prev_found !=U16_MAX ) { + err =0 ; + *found_index = prev_found; +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node_id %u, search_hash %llx, " +"found_index %u\n" , + node->node_id, search_hash, + *found_index); +#endif /* CONFIG_SSDFS_DEBUG */ + } +break ; + }else if (err == -EEXIST ) { +/* +* index has been found +*/ +break ; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find index: err %d\n" , + err); +break ; + }else +break ; + + start_offset += processed_bytes; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("prev_found %u, found_index %u, err %d\n" , + prev_found, *found_index, err); +#endif /* CONFIG_SSDFS_DEBUG */ + +return err; +} + +/* +* __ssdfs_btree_root_node_extract_index() - extract index from root node +* @node: node object +* @found_index: identification number of found index +* @search: btree search object [out] +* +* This method tries to extract index record from the index area. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +*/ +int __ssdfs_btree_root_node_extract_index (struct ssdfs_btree_node *node, +u16 found_index, +struct ssdfs_btree_index_key *ptr) +{ +size_t index_size =sizeof (struct ssdfs_btree_index ); +__le32 node_id; +int node_height; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !ptr); +BUG_ON (!rwsem_is_locked (&node->full_lock)); +BUG_ON (!rwsem_is_locked (&node->tree->lock)); + +SSDFS_DBG ("node_id %u, node_type %#x, found_index %u\n" , + node->node_id,atomic_read (&node->type), + found_index); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (found_index >=SSDFS_BTREE_ROOT_NODE_INDEX_COUNT ) { +SSDFS_ERR ("invalid found_index %u\n" , + found_index); +return -ERANGE ; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("index 0: node_id %u; index 1: node_id %u\n" , +cpu_to_le32 (node->raw.root_node.header.node_ids[0 ]), +cpu_to_le32 (node->raw.root_node.header.node_ids[1 ])); +#endif /* CONFIG_SSDFS_DEBUG */ + +down_read (&node->header_lock); + node_id = node->raw.root_node.header.node_ids[found_index]; + ptr->node_id =cpu_to_le32 (node_id); +ssdfs_memcpy (&ptr->index,0 , index_size, + &node->raw.root_node.indexes[found_index],0 , index_size, + index_size); +up_read (&node->header_lock); + + node_height =atomic_read (&node->height); +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (node_height <0 ); +#endif /* CONFIG_SSDFS_DEBUG */ + ptr->height = node_height -1 ; + +switch (node_height) { +case SSDFS_BTREE_LEAF_NODE_HEIGHT : +case SSDFS_BTREE_PARENT2LEAF_HEIGHT : + ptr->node_type =SSDFS_BTREE_LEAF_NODE ; +break ; + +case SSDFS_BTREE_PARENT2HYBRID_HEIGHT : + ptr->node_type =SSDFS_BTREE_HYBRID_NODE ; +break ; + +default : + ptr->node_type =SSDFS_BTREE_INDEX_NODE ; +break ; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node_height %u, node_type %#x\n" , + node_height, ptr->node_type); +#endif /* CONFIG_SSDFS_DEBUG */ + + ptr->flags =cpu_to_le16 (SSDFS_BTREE_INDEX_HAS_VALID_EXTENT ); + +return 0 ; +} + +/* +* ssdfs_btree_root_node_extract_index() - extract index from root node +* @node: node object +* @found_index: identification number of found index +* @search: btree search object [out] +* +* This method tries to extract index record from the index area. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +*/ +static inline +int ssdfs_btree_root_node_extract_index (struct ssdfs_btree_node *node, +u16 found_index, +struct ssdfs_btree_search *search) +{ +struct ssdfs_btree_index_key *ptr; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !search); +BUG_ON (!rwsem_is_locked (&node->full_lock)); +BUG_ON (!rwsem_is_locked (&node->tree->lock)); + +SSDFS_DBG ("node_id %u, node_type %#x, found_index %u\n" , + node->node_id,atomic_read (&node->type), + found_index); +#endif /* CONFIG_SSDFS_DEBUG */ + + ptr = &search->node.found_index; +return __ssdfs_btree_root_node_extract_index (node, found_index, ptr); +} + +/* +* ssdfs_btree_node_get_index() - extract index from node +* @pvec: pagevec object +* @area_offset: area offset from the node's beginning +* @area_size: size of the area +* @node_size: node size in bytes +* @position: position of index record in the node +* @ptr: pointer on index buffer [out] +* +* This method tries to extract index record from the index area. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +*/ +int ssdfs_btree_node_get_index (struct pagevec *pvec, +u32 area_offset,u32 area_size, +u32 node_size,u16 position, +struct ssdfs_btree_index_key *ptr) +{ +size_t index_size =sizeof (struct ssdfs_btree_index_key ); +struct page *page; +u32 page_index; +u32 page_off; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!pvec || !ptr); + +SSDFS_DBG ("area_offset %u, area_size %u, " +"node_size %u, position %u\n" , + area_offset, area_size, + node_size, position); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =__ssdfs_define_memory_page (area_offset, area_size, + node_size, index_size, position, + &page_index, &page_off); +if (unlikely (err)) { +SSDFS_ERR ("fail to define memory page: " +"position %u, err %d\n" , + position, err); +return err; + } + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (page_index >=U32_MAX ); +BUG_ON (page_off >=U32_MAX ); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (page_index >=pagevec_count (pvec)) { +SSDFS_ERR ("page_index %u > pvec_size %u\n" , + page_index, +pagevec_count (pvec)); +return -ERANGE ; + } + + page = pvec->pages[page_index]; + err =ssdfs_memcpy_from_page (ptr,0 , index_size, + page, page_off,PAGE_SIZE , + index_size); +if (unlikely (err)) { +SSDFS_ERR ("invalid page_off %u, err %d\n" , + page_off, err); +return err; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node_id %u, node_type %#x, hash %llx, " +"seg_id %llu, logical_blk %u, len %u\n" , +le32_to_cpu (ptr->node_id), + ptr->node_type, +le64_to_cpu (ptr->index.hash), +le64_to_cpu (ptr->index.extent.seg_id), +le32_to_cpu (ptr->index.extent.logical_blk), +le32_to_cpu (ptr->index.extent.len)); +#endif /* CONFIG_SSDFS_DEBUG */ + +return 0 ; +} + +/* +* __ssdfs_btree_common_node_extract_index() - extract index from node +* @node: node object +* @area: description of index area +* @found_index: identification number of found index +* @search: btree search object [out] +* +* This method tries to extract index record from the index area. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +*/ +int __ssdfs_btree_common_node_extract_index (struct ssdfs_btree_node *node, +struct ssdfs_btree_node_index_area *area, +u16 found_index, +struct ssdfs_btree_index_key *ptr) +{ +struct page *page; +size_t index_key_len =sizeof (struct ssdfs_btree_index_key ); +u32 page_index; +u32 page_off; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !area || !ptr); +BUG_ON (!rwsem_is_locked (&node->full_lock)); +BUG_ON (!rwsem_is_locked (&node->tree->lock)); + +SSDFS_DBG ("node_id %u, node_type %#x, found_index %u\n" , + node->node_id,atomic_read (&node->type), + found_index); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (found_index == area->index_count) { +SSDFS_ERR ("found_index %u == index_count %u\n" , + found_index, area->index_count); +return -ERANGE ; + } + + err =ssdfs_define_memory_page (node, area, found_index, + &page_index, &page_off); +if (unlikely (err)) { +SSDFS_ERR ("fail to define memory page: " +"node_id %u, found_index %u, err %d\n" , + node->node_id, found_index, err); +return err; + } + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (page_index >=U32_MAX ); +BUG_ON (page_off >=U32_MAX ); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (page_index >=pagevec_count (&node->content.pvec)) { +SSDFS_ERR ("page_index %u > pvec_size %u\n" , + page_index, +pagevec_count (&node->content.pvec)); +return -ERANGE ; + } + + page = node->content.pvec.pages[page_index]; + err =ssdfs_memcpy_from_page (ptr,0 , index_key_len, + page, page_off,PAGE_SIZE , + index_key_len); +if (unlikely (err)) { +SSDFS_ERR ("fail to copy: err %d\n" , err); +return err; + } + +if (ptr->node_type <=SSDFS_BTREE_NODE_UNKNOWN_TYPE || + ptr->node_type >=SSDFS_BTREE_NODE_TYPE_MAX ) { +SSDFS_WARN ("node_id %u, node_type %#x, found_index %u\n" , + node->node_id,atomic_read (&node->type), + found_index); +SSDFS_ERR ("page_index %u, page_off %u\n" , + page_index, page_off); +SSDFS_ERR ("FOUND_INDEX: node_id %u, node_type %#x, " +"height %u, flags %#x, hash %llx, " +"seg_id %llu, logical_blk %u, len %u\n" , +le32_to_cpu (ptr->node_id), + ptr->node_type, ptr->height, ptr->flags, +le64_to_cpu (ptr->index.hash), +le64_to_cpu (ptr->index.extent.seg_id), +le32_to_cpu (ptr->index.extent.logical_blk), +le32_to_cpu (ptr->index.extent.len)); +return -ERANGE ; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("FOUND_INDEX: node_id %u, node_type %#x, " +"height %u, flags %#x, hash %llx, " +"seg_id %llu, logical_blk %u, len %u\n" , +le32_to_cpu (ptr->node_id), + ptr->node_type, ptr->height, ptr->flags, +le64_to_cpu (ptr->index.hash), +le64_to_cpu (ptr->index.extent.seg_id), +le32_to_cpu (ptr->index.extent.logical_blk), +le32_to_cpu (ptr->index.extent.len)); +#endif /* CONFIG_SSDFS_DEBUG */ + +return 0 ; +} + +/* +* ssdfs_btree_common_node_extract_index() - extract index from node +* @node: node object +* @area: description of index area +* @found_index: identification number of found index +* @search: btree search object [out] +* +* This method tries to extract index record from the index area. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +*/ +static inline +int ssdfs_btree_common_node_extract_index (struct ssdfs_btree_node *node, +struct ssdfs_btree_node_index_area *area, +u16 found_index, +struct ssdfs_btree_search *search) +{ +struct ssdfs_btree_index_key *ptr; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !area || !search); +BUG_ON (!rwsem_is_locked (&node->full_lock)); +BUG_ON (!rwsem_is_locked (&node->tree->lock)); + +SSDFS_DBG ("node_id %u, node_type %#x, found_index %u\n" , + node->node_id,atomic_read (&node->type), + found_index); +#endif /* CONFIG_SSDFS_DEBUG */ + + ptr = &search->node.found_index; +return __ssdfs_btree_common_node_extract_index (node, area, + found_index, ptr); +} + +/* +* ssdfs_find_index_by_hash() - find index record in the node by hash +* @node: node object +* @area: description of index area +* @hash: hash value for the search +* @found_index: found position of index record in the node [out] +* +* This method tries to find node's index for +* the requested hash value. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +* %-ENODATA - unable to find the node's index. +* %-EEXIST - search hash has been found. +*/ +int ssdfs_find_index_by_hash (struct ssdfs_btree_node *node, +struct ssdfs_btree_node_index_area *area, +u64 hash, +u16 *found_index) +{ +int node_type; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !area || !found_index); + +SSDFS_DBG ("node_id %u, hash %llx, " +"area->start_hash %llx, area->end_hash %llx\n" , + node->node_id, hash, + area->start_hash, area->end_hash); +#endif /* CONFIG_SSDFS_DEBUG */ + + *found_index =U16_MAX ; + + node_type =atomic_read (&node->type); +if (node_type <=SSDFS_BTREE_NODE_UNKNOWN_TYPE || + node_type >=SSDFS_BTREE_NODE_TYPE_MAX ) { +SSDFS_ERR ("invalid node type %#x\n" , + node_type); +return -ERANGE ; + } + +if (atomic_read (&area->state) !=SSDFS_BTREE_NODE_INDEX_AREA_EXIST ) { +SSDFS_ERR ("index area hasn't been created: " +"node_id %u, node_type %#x\n" , + node->node_id, +atomic_read (&node->type)); +return -ERANGE ; + } + +if (area->index_count ==0 ) { + *found_index =0 ; +SSDFS_DBG ("index area is empty\n" ); +return -ENODATA ; + } + +if (area->index_count > area->index_capacity) { +SSDFS_ERR ("index_count %u > index_capacity %u\n" , + area->index_count, + area->index_capacity); +return -ERANGE ; + } + +if (area->start_hash ==U64_MAX ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("start_hash is invalid: node_id %u\n" , + node->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ENODATA ; + } + +#ifdef CONFIG_SSDFS_DEBUG +if (hash ==U64_MAX ) { +SSDFS_ERR ("invalid requested hash\n" ); +return -ERANGE ; + } +#endif /* CONFIG_SSDFS_DEBUG */ + +if (hash < area->start_hash) { + err =0 ; + *found_index =0 ; + goto finish_hash_search; + } + +if (area->end_hash ==U64_MAX ) + *found_index =0 ; +else if (hash >= area->end_hash) { + *found_index = area->index_count -1 ; + }else { +if (node_type ==SSDFS_BTREE_ROOT_NODE ) { + err =ssdfs_btree_root_node_find_index (node, hash, + found_index); +if (err == -ENODATA ) { +SSDFS_DBG ("unable to find index\n" ); + goto finish_hash_search; + }else if (err == -EEXIST ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("index exists already: " +"hash %llx, index %u\n" , + hash, *found_index); +#endif /* CONFIG_SSDFS_DEBUG */ + goto finish_hash_search; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find index in root node: " +"err %d\n" , + err); + goto finish_hash_search; + }else if (*found_index ==U16_MAX ) { + err = -ERANGE ; +SSDFS_ERR ("invalid index was found\n" ); + goto finish_hash_search; + } + }else { + err =ssdfs_btree_common_node_find_index (node, area, + hash, + found_index); +if (err == -ENODATA ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to find index: " +"node_id %u\n" , + node->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ + goto finish_hash_search; + }else if (err == -EEXIST ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("index exists already: " +"hash %llx, index %u\n" , + hash, *found_index); +#endif /* CONFIG_SSDFS_DEBUG */ + goto finish_hash_search; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find index in the node: " +"node_id %u, err %d\n" , + node->node_id, err); + goto finish_hash_search; + }else if (*found_index ==U16_MAX ) { + err = -ERANGE ; +SSDFS_ERR ("invalid index was found\n" ); + goto finish_hash_search; + } + } + } + +finish_hash_search: +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("hash %llx, found_index %u, err %d\n" , + hash, *found_index, err); +#endif /* CONFIG_SSDFS_DEBUG */ + +return err; +} + +/* +* ssdfs_btree_node_find_index_position() - find index's position +* @node: node object +* @hash: hash value +* @found_position: pointer on returned value [out] +* +* This method tries to find node's index for +* the requested hash value. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +* %-ENODATA - unable to find the node's index. +* %-ENOENT - node hasn't the index area. +* %-EACCES - node is under initialization yet. +*/ +int ssdfs_btree_node_find_index_position (struct ssdfs_btree_node *node, +u64 hash, +u16 *found_position) +{ +struct ssdfs_btree_node_index_area area; +size_t desc_size =sizeof (struct ssdfs_btree_node_index_area ); +int node_type; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !found_position); +BUG_ON (!node->tree); +BUG_ON (!rwsem_is_locked (&node->tree->lock)); + +SSDFS_DBG ("node_id %u, node_type %#x, hash %llx\n" , + node->node_id, +atomic_read (&node->type), + hash); +#endif /* CONFIG_SSDFS_DEBUG */ + +switch (atomic_read (&node->state)) { +case SSDFS_BTREE_NODE_CREATED : +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node %u is under initialization\n" , + node->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ +return -EACCES ; + +case SSDFS_BTREE_NODE_INITIALIZED : +case SSDFS_BTREE_NODE_DIRTY : +/* expected state */ +break ; + +default : +SSDFS_ERR ("invalid node state %#x\n" , +atomic_read (&node->state)); +return -ERANGE ; + } + +if (!is_ssdfs_btree_node_index_area_exist (node)) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node %u hasn't index area\n" , + node->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ENOENT ; + } + + *found_position =U16_MAX ; + +down_read (&node->full_lock); + + node_type =atomic_read (&node->type); +if (node_type <=SSDFS_BTREE_NODE_UNKNOWN_TYPE || + node_type >=SSDFS_BTREE_NODE_TYPE_MAX ) { + err = -ERANGE ; +SSDFS_ERR ("invalid node type %#x\n" , + node_type); + goto finish_index_search; + } + +down_read (&node->header_lock); +ssdfs_memcpy (&area,0 , desc_size, + &node->index_area,0 , desc_size, + desc_size); + err =ssdfs_find_index_by_hash (node, &area, hash, + found_position); +up_read (&node->header_lock); + +if (err == -EEXIST ) { +/* hash == found hash */ + err =0 ; + }else if (err == -ENODATA ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to find an index: " +"node_id %u, hash %llx\n" , + node->node_id, hash); +#endif /* CONFIG_SSDFS_DEBUG */ + goto finish_index_search; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find an index: " +"node_id %u, hash %llx, err %d\n" , + node->node_id, hash, err); + goto finish_index_search; + } + +finish_index_search: +up_read (&node->full_lock); + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (*found_position ==U16_MAX ); +#endif /* CONFIG_SSDFS_DEBUG */ + +return err; +} + +/* +* ssdfs_btree_node_find_index() - find node's index +* @search: btree search object +* +* This method tries to find node's index for +* the requested hash value. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +* %-ENODATA - unable to find the node's index. +* %-ENOENT - node hasn't the index area. +* %-EACCES - node is under initialization yet. +*/ +int ssdfs_btree_node_find_index (struct ssdfs_btree_search *search) +{ +struct ssdfs_btree_node *node; +struct ssdfs_btree_node_index_area area; +size_t desc_size =sizeof (struct ssdfs_btree_node_index_area ); +int tree_height; +int node_type; +u16 found_index =U16_MAX ; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!search); + +SSDFS_DBG ("type %#x, flags %#x, " +"start_hash %llx, end_hash %llx, " +"state %#x, node_id %u, height %u, " +"parent %p, child %p\n" , + search->request.type, search->request.flags, + search->request.start.hash, search->request.end.hash, + search->node.state, search->node.id, + search->node.height, search->node.parent, + search->node.child); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (search->node.parent) { + node = search->node.parent; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node->tree); +#endif /* CONFIG_SSDFS_DEBUG */ + + tree_height =atomic_read (&node->tree->height); +if (tree_height <= (SSDFS_BTREE_LEAF_NODE_HEIGHT +1 )) { +/* tree has only root node */ +return -ENODATA ; + } + } + + node = search->node.child; +if (!node) { +SSDFS_WARN ("child node is NULL\n" ); +return -ERANGE ; + } + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node->tree); +BUG_ON (!rwsem_is_locked (&node->tree->lock)); +#endif /* CONFIG_SSDFS_DEBUG */ + +switch (atomic_read (&node->state)) { +case SSDFS_BTREE_NODE_CREATED : +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node %u is under initialization\n" , + node->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ +return -EACCES ; + +case SSDFS_BTREE_NODE_INITIALIZED : +case SSDFS_BTREE_NODE_DIRTY : +/* expected state */ +break ; + +default : +SSDFS_ERR ("invalid node state %#x\n" , +atomic_read (&node->state)); +return -ERANGE ; + } + +if (!is_ssdfs_btree_node_index_area_exist (node)) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node %u hasn't index area\n" , + node->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ + + node = search->node.parent; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("try parent node %u\n" , + node->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ + } + +down_read (&node->full_lock); + + node_type =atomic_read (&node->type); +if (node_type <=SSDFS_BTREE_NODE_UNKNOWN_TYPE || + node_type >=SSDFS_BTREE_NODE_TYPE_MAX ) { + err = -ERANGE ; +SSDFS_ERR ("invalid node type %#x\n" , + node_type); + goto finish_index_search; + } + +down_read (&node->header_lock); +ssdfs_memcpy (&area,0 , desc_size, + &node->index_area,0 , desc_size, + desc_size); + err =ssdfs_find_index_by_hash (node, &area, + search->request.start.hash, + &found_index); +up_read (&node->header_lock); + +if (err == -EEXIST ) { +/* hash == found hash */ + err =0 ; + }else if (err == -ENODATA ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to find an index: " +"node_id %u, hash %llx\n" , + node->node_id, search->request.start.hash); +#endif /* CONFIG_SSDFS_DEBUG */ + goto finish_index_search; + }else if (unlikely (err)) { +SSDFS_WARN ("fail to find an index: " +"node_id %u, hash %llx, err %d\n" , + node->node_id, search->request.start.hash, + err); + goto finish_index_search; + } + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (found_index ==U16_MAX ); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (node_type ==SSDFS_BTREE_ROOT_NODE ) { + err =ssdfs_btree_root_node_extract_index (node, + found_index, + search); + }else { + err =ssdfs_btree_common_node_extract_index (node, &area, + found_index, + search); + } + +if (unlikely (err)) { +SSDFS_ERR ("fail to extract index: " +"node_id %u, node_type %#x, " +"found_index %u, err %d\n" , + node->node_id, node_type, + found_index, err); + goto finish_index_search; + } + +finish_index_search: +up_read (&node->full_lock); + +if (unlikely (err)) +ssdfs_debug_show_btree_node_indexes (node->tree, node); + +return err; +} + +/* +* can_add_new_index() - check that index area has free space +* @node: node object +*/ +bool can_add_new_index (struct ssdfs_btree_node *node) +{ +bool can_add = false; +u16 count, capacity; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node); + +SSDFS_DBG ("node_id %u\n" , node->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ + +down_read (&node->header_lock); + count = node->index_area.index_count; + capacity = node->index_area.index_capacity; +if (count > capacity) + err = -ERANGE ; +else + can_add = count < capacity; +up_read (&node->header_lock); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("count %u, capacity %u, can_add %#x, err %d\n" , + count, capacity, can_add, err); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (unlikely (err)) { +SSDFS_WARN ("count %u > capacity %u\n" , + count, capacity); +return false; + } + +return can_add; +} + +/* +* ssdfs_btree_root_node_add_index() - add index record into the root node +* @node: node object +* @position: position in the node for storing the new index record +* @ptr: pointer on storing index record [in] +* +* This method tries to add index record into the root node. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +* %-ENOSPC - root node hasn't free space. +* %-EEXIST - root node contains such record already. +*/ +static +int ssdfs_btree_root_node_add_index (struct ssdfs_btree_node *node, +u16 position, +struct ssdfs_btree_index_key *ptr) +{ +struct ssdfs_btree_index *found =NULL ; +size_t index_size =sizeof (struct ssdfs_btree_index ); +u64 hash1, hash2; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !ptr); +BUG_ON (!rwsem_is_locked (&node->full_lock)); +BUG_ON (!rwsem_is_locked (&node->header_lock)); + +SSDFS_DBG ("node_id %u, position %u\n" , + node->node_id, position); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (position >=SSDFS_BTREE_ROOT_NODE_INDEX_COUNT ) { +SSDFS_ERR ("invalid position %u\n" , + position); +return -ERANGE ; + } + +if (node->index_area.index_count > node->index_area.index_capacity) { +SSDFS_ERR ("index_count %u > index_capacity %u\n" , + node->index_area.index_count, + node->index_area.index_capacity); +return -ERANGE ; + } + +if (node->index_area.index_count == node->index_area.index_capacity) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to add the index: " +"index_count %u, index_capacity %u\n" , + node->index_area.index_count, + node->index_area.index_capacity); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ENOSPC ; + } + + found = &node->raw.root_node.indexes[position]; + + hash1 =le64_to_cpu (found->hash); + hash2 =le64_to_cpu (ptr->index.hash); + +if (hash1 == hash2) { +ssdfs_memcpy (&node->raw.root_node.indexes[position], +0 , index_size, + &ptr->index,0 , index_size, + index_size); + }else if (hash1 < hash2) { +ssdfs_memcpy (&node->raw.root_node.indexes[position +1 ], +0 , index_size, + &ptr->index,0 , index_size, + index_size); + position++; + node->index_area.index_count++; + }else { +void *indexes = node->raw.root_node.indexes; +u32 dst_off = (u32)(position +1 ) * index_size; +u32 src_off = (u32 )position * index_size; +u32 array_size = index_size *SSDFS_BTREE_ROOT_NODE_INDEX_COUNT ; +u32 move_size = (u32)(node->index_area.index_count - position) * + index_size; + + err =ssdfs_memmove (indexes, dst_off, array_size, + indexes, src_off, array_size, + move_size); +if (unlikely (err)) { +SSDFS_ERR ("fail to move: err %d\n" , err); +return err; + } + +ssdfs_memcpy (&node->raw.root_node.indexes[position], +0 , index_size, + &ptr->index,0 , index_size, + index_size); + node->index_area.index_count++; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node_id %u, node_type %#x, hash %llx, " +"seg_id %llu, logical_blk %u, len %u\n" , +le32_to_cpu (ptr->node_id), + ptr->node_type, +le64_to_cpu (ptr->index.hash), +le64_to_cpu (ptr->index.extent.seg_id), +le32_to_cpu (ptr->index.extent.logical_blk), +le32_to_cpu (ptr->index.extent.len)); +#endif /* CONFIG_SSDFS_DEBUG */ + + found = &node->raw.root_node.indexes[0 ]; + node->index_area.start_hash =le64_to_cpu (found->hash); + + found = &node->raw.root_node.indexes[node->index_area.index_count -1 ]; + node->index_area.end_hash =le64_to_cpu (found->hash); + +ssdfs_memcpy (&node->raw.root_node.header.node_ids[position], +0 ,sizeof (__le32), + &ptr->node_id,0 ,sizeof (__le32), +sizeof (__le32)); + +return 0 ; +} + +/* +* __ssdfs_btree_common_node_add_index() - add index record into the node +* @node: node object +* @position: position in the node for storing the new index record +* @ptr: pointer on storing index record [in] +* +* This method tries to add index record into the common node. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +*/ +static +int __ssdfs_btree_common_node_add_index (struct ssdfs_btree_node *node, +u16 position, +struct ssdfs_btree_index_key *ptr) +{ +struct page *page; +size_t index_key_len =sizeof (struct ssdfs_btree_index_key ); +u32 page_index; +u32 page_off; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !ptr); +BUG_ON (!rwsem_is_locked (&node->full_lock)); +BUG_ON (!rwsem_is_locked (&node->header_lock)); + +SSDFS_DBG ("node_id %u, position %u\n" , + node->node_id, position); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (position != node->index_area.index_count) { +SSDFS_ERR ("cannot add index: " +"position %u, index_count %u\n" , + position, + node->index_area.index_count); +return -ERANGE ; + } + + err =ssdfs_define_memory_page (node, &node->index_area, + position, + &page_index, &page_off); +if (unlikely (err)) { +SSDFS_ERR ("fail to define memory page: " +"node_id %u, position %u, err %d\n" , + node->node_id, position, err); +return err; + } + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (page_index >=U32_MAX ); +BUG_ON (page_off >=U32_MAX ); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (page_index >=pagevec_count (&node->content.pvec)) { +SSDFS_ERR ("page_index %u > pvec_size %u\n" , + page_index, +pagevec_count (&node->content.pvec)); +return -ERANGE ; + } + + page = node->content.pvec.pages[page_index]; + +ssdfs_lock_page (page); + err =ssdfs_memcpy_to_page (page, page_off,PAGE_SIZE , + ptr,0 , index_key_len, + index_key_len); +ssdfs_unlock_page (page); + +if (unlikely (err)) { +SSDFS_ERR ("fail to copy: err %d\n" , err); +return err; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("page_index %u, page_off %u\n" , + page_index, page_off); +SSDFS_DBG ("node_id %u, node_type %#x, hash %llx, " +"seg_id %llu, logical_blk %u, len %u\n" , +le32_to_cpu (ptr->node_id), + ptr->node_type, +le64_to_cpu (ptr->index.hash), +le64_to_cpu (ptr->index.extent.seg_id), +le32_to_cpu (ptr->index.extent.logical_blk), +le32_to_cpu (ptr->index.extent.len)); +#endif /* CONFIG_SSDFS_DEBUG */ + +return 0 ; +} + +/* +* ssdfs_btree_root_node_change_index() - change index record into root node +* @node: node object +* @found_index: position in the node of the changing index record +* @new_index: pointer on new index record state [in] +* +* This method tries to change the index record into the root node. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +*/ +static inline +int ssdfs_btree_root_node_change_index (struct ssdfs_btree_node *node, +u16 found_index, +struct ssdfs_btree_index_key *new_index) +{ +size_t index_size =sizeof (struct ssdfs_btree_index ); + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !new_index); +BUG_ON (!rwsem_is_locked (&node->full_lock)); +BUG_ON (!rwsem_is_locked (&node->header_lock)); + +SSDFS_DBG ("node_id %u, node_type %#x, found_index %u\n" , + node->node_id,atomic_read (&node->type), + found_index); +SSDFS_DBG ("node_id %u, node_type %#x, hash %llx, " +"seg_id %llu, logical_blk %u, len %u\n" , +le32_to_cpu (new_index->node_id), + new_index->node_type, +le64_to_cpu (new_index->index.hash), +le64_to_cpu (new_index->index.extent.seg_id), +le32_to_cpu (new_index->index.extent.logical_blk), +le32_to_cpu (new_index->index.extent.len)); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (found_index >=SSDFS_BTREE_ROOT_NODE_INDEX_COUNT ) { +SSDFS_ERR ("invalid found_index %u\n" , + found_index); +return -ERANGE ; + } + +ssdfs_memcpy (&node->raw.root_node.indexes[found_index], +0 , index_size, + &new_index->index,0 , index_size, + index_size); + +switch (found_index) { +case SSDFS_ROOT_NODE_LEFT_LEAF_NODE : + node->index_area.start_hash = +le64_to_cpu (new_index->index.hash); +break ; + +case SSDFS_ROOT_NODE_RIGHT_LEAF_NODE : + node->index_area.end_hash = +le64_to_cpu (new_index->index.hash); +break ; + +default : +BUG (); + } + +ssdfs_memcpy (&node->raw.root_node.header.node_ids[found_index], +0 ,sizeof (__le32), + &new_index->node_id,0 ,sizeof (__le32), +sizeof (__le32)); + +return 0 ; +} + +/* +* ssdfs_btree_common_node_change_index() - change index record into common node +* @node: node object +* @found_index: position in the node of the changing index record +* @new_index: pointer on new index record state [in] +* +* This method tries to change the index record into the common node. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +*/ +static +int ssdfs_btree_common_node_change_index (struct ssdfs_btree_node *node, +struct ssdfs_btree_node_index_area *area, +u16 found_index, +struct ssdfs_btree_index_key *new_index) +{ +struct page *page; +size_t index_key_len =sizeof (struct ssdfs_btree_index_key ); +u32 page_index; +u32 page_off; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !area); +BUG_ON (!rwsem_is_locked (&node->full_lock)); +BUG_ON (!rwsem_is_locked (&node->header_lock)); + +SSDFS_DBG ("node_id %u, node_type %#x, found_index %u\n" , + node->node_id,atomic_read (&node->type), + found_index); +SSDFS_DBG ("node_id %u, node_type %#x, hash %llx, " +"seg_id %llu, logical_blk %u, len %u\n" , +le32_to_cpu (new_index->node_id), + new_index->node_type, +le64_to_cpu (new_index->index.hash), +le64_to_cpu (new_index->index.extent.seg_id), +le32_to_cpu (new_index->index.extent.logical_blk), +le32_to_cpu (new_index->index.extent.len)); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (found_index == area->index_count) { +SSDFS_ERR ("found_index %u == index_count %u\n" , + found_index, area->index_count); +return -ERANGE ; + } + + err =ssdfs_define_memory_page (node, area, found_index, + &page_index, &page_off); +if (unlikely (err)) { +SSDFS_ERR ("fail to define memory page: " +"node_id %u, found_index %u, err %d\n" , + node->node_id, found_index, err); +return err; + } + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (page_index >=U32_MAX ); +BUG_ON (page_off >=U32_MAX ); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (page_index >=pagevec_count (&node->content.pvec)) { +SSDFS_ERR ("page_index %u > pvec_size %u\n" , + page_index, +pagevec_count (&node->content.pvec)); +return -ERANGE ; + } + + page = node->content.pvec.pages[page_index]; + err =ssdfs_memcpy_to_page (page, page_off,PAGE_SIZE , + new_index,0 , index_key_len, + index_key_len); +if (unlikely (err)) { +SSDFS_ERR ("fail to copy: err %d\n" , err); +return err; + } + +if (found_index ==0 ) + area->start_hash =le64_to_cpu (new_index->index.hash); +else if (found_index == (area->index_count -1 )) + area->end_hash =le64_to_cpu (new_index->index.hash); + +return 0 ; +} + +/* +* ssdfs_btree_common_node_insert_index() - insert index record into the node +* @node: node object +* @position: position in the node for storing the new index record +* @ptr: pointer on storing index record [in] +* +* This method tries to insert the index record into the common node. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +*/ +static +int ssdfs_btree_common_node_insert_index (struct ssdfs_btree_node *node, +u16 position, +struct ssdfs_btree_index_key *ptr) +{ +struct ssdfs_btree_index_key buffer[2 ]; +struct page *page; +void *kaddr; +u32 page_index; +u32 page_off; +u16 cur_pos = position; +u8 index_size; +bool is_valid_index_in_buffer = false; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !ptr); +BUG_ON (!rwsem_is_locked (&node->full_lock)); +BUG_ON (!rwsem_is_locked (&node->header_lock)); + +SSDFS_DBG ("node_id %u, position %u\n" , + node->node_id, position); +SSDFS_DBG ("node_id %u, node_type %#x, hash %llx, " +"seg_id %llu, logical_blk %u, len %u\n" , +le32_to_cpu (ptr->node_id), + ptr->node_type, +le64_to_cpu (ptr->index.hash), +le64_to_cpu (ptr->index.extent.seg_id), +le32_to_cpu (ptr->index.extent.logical_blk), +le32_to_cpu (ptr->index.extent.len)); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!(position < node->index_area.index_count)) { +SSDFS_ERR ("cannot insert index: " +"position %u, index_count %u\n" , + position, + node->index_area.index_count); +return -ERANGE ; + } + + index_size = node->index_area.index_size; +if (index_size !=sizeof (struct ssdfs_btree_index_key )) { +SSDFS_ERR ("invalid index_size %u\n" , + index_size); +return -ERANGE ; + } + +ssdfs_memcpy (&buffer[0 ],0 , index_size, + ptr,0 , index_size, + index_size); + +do { +u32 rest_capacity; +u32 moving_count; +u32 moving_bytes; + +if (cur_pos > node->index_area.index_count) { +SSDFS_ERR ("cur_pos %u, index_area.index_count %u\n" , + cur_pos, node->index_area.index_count); +return -ERANGE ; + } + + err =ssdfs_define_memory_page (node, &node->index_area, + cur_pos, + &page_index, &page_off); +if (unlikely (err)) { +SSDFS_ERR ("fail to define memory page: " +"node_id %u, position %u, err %d\n" , + node->node_id, cur_pos, err); +return err; + } + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (page_index >=U32_MAX ); +BUG_ON (page_off >=U32_MAX ); +#endif /* CONFIG_SSDFS_DEBUG */ + + rest_capacity =PAGE_SIZE - page_off; + rest_capacity /= index_size; + +if (rest_capacity ==0 ) { +SSDFS_WARN ("rest_capacity == 0\n" ); +return -ERANGE ; + } + + moving_count = node->index_area.index_count - cur_pos; + moving_count =min_t (u32, moving_count, rest_capacity); + +if (moving_count == rest_capacity) { +/* +* Latest item will be moved into +* temporary buffer (exclude from count) +*/ + moving_bytes = (moving_count -1 ) * index_size; + }else + moving_bytes = moving_count * index_size; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("page_index %u, page_off %u, cur_pos %u, " +"moving_count %u, rest_capacity %u\n" , + page_index, page_off, cur_pos, + moving_count, rest_capacity); + +if ((page_off + index_size) >PAGE_SIZE ) { +SSDFS_WARN ("invalid offset: " +"page_off %u, index_size %u\n" , + page_off, index_size); +return -ERANGE ; + } + +if ((page_off + moving_bytes + index_size) >PAGE_SIZE ) { +SSDFS_WARN ("invalid offset: " +"page_off %u, moving_bytes %u, " +"index_size %u\n" , + page_off, moving_bytes, index_size); +return -ERANGE ; + } +#endif /* CONFIG_SSDFS_DEBUG */ + +if (page_index >=pagevec_count (&node->content.pvec)) { +SSDFS_ERR ("page_index %u > pvec_size %u\n" , + page_index, +pagevec_count (&node->content.pvec)); +return -ERANGE ; + } + + page = node->content.pvec.pages[page_index]; + +ssdfs_lock_page (page); + kaddr =kmap_local_page (page); + +if (moving_count ==0 ) { + err =ssdfs_memcpy (kaddr, page_off,PAGE_SIZE , + &buffer[0 ],0 , index_size, + index_size); +if (unlikely (err)) { +SSDFS_ERR ("fail to copy: err %d\n" , err); + goto finish_copy; + } + + is_valid_index_in_buffer = false; + cur_pos++; + }else { +if (moving_count == rest_capacity) { + err =ssdfs_memcpy (&buffer[1 ], +0 , index_size, + kaddr, +PAGE_SIZE - index_size, +PAGE_SIZE , + index_size); +if (unlikely (err)) { +SSDFS_ERR ("fail to copy: err %d\n" , + err); + goto finish_copy; + } + + is_valid_index_in_buffer = true; + }else + is_valid_index_in_buffer = false; + + err =ssdfs_memmove (kaddr, + page_off + index_size, +PAGE_SIZE , + kaddr, + page_off, +PAGE_SIZE , + moving_bytes); +if (unlikely (err)) { +SSDFS_ERR ("fail to move: err %d\n" , + err); + goto finish_copy; + } + + err =ssdfs_memcpy (kaddr, page_off,PAGE_SIZE , + &buffer[0 ],0 , index_size, + index_size); +if (unlikely (err)) { +SSDFS_ERR ("fail to copy: err %d\n" , + err); + goto finish_copy; + } + +if (is_valid_index_in_buffer) { +ssdfs_memcpy (&buffer[0 ],0 , index_size, + &buffer[1 ],0 , index_size, + index_size); + } + + cur_pos += moving_count; + } + +finish_copy: +flush_dcache_page (page); +kunmap_local (kaddr); +ssdfs_unlock_page (page); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("cur_pos %u, index_area.index_count %u, " +"is_valid_index_in_buffer %#x\n" , + cur_pos, node->index_area.index_count, + is_valid_index_in_buffer); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (unlikely (err)) { +SSDFS_ERR ("cur_pos %u, index_area.index_count %u, " +"is_valid_index_in_buffer %#x\n" , + cur_pos, node->index_area.index_count, + is_valid_index_in_buffer); +return err; + } + }while (is_valid_index_in_buffer); + +return 0 ; +} + +/* +* ssdfs_btree_common_node_add_index() - add index record into the node +* @node: node object +* @position: position in the node for storing the new index record +* @ptr: pointer on storing index record [in] +* +* This method tries to add the index record into the common node. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +* %-ENOSPC - node hasn't free space. +*/ +static +int ssdfs_btree_common_node_add_index (struct ssdfs_btree_node *node, +u16 position, +struct ssdfs_btree_index_key *ptr) +{ +struct ssdfs_btree_index_key tmp_key; +u64 hash1, hash2; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !ptr); +BUG_ON (!rwsem_is_locked (&node->full_lock)); +BUG_ON (!rwsem_is_locked (&node->header_lock)); + +SSDFS_DBG ("node_id %u, position %u, index_count %u\n" , + node->node_id, position, + node->index_area.index_count); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (node->index_area.index_count > node->index_area.index_capacity) { +SSDFS_ERR ("index_count %u > index_capacity %u\n" , + node->index_area.index_count, + node->index_area.index_capacity); +return -ERANGE ; + } + +if (node->index_area.index_count == node->index_area.index_capacity) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to add the index: " +"index_count %u, index_capacity %u\n" , + node->index_area.index_count, + node->index_area.index_capacity); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ENOSPC ; + } + +if (position > node->index_area.index_count) { +SSDFS_ERR ("invalid index place: " +"position %u, index_count %u\n" , + position, + node->index_area.index_count); +return -ERANGE ; + } + +if (position == node->index_area.index_count) { + err =__ssdfs_btree_common_node_add_index (node, position, ptr); +if (unlikely (err)) { +SSDFS_ERR ("fail to add index: " +"node_id %u, position %u, err %d\n" , + node->node_id, position, err); +return err; + } + + node->index_area.index_count++; + }else { + err =__ssdfs_btree_common_node_extract_index (node, + &node->index_area, + position, + &tmp_key); +if (unlikely (err)) { +SSDFS_ERR ("fail to extract the index: err %d\n" , err); +return err; + } + + hash1 =le64_to_cpu (tmp_key.index.hash); + hash2 =le64_to_cpu (ptr->index.hash); + +if (hash1 == hash2) { + err =ssdfs_btree_common_node_change_index (node, + &node->index_area, + position, + ptr); +if (unlikely (err)) { +SSDFS_ERR ("fail to change index: " +"node_id %u, position %u, err %d\n" , + node->node_id, position, err); +return err; + } + }else { +if (hash2 > hash1) + position++; + +if (position == node->index_area.index_count) { + err =__ssdfs_btree_common_node_add_index (node, + position, + ptr); +if (unlikely (err)) { +SSDFS_ERR ("fail to add index: " +"node_id %u, position %u, " +"err %d\n" , + node->node_id, position, err); +return err; + } + }else { + err =ssdfs_btree_common_node_insert_index (node, + position, + ptr); +if (unlikely (err)) { +SSDFS_ERR ("fail to insert index: " +"node_id %u, position %u, " +"err %d\n" , + node->node_id, position, err); +return err; + } + } + + node->index_area.index_count++; + } + } + + err =__ssdfs_btree_common_node_extract_index (node, + &node->index_area, +0 , + &tmp_key); +if (unlikely (err)) { +SSDFS_ERR ("fail to extract the index: err %d\n" , err); +return err; + } + + node->index_area.start_hash =le64_to_cpu (tmp_key.index.hash); + + err =__ssdfs_btree_common_node_extract_index (node, + &node->index_area, + node->index_area.index_count -1 , + &tmp_key); +if (unlikely (err)) { +SSDFS_ERR ("fail to extract the index: err %d\n" , err); +return err; + } + + node->index_area.end_hash =le64_to_cpu (tmp_key.index.hash); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("start_hash %llx, end_hash %llx, " +"index_count %u, index_capacity %u\n" , + node->index_area.start_hash, + node->index_area.end_hash, + node->index_area.index_count, + node->index_area.index_capacity); +#endif /* CONFIG_SSDFS_DEBUG */ + +return 0 ; +} + +/* +* ssdfs_btree_node_add_index() - add index into node's index area +* @node: node object +* @index: new index +* +* This method tries to insert the index into node's index area. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +* %-ENOSPC - index area hasn't free space. +* %-ENOENT - node hasn't the index area. +* %-EFAULT - corrupted index or node's index area. +* %-EACCES - node is under initialization yet. +*/ +int ssdfs_btree_node_add_index (struct ssdfs_btree_node *node, +struct ssdfs_btree_index_key *index) +{ +struct ssdfs_fs_info *fsi; +u64 hash; +int node_type; +u16 found =U16_MAX ; +u16 count; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !node->tree || !node->tree->fsi || !index); +BUG_ON (!rwsem_is_locked (&node->tree->lock)); +#endif /* CONFIG_SSDFS_DEBUG */ + + hash =le64_to_cpu (index->index.hash); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node_id %u, hash %llx\n" , + node->node_id, hash); +#endif /* CONFIG_SSDFS_DEBUG */ + + fsi = node->tree->fsi; + +switch (atomic_read (&node->state)) { +case SSDFS_BTREE_NODE_CREATED : +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node %u is under initialization\n" , + node->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ +return -EACCES ; + +case SSDFS_BTREE_NODE_INITIALIZED : +case SSDFS_BTREE_NODE_DIRTY : +/* expected state */ +break ; + +default : +SSDFS_ERR ("invalid node state %#x\n" , +atomic_read (&node->state)); +return -ERANGE ; + } + +if (!is_ssdfs_btree_node_index_area_exist (node)) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node %u hasn't index area\n" , + node->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ENOENT ; + } + +#ifdef CONFIG_SSDFS_DEBUG +if (hash ==U64_MAX ) { +SSDFS_ERR ("invalid hash %llx\n" , hash); +return -ERANGE ; + } +#endif /* CONFIG_SSDFS_DEBUG */ + + node_type =atomic_read (&node->type); +if (node_type <=SSDFS_BTREE_NODE_UNKNOWN_TYPE || + node_type >=SSDFS_BTREE_NODE_TYPE_MAX ) { +SSDFS_ERR ("invalid node type %#x\n" , + node_type); +return -ERANGE ; + } + +if (!can_add_new_index (node)) { +u32 new_size; + +down_read (&node->header_lock); + new_size = node->index_area.area_size *2 ; +up_read (&node->header_lock); + + err =ssdfs_btree_node_resize_index_area (node, new_size); +if (err == -EACCES ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node %u is under initialization\n" , + node->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ +return err; + }else if (err == -ENOSPC ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("index area cannot be resized: " +"node_id %u\n" , node->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ +return err; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to resize index area: " +"node_id %u, new_size %u, err %d\n" , + node->node_id, new_size, err); +return err; + } + } + +if (node_type ==SSDFS_BTREE_ROOT_NODE ) { +down_read (&node->full_lock); +down_write (&node->header_lock); + + err =ssdfs_find_index_by_hash (node, &node->index_area, + hash, &found); + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (found >=U16_MAX ); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (err == -ENODATA ) { +/* node hasn't any index */ + err =0 ; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find an index: " +"node_id %u, hash %llx, err %d\n" , + node->node_id, hash, err); + goto finish_change_root_node; + } + + err =ssdfs_btree_root_node_add_index (node, found, + index); +if (unlikely (err)) { +SSDFS_ERR ("fail to change index: " +"node_id %u, node_type %#x, " +"found_index %u, err %d\n" , + node->node_id, node_type, + found, err); + } + +finish_change_root_node: +up_write (&node->header_lock); +up_read (&node->full_lock); + +if (unlikely (err)) { +ssdfs_debug_show_btree_node_indexes (node->tree, node); +return err; + } + }else { +down_write (&node->full_lock); +down_write (&node->header_lock); + + err =ssdfs_find_index_by_hash (node, &node->index_area, + hash, &found); + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (found >=U16_MAX ); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (err == -EEXIST ) { +/* index exist already */ + err =0 ; + }else if (err == -ENODATA ) { +/* node hasn't any index */ + err =0 ; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find an index: " +"node_id %u, hash %llx, err %d\n" , + node->node_id, hash, err); +up_write (&node->header_lock); +up_write (&node->full_lock); +return err; + } + + count = (node->index_area.index_count +1 ) - found; + err =ssdfs_lock_index_range (node, found, count); +BUG_ON (err == -ENODATA ); +if (unlikely (err)) { +SSDFS_ERR ("fail to lock index range: " +"start %u, count %u, err %d\n" , + found, count, err); +up_write (&node->header_lock); +up_write (&node->full_lock); +return err; + } + +downgrade_write (&node->full_lock); + + err =ssdfs_btree_common_node_add_index (node, found, + index); +ssdfs_unlock_index_range (node, found, count); + +if (!err) + err =ssdfs_set_dirty_index_range (node, found, count); + +up_write (&node->header_lock); +up_read (&node->full_lock); + +if (unlikely (err)) { +SSDFS_ERR ("fail to add index: " +"node_id %u, node_type %#x, " +"found_index %u, err %d\n" , + node->node_id, node_type, + found, err); +ssdfs_debug_show_btree_node_indexes (node->tree, node); + } + } + +ssdfs_set_node_update_cno (node); +set_ssdfs_btree_node_dirty (node); + +return 0 ; +} + +static inline +bool is_ssdfs_btree_index_key_identical (struct ssdfs_btree_index_key *index1, +struct ssdfs_btree_index_key *index2) +{ +u32 node_id1, node_id2; +u8 node_type1, node_type2; +u64 hash1, hash2; +u64 seg_id1, seg_id2; +u32 logical_blk1, logical_blk2; +u32 len1, len2; + + node_id1 =le32_to_cpu (index1->node_id); + node_type1 = index1->node_type; + hash1 =le64_to_cpu (index1->index.hash); + seg_id1 =le64_to_cpu (index1->index.extent.seg_id); + logical_blk1 =le32_to_cpu (index1->index.extent.logical_blk); + len1 =le32_to_cpu (index1->index.extent.len); + + node_id2 =le32_to_cpu (index2->node_id); + node_type2 = index2->node_type; + hash2 =le64_to_cpu (index2->index.hash); + seg_id2 =le64_to_cpu (index2->index.extent.seg_id); + logical_blk2 =le32_to_cpu (index2->index.extent.logical_blk); + len2 =le32_to_cpu (index2->index.extent.len); + +return node_id1 == node_id2 && node_type1 == node_type2 && + hash1 == hash2 && seg_id1 == seg_id2 && + logical_blk1 == logical_blk2 && len1 == len2; +} + +/* +* __ssdfs_btree_node_change_index() - change existing index +* @node: node object +* @old_index: old index +* @new_index: new index +* +* This method tries to change @old_index on @new_index into +* node's index area. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +* %-ENODATA - node's index area doesn't contain @old_index. +* %-ENOENT - node hasn't the index area. +* %-EFAULT - corrupted index or node's index area. +* %-EACCES - node is under initialization yet. +*/ +static +int __ssdfs_btree_node_change_index (struct ssdfs_btree_node *node, +struct ssdfs_btree_index_key *old_index, +struct ssdfs_btree_index_key *new_index) +{ +struct ssdfs_fs_info *fsi; +struct ssdfs_btree_node_index_area area; +size_t desc_size =sizeof (struct ssdfs_btree_node_index_area ); +int node_type; +u64 old_hash, new_hash; +u16 found =U16_MAX ; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !node->tree || !node->tree->fsi); +BUG_ON (!old_index || !new_index); +BUG_ON (!rwsem_is_locked (&node->tree->lock)); +#endif /* CONFIG_SSDFS_DEBUG */ + + fsi = node->tree->fsi; + old_hash =le64_to_cpu (old_index->index.hash); + new_hash =le64_to_cpu (new_index->index.hash); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node_id %u, old_hash %llx, new_hash %llx\n" , + node->node_id, old_hash, new_hash); +#endif /* CONFIG_SSDFS_DEBUG */ + +switch (atomic_read (&node->state)) { +case SSDFS_BTREE_NODE_CREATED : +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node %u is under initialization\n" , + node->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ +return -EACCES ; + +case SSDFS_BTREE_NODE_INITIALIZED : +case SSDFS_BTREE_NODE_DIRTY : +/* expected state */ +break ; + +default : +SSDFS_ERR ("invalid node state %#x\n" , +atomic_read (&node->state)); +return -ERANGE ; + } + +if (!is_ssdfs_btree_node_index_area_exist (node)) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node %u hasn't index area\n" , + node->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ENOENT ; + } + +#ifdef CONFIG_SSDFS_DEBUG +if (old_hash ==U64_MAX || new_hash ==U64_MAX ) { +SSDFS_ERR ("invalid old_hash %llx or new_hash %llx\n" , + old_hash, new_hash); +return -ERANGE ; + } +#endif /* CONFIG_SSDFS_DEBUG */ + + node_type =atomic_read (&node->type); +if (node_type <=SSDFS_BTREE_NODE_UNKNOWN_TYPE || + node_type >=SSDFS_BTREE_NODE_TYPE_MAX ) { +SSDFS_ERR ("invalid node type %#x\n" , + node_type); +return -ERANGE ; + } + +if (node_type ==SSDFS_BTREE_ROOT_NODE ) { +down_read (&node->full_lock); + + err =ssdfs_find_index_by_hash (node, &node->index_area, + old_hash, &found); +if (err == -EEXIST ) { + err =0 ; +/* +* Index has been found. +* Continue logic. +*/ + }else if (err == -ENODATA ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to find an index: " +"node_id %u, hash %llx\n" , + node->node_id, old_hash); +#endif /* CONFIG_SSDFS_DEBUG */ + goto finish_change_root_node; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find an index: " +"node_id %u, hash %llx, err %d\n" , + node->node_id, old_hash, err); + goto finish_change_root_node; + }else { + err = -ERANGE ; +SSDFS_ERR ("no index for the hash %llx\n" , + old_hash); + goto finish_change_root_node; + } + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (found ==U16_MAX ); +#endif /* CONFIG_SSDFS_DEBUG */ + +down_write (&node->header_lock); + + err =ssdfs_btree_root_node_change_index (node, found, + new_index); +if (unlikely (err)) { +SSDFS_ERR ("fail to change index: " +"node_id %u, node_type %#x, " +"found %u, err %d\n" , + node->node_id, node_type, + found, err); + } + +up_write (&node->header_lock); +finish_change_root_node: +up_read (&node->full_lock); + +if (unlikely (err)) +return err; + }else { +down_read (&node->full_lock); + +down_read (&node->header_lock); +ssdfs_memcpy (&area,0 , desc_size, + &node->index_area,0 , desc_size, + desc_size); +up_read (&node->header_lock); + + err =ssdfs_find_index_by_hash (node, &area, + old_hash, &found); +if (err == -EEXIST ) { + err =0 ; +/* +* Index has been found. +* Continue logic. +*/ + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find an index: " +"node_id %u, hash %llx, err %d\n" , + node->node_id, old_hash, err); + }else { + err = -ERANGE ; +SSDFS_ERR ("no index for the hash %llx\n" , + old_hash); + } + +up_read (&node->full_lock); + +if (unlikely (err)) +return err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (found ==U16_MAX ); +#endif /* CONFIG_SSDFS_DEBUG */ + +down_write (&node->full_lock); +down_write (&node->header_lock); + + err =ssdfs_lock_index_range (node, found,1 ); +BUG_ON (err == -ENODATA ); + +if (unlikely (err)) { +SSDFS_ERR ("fail to lock index %u, err %d\n" , + found, err); +up_write (&node->header_lock); +up_write (&node->full_lock); +return err; + } + +downgrade_write (&node->full_lock); + + err =ssdfs_btree_common_node_change_index (node, + &node->index_area, + found, new_index); +ssdfs_unlock_index_range (node, found,1 ); + +if (!err) + err =ssdfs_set_dirty_index_range (node, found,1 ); + +up_write (&node->header_lock); +up_read (&node->full_lock); + +if (unlikely (err)) { +SSDFS_ERR ("fail to change index: " +"node_id %u, node_type %#x, " +"found %u, err %d\n" , + node->node_id, node_type, + found, err); +return err; + } + } + +ssdfs_set_node_update_cno (node); +set_ssdfs_btree_node_dirty (node); + +return 0 ; +} + +/* +* ssdfs_btree_node_change_index() - change existing index +* @node: node object +* @old_index: old index +* @new_index: new index +* +* This method tries to change @old_index on @new_index into +* node's index area. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +* %-ENODATA - node's index area doesn't contain @old_index. +* %-ENOENT - node hasn't the index area. +* %-EFAULT - corrupted index or node's index area. +* %-EACCES - node is under initialization yet. +*/ +int ssdfs_btree_node_change_index (struct ssdfs_btree_node *node, +struct ssdfs_btree_index_key *old_index, +struct ssdfs_btree_index_key *new_index) +{ +u64 old_hash, new_hash; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!node || !node->tree || !node->tree->fsi); +BUG_ON (!old_index || !new_index); +BUG_ON (!rwsem_is_locked (&node->tree->lock)); +#endif /* CONFIG_SSDFS_DEBUG */ + + old_hash =le64_to_cpu (old_index->index.hash); + new_hash =le64_to_cpu (new_index->index.hash); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node_id %u, old_hash %llx, new_hash %llx\n" , + node->node_id, old_hash, new_hash); +#endif /* CONFIG_SSDFS_DEBUG */ + +switch (atomic_read (&node->state)) { +case SSDFS_BTREE_NODE_CREATED : +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node %u is under initialization\n" , + node->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ +return -EACCES ; + +case SSDFS_BTREE_NODE_INITIALIZED : +case SSDFS_BTREE_NODE_DIRTY : +/* expected state */ +break ; + +default : +SSDFS_ERR ("invalid node state %#x\n" , +atomic_read (&node->state)); +return -ERANGE ; + } + +if (!is_ssdfs_btree_node_index_area_exist (node)) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("node %u hasn't index area\n" , + node->node_id); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ENOENT ; + } + +#ifdef CONFIG_SSDFS_DEBUG +if (old_hash ==U64_MAX || new_hash ==U64_MAX ) { +SSDFS_ERR ("invalid old_hash %llx or new_hash %llx\n" , + old_hash, new_hash); +return -ERANGE ; + } +#endif /* CONFIG_SSDFS_DEBUG */ + +if (is_ssdfs_btree_index_key_identical (old_index, new_index)) { +SSDFS_DBG ("old and new index are identical\n" ); +return 0 ; + } + +if (old_hash == new_hash) { + err =__ssdfs_btree_node_change_index (node, old_index, + new_index); +if (unlikely (err)) { +SSDFS_ERR ("fail to change index: " +"old_hash %llx, err %d\n" , + old_hash, err); + goto fail_change_index; + } + }else { + err =ssdfs_btree_node_delete_index (node, old_hash); +if (unlikely (err)) { +SSDFS_ERR ("fail to delete index: " +"old_hash %llx, err %d\n" , + old_hash, err); + goto fail_change_index; + } + + err =ssdfs_btree_node_add_index (node, new_index); +if (unlikely (err)) { +SSDFS_ERR ("fail to add index: " +"new_hash %llx, err %d\n" , + new_hash, err); + goto fail_change_index; + } + } + +return 0 ; + +fail_change_index: +SSDFS_ERR ("node_id %u, node_type %#x\n" , + node->node_id, +atomic_read (&node->type)); +SSDFS_ERR ("node_id %u, node_type %#x, old_hash %llx, " +"seg_id %llu, logical_blk %u, len %u\n" , +le32_to_cpu (old_index->node_id), + old_index->node_type, +le64_to_cpu (old_index->index.hash), +le64_to_cpu (old_index->index.extent.seg_id), +le32_to_cpu (old_index->index.extent.logical_blk), +le32_to_cpu (old_index->index.extent.len)); +SSDFS_ERR ("node_id %u, node_type %#x, new_hash %llx, " +"seg_id %llu, logical_blk %u, len %u\n" , +le32_to_cpu (new_index->node_id), + new_index->node_type, +le64_to_cpu (new_index->index.hash), +le64_to_cpu (new_index->index.extent.seg_id), +le32_to_cpu (new_index->index.extent.logical_blk), +le32_to_cpu (new_index->index.extent.len)); + +return err; +}
Signature · 1 line
2.34.1