📄 btree.c
字号:
/** * befs_btree_read - Traverse leafnodes of a btree * @sb: Filesystem superblock * @ds: Datastream containing btree * @key_no: Key number (alphabetical order) of key to read * @bufsize: Size of the buffer to return key in * @keybuf: Pointer to a buffer to put the key in * @keysize: Length of the returned key * @value: Value stored with the returned key * * Heres how it works: Key_no is the index of the key/value pair to * return in keybuf/value. * Bufsize is the size of keybuf (BEFS_NAME_LEN+1 is a good size). Keysize is * the number of charecters in the key (just a convenience). * * Algorithm: * Get the first leafnode of the tree. See if the requested key is in that * node. If not, follow the node->right link to the next leafnode. Repeat * until the (key_no)th key is found or the tree is out of keys. */intbefs_btree_read(struct super_block *sb, befs_data_stream * ds, loff_t key_no, size_t bufsize, char *keybuf, size_t * keysize, befs_off_t * value){ befs_btree_node *this_node; befs_btree_super bt_super; befs_off_t node_off = 0; int cur_key; fs64 *valarray; char *keystart; u16 keylen; int res; uint key_sum = 0; befs_debug(sb, "---> befs_btree_read()"); if (befs_bt_read_super(sb, ds, &bt_super) != BEFS_OK) { befs_error(sb, "befs_btree_read() failed to read index superblock"); goto error; } if ((this_node = (befs_btree_node *) kmalloc(sizeof (befs_btree_node), GFP_NOFS)) == NULL) { befs_error(sb, "befs_btree_read() failed to allocate %u " "bytes of memory", sizeof (befs_btree_node)); goto error; } node_off = bt_super.root_node_ptr; this_node->bh = NULL; /* seeks down to first leafnode, reads it into this_node */ res = befs_btree_seekleaf(sb, ds, &bt_super, this_node, &node_off); if (res == BEFS_BT_EMPTY) { brelse(this_node->bh); kfree(this_node); *value = 0; *keysize = 0; befs_debug(sb, "<--- befs_btree_read() Tree is EMPTY"); return BEFS_BT_EMPTY; } else if (res == BEFS_ERR) { goto error_alloc; } /* find the leaf node containing the key_no key */ while (key_sum + this_node->head.all_key_count <= key_no) { /* no more nodes to look in: key_no is too large */ if (this_node->head.right == befs_bt_inval) { *keysize = 0; *value = 0; befs_debug(sb, "<--- befs_btree_read() END of keys at %Lu", key_sum + this_node->head.all_key_count); brelse(this_node->bh); kfree(this_node); return BEFS_BT_END; } key_sum += this_node->head.all_key_count; node_off = this_node->head.right; if (befs_bt_read_node(sb, ds, this_node, node_off) != BEFS_OK) { befs_error(sb, "befs_btree_read() failed to read " "node at %Lu", node_off); goto error_alloc; } } /* how many keys into this_node is key_no */ cur_key = key_no - key_sum; /* get pointers to datastructures within the node body */ valarray = befs_bt_valarray(this_node); keystart = befs_bt_get_key(sb, this_node, cur_key, &keylen); befs_debug(sb, "Read [%Lu,%d]: keysize %d", node_off, cur_key, keylen); if (bufsize < keylen + 1) { befs_error(sb, "befs_btree_read() keybuf too small (%u) " "for key of size %d", bufsize, keylen); brelse(this_node->bh); goto error_alloc; }; strncpy(keybuf, keystart, keylen); *value = fs64_to_cpu(sb, valarray[cur_key]); *keysize = keylen; keybuf[keylen] = '\0'; befs_debug(sb, "Read [%Lu,%d]: Key \"%.*s\", Value %Lu", node_off, cur_key, keylen, keybuf, *value); brelse(this_node->bh); kfree(this_node); befs_debug(sb, "<--- befs_btree_read()"); return BEFS_OK; error_alloc: kfree(this_node); error: *keysize = 0; *value = 0; befs_debug(sb, "<--- befs_btree_read() ERROR"); return BEFS_ERR;}/** * befs_btree_seekleaf - Find the first leafnode in the btree * @sb: Filesystem superblock * @ds: Datastream containing btree * @bt_super: Pointer to the superblock of the btree * @this_node: Buffer to return the leafnode in * @node_off: Pointer to offset of current node within datastream. Modified * by the function. * * * Helper function for btree traverse. Moves the current position to the * start of the first leaf node. * * Also checks for an empty tree. If there are no keys, returns BEFS_BT_EMPTY. */static intbefs_btree_seekleaf(struct super_block *sb, befs_data_stream * ds, befs_btree_super * bt_super, befs_btree_node * this_node, befs_off_t * node_off){ befs_debug(sb, "---> befs_btree_seekleaf()"); if (befs_bt_read_node(sb, ds, this_node, *node_off) != BEFS_OK) { befs_error(sb, "befs_btree_seekleaf() failed to read " "node at %Lu", *node_off); goto error; } befs_debug(sb, "Seekleaf to root node %Lu", *node_off); if (this_node->head.all_key_count == 0 && befs_leafnode(this_node)) { befs_debug(sb, "<--- befs_btree_seekleaf() Tree is EMPTY"); return BEFS_BT_EMPTY; } while (!befs_leafnode(this_node)) { if (this_node->head.all_key_count == 0) { befs_debug(sb, "befs_btree_seekleaf() encountered " "an empty interior node: %Lu. Using Overflow " "node: %Lu", *node_off, this_node->head.overflow); *node_off = this_node->head.overflow; } else { fs64 *valarray = befs_bt_valarray(this_node); *node_off = fs64_to_cpu(sb, valarray[0]); } if (befs_bt_read_node(sb, ds, this_node, *node_off) != BEFS_OK) { befs_error(sb, "befs_btree_seekleaf() failed to read " "node at %Lu", *node_off); goto error; } befs_debug(sb, "Seekleaf to child node %Lu", *node_off); } befs_debug(sb, "Node %Lu is a leaf node", *node_off); return BEFS_OK; error: befs_debug(sb, "<--- befs_btree_seekleaf() ERROR"); return BEFS_ERR;}/** * befs_leafnode - Determine if the btree node is a leaf node or an * interior node * @node: Pointer to node structure to test * * Return 1 if leaf, 0 if interior */static intbefs_leafnode(befs_btree_node * node){ /* all interior nodes (and only interior nodes) have an overflow node */ if (node->head.overflow == befs_bt_inval) return 1; else return 0;}/** * befs_bt_keylen_index - Finds start of keylen index in a node * @node: Pointer to the node structure to find the keylen index within * * Returns a pointer to the start of the key length index array * of the B+tree node *@node * * "The length of all the keys in the node is added to the size of the * header and then rounded up to a multiple of four to get the beginning * of the key length index" (p.88, practical filesystem design). * * Except that rounding up to 8 works, and rounding up to 4 doesn't. */static fs16 *befs_bt_keylen_index(befs_btree_node * node){ const int keylen_align = 8; unsigned long int off = (sizeof (befs_btree_nodehead) + node->head.all_key_length); ulong tmp = off % keylen_align; if (tmp) off += keylen_align - tmp; return (fs16 *) ((void *) node->od_node + off);}/** * befs_bt_valarray - Finds the start of value array in a node * @node: Pointer to the node structure to find the value array within * * Returns a pointer to the start of the value array * of the node pointed to by the node header */static fs64 *befs_bt_valarray(befs_btree_node * node){ void *keylen_index_start = (void *) befs_bt_keylen_index(node); size_t keylen_index_size = node->head.all_key_count * sizeof (fs16); return (fs64 *) (keylen_index_start + keylen_index_size);}/** * befs_bt_keydata - Finds start of keydata array in a node * @node: Pointer to the node structure to find the keydata array within * * Returns a pointer to the start of the keydata array * of the node pointed to by the node header */static char *befs_bt_keydata(befs_btree_node * node){ return (char *) ((void *) node->od_node + sizeof (befs_btree_nodehead));}/** * befs_bt_get_key - returns a pointer to the start of a key * @sb: filesystem superblock * @node: node in which to look for the key * @index: the index of the key to get * @keylen: modified to be the length of the key at @index * * Returns a valid pointer into @node on success. * Returns NULL on failure (bad input) and sets *@keylen = 0 */static char *befs_bt_get_key(struct super_block *sb, befs_btree_node * node, int index, u16 * keylen){ int prev_key_end; char *keystart; fs16 *keylen_index; if (index < 0 || index > node->head.all_key_count) { *keylen = 0; return NULL; } keystart = befs_bt_keydata(node); keylen_index = befs_bt_keylen_index(node); if (index == 0) prev_key_end = 0; else prev_key_end = fs16_to_cpu(sb, keylen_index[index - 1]); *keylen = fs16_to_cpu(sb, keylen_index[index]) - prev_key_end; return keystart + prev_key_end;}/** * befs_compare_strings - compare two strings * @key1: pointer to the first key to be compared * @keylen1: length in bytes of key1 * @key2: pointer to the second key to be compared * @kelen2: length in bytes of key2 * * Returns 0 if @key1 and @key2 are equal. * Returns >0 if @key1 is greater. * Returns <0 if @key2 is greater.. */static intbefs_compare_strings(const void *key1, int keylen1, const void *key2, int keylen2){ int len = min_t(int, keylen1, keylen2); int result = strncmp(key1, key2, len); if (result == 0) result = keylen1 - keylen2; return result;}/* These will be used for non-string keyed btrees */#if 0static intbtree_compare_int32(cont void *key1, int keylen1, const void *key2, int keylen2){ return *(int32_t *) key1 - *(int32_t *) key2;}static intbtree_compare_uint32(cont void *key1, int keylen1, const void *key2, int keylen2){ if (*(u_int32_t *) key1 == *(u_int32_t *) key2) return 0; else if (*(u_int32_t *) key1 > *(u_int32_t *) key2) return 1; return -1;}static intbtree_compare_int64(cont void *key1, int keylen1, const void *key2, int keylen2){ if (*(int64_t *) key1 == *(int64_t *) key2) return 0; else if (*(int64_t *) key1 > *(int64_t *) key2) return 1; return -1;}static intbtree_compare_uint64(cont void *key1, int keylen1, const void *key2, int keylen2){ if (*(u_int64_t *) key1 == *(u_int64_t *) key2) return 0; else if (*(u_int64_t *) key1 > *(u_int64_t *) key2) return 1; return -1;}static intbtree_compare_float(cont void *key1, int keylen1, const void *key2, int keylen2){ float result = *(float *) key1 - *(float *) key2; if (result == 0.0f) return 0; return (result < 0.0f) ? -1 : 1;}static intbtree_compare_double(cont void *key1, int keylen1, const void *key2, int keylen2){ double result = *(double *) key1 - *(double *) key2; if (result == 0.0) return 0; return (result < 0.0) ? -1 : 1;}#endif //0
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -