Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * linux/fs/befs/btree.c
0003  *
0004  * Copyright (C) 2001-2002 Will Dyson <will_dyson@pobox.com>
0005  *
0006  * Licensed under the GNU GPL. See the file COPYING for details.
0007  *
0008  * 2002-02-05: Sergey S. Kostyliov added binary search within
0009  *      btree nodes.
0010  *
0011  * Many thanks to:
0012  *
0013  * Dominic Giampaolo, author of "Practical File System
0014  * Design with the Be File System", for such a helpful book.
0015  *
0016  * Marcus J. Ranum, author of the b+tree package in
0017  * comp.sources.misc volume 10. This code is not copied from that
0018  * work, but it is partially based on it.
0019  *
0020  * Makoto Kato, author of the original BeFS for linux filesystem
0021  * driver.
0022  */
0023 
0024 #include <linux/kernel.h>
0025 #include <linux/string.h>
0026 #include <linux/slab.h>
0027 #include <linux/mm.h>
0028 #include <linux/buffer_head.h>
0029 
0030 #include "befs.h"
0031 #include "btree.h"
0032 #include "datastream.h"
0033 
0034 /*
0035  * The btree functions in this file are built on top of the
0036  * datastream.c interface, which is in turn built on top of the
0037  * io.c interface.
0038  */
0039 
0040 /* Befs B+tree structure:
0041  *
0042  * The first thing in the tree is the tree superblock. It tells you
0043  * all kinds of useful things about the tree, like where the rootnode
0044  * is located, and the size of the nodes (always 1024 with current version
0045  * of BeOS).
0046  *
0047  * The rest of the tree consists of a series of nodes. Nodes contain a header
0048  * (struct befs_btree_nodehead), the packed key data, an array of shorts
0049  * containing the ending offsets for each of the keys, and an array of
0050  * befs_off_t values. In interior nodes, the keys are the ending keys for
0051  * the childnode they point to, and the values are offsets into the
0052  * datastream containing the tree.
0053  */
0054 
0055 /* Note:
0056  *
0057  * The book states 2 confusing things about befs b+trees. First,
0058  * it states that the overflow field of node headers is used by internal nodes
0059  * to point to another node that "effectively continues this one". Here is what
0060  * I believe that means. Each key in internal nodes points to another node that
0061  * contains key values less than itself. Inspection reveals that the last key
0062  * in the internal node is not the last key in the index. Keys that are
0063  * greater than the last key in the internal node go into the overflow node.
0064  * I imagine there is a performance reason for this.
0065  *
0066  * Second, it states that the header of a btree node is sufficient to
0067  * distinguish internal nodes from leaf nodes. Without saying exactly how.
0068  * After figuring out the first, it becomes obvious that internal nodes have
0069  * overflow nodes and leafnodes do not.
0070  */
0071 
0072 /*
0073  * Currently, this code is only good for directory B+trees.
0074  * In order to be used for other BFS indexes, it needs to be extended to handle
0075  * duplicate keys and non-string keytypes (int32, int64, float, double).
0076  */
0077 
0078 /*
0079  * In memory structure of each btree node
0080  */
0081 struct befs_btree_node {
0082     befs_host_btree_nodehead head;  /* head of node converted to cpu byteorder */
0083     struct buffer_head *bh;
0084     befs_btree_nodehead *od_node;   /* on disk node */
0085 };
0086 
0087 /* local constants */
0088 static const befs_off_t BEFS_BT_INVAL = 0xffffffffffffffffULL;
0089 
0090 /* local functions */
0091 static int befs_btree_seekleaf(struct super_block *sb, const befs_data_stream *ds,
0092                    befs_btree_super * bt_super,
0093                    struct befs_btree_node *this_node,
0094                    befs_off_t * node_off);
0095 
0096 static int befs_bt_read_super(struct super_block *sb, const befs_data_stream *ds,
0097                   befs_btree_super * sup);
0098 
0099 static int befs_bt_read_node(struct super_block *sb, const befs_data_stream *ds,
0100                  struct befs_btree_node *node,
0101                  befs_off_t node_off);
0102 
0103 static int befs_leafnode(struct befs_btree_node *node);
0104 
0105 static fs16 *befs_bt_keylen_index(struct befs_btree_node *node);
0106 
0107 static fs64 *befs_bt_valarray(struct befs_btree_node *node);
0108 
0109 static char *befs_bt_keydata(struct befs_btree_node *node);
0110 
0111 static int befs_find_key(struct super_block *sb,
0112              struct befs_btree_node *node,
0113              const char *findkey, befs_off_t * value);
0114 
0115 static char *befs_bt_get_key(struct super_block *sb,
0116                  struct befs_btree_node *node,
0117                  int index, u16 * keylen);
0118 
0119 static int befs_compare_strings(const void *key1, int keylen1,
0120                 const void *key2, int keylen2);
0121 
0122 /**
0123  * befs_bt_read_super() - read in btree superblock convert to cpu byteorder
0124  * @sb:        Filesystem superblock
0125  * @ds:        Datastream to read from
0126  * @sup:       Buffer in which to place the btree superblock
0127  *
0128  * Calls befs_read_datastream to read in the btree superblock and
0129  * makes sure it is in cpu byteorder, byteswapping if necessary.
0130  * Return: BEFS_OK on success and if *@sup contains the btree superblock in cpu
0131  * byte order. Otherwise return BEFS_ERR on error.
0132  */
0133 static int
0134 befs_bt_read_super(struct super_block *sb, const befs_data_stream *ds,
0135            befs_btree_super * sup)
0136 {
0137     struct buffer_head *bh;
0138     befs_disk_btree_super *od_sup;
0139 
0140     befs_debug(sb, "---> %s", __func__);
0141 
0142     bh = befs_read_datastream(sb, ds, 0, NULL);
0143 
0144     if (!bh) {
0145         befs_error(sb, "Couldn't read index header.");
0146         goto error;
0147     }
0148     od_sup = (befs_disk_btree_super *) bh->b_data;
0149     befs_dump_index_entry(sb, od_sup);
0150 
0151     sup->magic = fs32_to_cpu(sb, od_sup->magic);
0152     sup->node_size = fs32_to_cpu(sb, od_sup->node_size);
0153     sup->max_depth = fs32_to_cpu(sb, od_sup->max_depth);
0154     sup->data_type = fs32_to_cpu(sb, od_sup->data_type);
0155     sup->root_node_ptr = fs64_to_cpu(sb, od_sup->root_node_ptr);
0156 
0157     brelse(bh);
0158     if (sup->magic != BEFS_BTREE_MAGIC) {
0159         befs_error(sb, "Index header has bad magic.");
0160         goto error;
0161     }
0162 
0163     befs_debug(sb, "<--- %s", __func__);
0164     return BEFS_OK;
0165 
0166       error:
0167     befs_debug(sb, "<--- %s ERROR", __func__);
0168     return BEFS_ERR;
0169 }
0170 
0171 /**
0172  * befs_bt_read_node - read in btree node and convert to cpu byteorder
0173  * @sb: Filesystem superblock
0174  * @ds: Datastream to read from
0175  * @node: Buffer in which to place the btree node
0176  * @node_off: Starting offset (in bytes) of the node in @ds
0177  *
0178  * Calls befs_read_datastream to read in the indicated btree node and
0179  * makes sure its header fields are in cpu byteorder, byteswapping if
0180  * necessary.
0181  * Note: node->bh must be NULL when this function is called the first time.
0182  * Don't forget brelse(node->bh) after last call.
0183  *
0184  * On success, returns BEFS_OK and *@node contains the btree node that
0185  * starts at @node_off, with the node->head fields in cpu byte order.
0186  *
0187  * On failure, BEFS_ERR is returned.
0188  */
0189 
0190 static int
0191 befs_bt_read_node(struct super_block *sb, const befs_data_stream *ds,
0192           struct befs_btree_node *node, befs_off_t node_off)
0193 {
0194     uint off = 0;
0195 
0196     befs_debug(sb, "---> %s", __func__);
0197 
0198     if (node->bh)
0199         brelse(node->bh);
0200 
0201     node->bh = befs_read_datastream(sb, ds, node_off, &off);
0202     if (!node->bh) {
0203         befs_error(sb, "%s failed to read "
0204                "node at %llu", __func__, node_off);
0205         befs_debug(sb, "<--- %s ERROR", __func__);
0206 
0207         return BEFS_ERR;
0208     }
0209     node->od_node =
0210         (befs_btree_nodehead *) ((void *) node->bh->b_data + off);
0211 
0212     befs_dump_index_node(sb, node->od_node);
0213 
0214     node->head.left = fs64_to_cpu(sb, node->od_node->left);
0215     node->head.right = fs64_to_cpu(sb, node->od_node->right);
0216     node->head.overflow = fs64_to_cpu(sb, node->od_node->overflow);
0217     node->head.all_key_count =
0218         fs16_to_cpu(sb, node->od_node->all_key_count);
0219     node->head.all_key_length =
0220         fs16_to_cpu(sb, node->od_node->all_key_length);
0221 
0222     befs_debug(sb, "<--- %s", __func__);
0223     return BEFS_OK;
0224 }
0225 
0226 /**
0227  * befs_btree_find - Find a key in a befs B+tree
0228  * @sb: Filesystem superblock
0229  * @ds: Datastream containing btree
0230  * @key: Key string to lookup in btree
0231  * @value: Value stored with @key
0232  *
0233  * On success, returns BEFS_OK and sets *@value to the value stored
0234  * with @key (usually the disk block number of an inode).
0235  *
0236  * On failure, returns BEFS_ERR or BEFS_BT_NOT_FOUND.
0237  *
0238  * Algorithm:
0239  *   Read the superblock and rootnode of the b+tree.
0240  *   Drill down through the interior nodes using befs_find_key().
0241  *   Once at the correct leaf node, use befs_find_key() again to get the
0242  *   actual value stored with the key.
0243  */
0244 int
0245 befs_btree_find(struct super_block *sb, const befs_data_stream *ds,
0246         const char *key, befs_off_t * value)
0247 {
0248     struct befs_btree_node *this_node;
0249     befs_btree_super bt_super;
0250     befs_off_t node_off;
0251     int res;
0252 
0253     befs_debug(sb, "---> %s Key: %s", __func__, key);
0254 
0255     if (befs_bt_read_super(sb, ds, &bt_super) != BEFS_OK) {
0256         befs_error(sb,
0257                "befs_btree_find() failed to read index superblock");
0258         goto error;
0259     }
0260 
0261     this_node = kmalloc(sizeof(struct befs_btree_node),
0262                         GFP_NOFS);
0263     if (!this_node) {
0264         befs_error(sb, "befs_btree_find() failed to allocate %zu "
0265                "bytes of memory", sizeof(struct befs_btree_node));
0266         goto error;
0267     }
0268 
0269     this_node->bh = NULL;
0270 
0271     /* read in root node */
0272     node_off = bt_super.root_node_ptr;
0273     if (befs_bt_read_node(sb, ds, this_node, node_off) != BEFS_OK) {
0274         befs_error(sb, "befs_btree_find() failed to read "
0275                "node at %llu", node_off);
0276         goto error_alloc;
0277     }
0278 
0279     while (!befs_leafnode(this_node)) {
0280         res = befs_find_key(sb, this_node, key, &node_off);
0281         /* if no key set, try the overflow node */
0282         if (res == BEFS_BT_OVERFLOW)
0283             node_off = this_node->head.overflow;
0284         if (befs_bt_read_node(sb, ds, this_node, node_off) != BEFS_OK) {
0285             befs_error(sb, "befs_btree_find() failed to read "
0286                    "node at %llu", node_off);
0287             goto error_alloc;
0288         }
0289     }
0290 
0291     /* at a leaf node now, check if it is correct */
0292     res = befs_find_key(sb, this_node, key, value);
0293 
0294     brelse(this_node->bh);
0295     kfree(this_node);
0296 
0297     if (res != BEFS_BT_MATCH) {
0298         befs_error(sb, "<--- %s Key %s not found", __func__, key);
0299         befs_debug(sb, "<--- %s ERROR", __func__);
0300         *value = 0;
0301         return BEFS_BT_NOT_FOUND;
0302     }
0303     befs_debug(sb, "<--- %s Found key %s, value %llu", __func__,
0304            key, *value);
0305     return BEFS_OK;
0306 
0307       error_alloc:
0308     kfree(this_node);
0309       error:
0310     *value = 0;
0311     befs_debug(sb, "<--- %s ERROR", __func__);
0312     return BEFS_ERR;
0313 }
0314 
0315 /**
0316  * befs_find_key - Search for a key within a node
0317  * @sb: Filesystem superblock
0318  * @node: Node to find the key within
0319  * @findkey: Keystring to search for
0320  * @value: If key is found, the value stored with the key is put here
0321  *
0322  * Finds exact match if one exists, and returns BEFS_BT_MATCH.
0323  * If there is no match and node's value array is too small for key, return
0324  * BEFS_BT_OVERFLOW.
0325  * If no match and node should countain this key, return BEFS_BT_NOT_FOUND.
0326  *
0327  * Uses binary search instead of a linear.
0328  */
0329 static int
0330 befs_find_key(struct super_block *sb, struct befs_btree_node *node,
0331           const char *findkey, befs_off_t * value)
0332 {
0333     int first, last, mid;
0334     int eq;
0335     u16 keylen;
0336     int findkey_len;
0337     char *thiskey;
0338     fs64 *valarray;
0339 
0340     befs_debug(sb, "---> %s %s", __func__, findkey);
0341 
0342     findkey_len = strlen(findkey);
0343 
0344     /* if node can not contain key, just skip this node */
0345     last = node->head.all_key_count - 1;
0346     thiskey = befs_bt_get_key(sb, node, last, &keylen);
0347 
0348     eq = befs_compare_strings(thiskey, keylen, findkey, findkey_len);
0349     if (eq < 0) {
0350         befs_debug(sb, "<--- node can't contain %s", findkey);
0351         return BEFS_BT_OVERFLOW;
0352     }
0353 
0354     valarray = befs_bt_valarray(node);
0355 
0356     /* simple binary search */
0357     first = 0;
0358     mid = 0;
0359     while (last >= first) {
0360         mid = (last + first) / 2;
0361         befs_debug(sb, "first: %d, last: %d, mid: %d", first, last,
0362                mid);
0363         thiskey = befs_bt_get_key(sb, node, mid, &keylen);
0364         eq = befs_compare_strings(thiskey, keylen, findkey,
0365                       findkey_len);
0366 
0367         if (eq == 0) {
0368             befs_debug(sb, "<--- %s found %s at %d",
0369                    __func__, thiskey, mid);
0370 
0371             *value = fs64_to_cpu(sb, valarray[mid]);
0372             return BEFS_BT_MATCH;
0373         }
0374         if (eq > 0)
0375             last = mid - 1;
0376         else
0377             first = mid + 1;
0378     }
0379 
0380     /* return an existing value so caller can arrive to a leaf node */
0381     if (eq < 0)
0382         *value = fs64_to_cpu(sb, valarray[mid + 1]);
0383     else
0384         *value = fs64_to_cpu(sb, valarray[mid]);
0385     befs_error(sb, "<--- %s %s not found", __func__, findkey);
0386     befs_debug(sb, "<--- %s ERROR", __func__);
0387     return BEFS_BT_NOT_FOUND;
0388 }
0389 
0390 /**
0391  * befs_btree_read - Traverse leafnodes of a btree
0392  * @sb: Filesystem superblock
0393  * @ds: Datastream containing btree
0394  * @key_no: Key number (alphabetical order) of key to read
0395  * @bufsize: Size of the buffer to return key in
0396  * @keybuf: Pointer to a buffer to put the key in
0397  * @keysize: Length of the returned key
0398  * @value: Value stored with the returned key
0399  *
0400  * Here's how it works: Key_no is the index of the key/value pair to
0401  * return in keybuf/value.
0402  * Bufsize is the size of keybuf (BEFS_NAME_LEN+1 is a good size). Keysize is
0403  * the number of characters in the key (just a convenience).
0404  *
0405  * Algorithm:
0406  *   Get the first leafnode of the tree. See if the requested key is in that
0407  *   node. If not, follow the node->right link to the next leafnode. Repeat
0408  *   until the (key_no)th key is found or the tree is out of keys.
0409  */
0410 int
0411 befs_btree_read(struct super_block *sb, const befs_data_stream *ds,
0412         loff_t key_no, size_t bufsize, char *keybuf, size_t * keysize,
0413         befs_off_t * value)
0414 {
0415     struct befs_btree_node *this_node;
0416     befs_btree_super bt_super;
0417     befs_off_t node_off;
0418     int cur_key;
0419     fs64 *valarray;
0420     char *keystart;
0421     u16 keylen;
0422     int res;
0423 
0424     uint key_sum = 0;
0425 
0426     befs_debug(sb, "---> %s", __func__);
0427 
0428     if (befs_bt_read_super(sb, ds, &bt_super) != BEFS_OK) {
0429         befs_error(sb,
0430                "befs_btree_read() failed to read index superblock");
0431         goto error;
0432     }
0433 
0434     this_node = kmalloc(sizeof(struct befs_btree_node), GFP_NOFS);
0435     if (this_node == NULL) {
0436         befs_error(sb, "befs_btree_read() failed to allocate %zu "
0437                "bytes of memory", sizeof(struct befs_btree_node));
0438         goto error;
0439     }
0440 
0441     node_off = bt_super.root_node_ptr;
0442     this_node->bh = NULL;
0443 
0444     /* seeks down to first leafnode, reads it into this_node */
0445     res = befs_btree_seekleaf(sb, ds, &bt_super, this_node, &node_off);
0446     if (res == BEFS_BT_EMPTY) {
0447         brelse(this_node->bh);
0448         kfree(this_node);
0449         *value = 0;
0450         *keysize = 0;
0451         befs_debug(sb, "<--- %s Tree is EMPTY", __func__);
0452         return BEFS_BT_EMPTY;
0453     } else if (res == BEFS_ERR) {
0454         goto error_alloc;
0455     }
0456 
0457     /* find the leaf node containing the key_no key */
0458 
0459     while (key_sum + this_node->head.all_key_count <= key_no) {
0460 
0461         /* no more nodes to look in: key_no is too large */
0462         if (this_node->head.right == BEFS_BT_INVAL) {
0463             *keysize = 0;
0464             *value = 0;
0465             befs_debug(sb,
0466                    "<--- %s END of keys at %llu", __func__,
0467                    (unsigned long long)
0468                    key_sum + this_node->head.all_key_count);
0469             brelse(this_node->bh);
0470             kfree(this_node);
0471             return BEFS_BT_END;
0472         }
0473 
0474         key_sum += this_node->head.all_key_count;
0475         node_off = this_node->head.right;
0476 
0477         if (befs_bt_read_node(sb, ds, this_node, node_off) != BEFS_OK) {
0478             befs_error(sb, "%s failed to read node at %llu",
0479                   __func__, (unsigned long long)node_off);
0480             goto error_alloc;
0481         }
0482     }
0483 
0484     /* how many keys into this_node is key_no */
0485     cur_key = key_no - key_sum;
0486 
0487     /* get pointers to datastructures within the node body */
0488     valarray = befs_bt_valarray(this_node);
0489 
0490     keystart = befs_bt_get_key(sb, this_node, cur_key, &keylen);
0491 
0492     befs_debug(sb, "Read [%llu,%d]: keysize %d",
0493            (long long unsigned int)node_off, (int)cur_key,
0494            (int)keylen);
0495 
0496     if (bufsize < keylen + 1) {
0497         befs_error(sb, "%s keybuf too small (%zu) "
0498                "for key of size %d", __func__, bufsize, keylen);
0499         brelse(this_node->bh);
0500         goto error_alloc;
0501     }
0502 
0503     strlcpy(keybuf, keystart, keylen + 1);
0504     *value = fs64_to_cpu(sb, valarray[cur_key]);
0505     *keysize = keylen;
0506 
0507     befs_debug(sb, "Read [%llu,%d]: Key \"%.*s\", Value %llu", node_off,
0508            cur_key, keylen, keybuf, *value);
0509 
0510     brelse(this_node->bh);
0511     kfree(this_node);
0512 
0513     befs_debug(sb, "<--- %s", __func__);
0514 
0515     return BEFS_OK;
0516 
0517       error_alloc:
0518     kfree(this_node);
0519 
0520       error:
0521     *keysize = 0;
0522     *value = 0;
0523     befs_debug(sb, "<--- %s ERROR", __func__);
0524     return BEFS_ERR;
0525 }
0526 
0527 /**
0528  * befs_btree_seekleaf - Find the first leafnode in the btree
0529  * @sb: Filesystem superblock
0530  * @ds: Datastream containing btree
0531  * @bt_super: Pointer to the superblock of the btree
0532  * @this_node: Buffer to return the leafnode in
0533  * @node_off: Pointer to offset of current node within datastream. Modified
0534  *      by the function.
0535  *
0536  * Helper function for btree traverse. Moves the current position to the
0537  * start of the first leaf node.
0538  *
0539  * Also checks for an empty tree. If there are no keys, returns BEFS_BT_EMPTY.
0540  */
0541 static int
0542 befs_btree_seekleaf(struct super_block *sb, const befs_data_stream *ds,
0543             befs_btree_super *bt_super,
0544             struct befs_btree_node *this_node,
0545             befs_off_t * node_off)
0546 {
0547 
0548     befs_debug(sb, "---> %s", __func__);
0549 
0550     if (befs_bt_read_node(sb, ds, this_node, *node_off) != BEFS_OK) {
0551         befs_error(sb, "%s failed to read "
0552                "node at %llu", __func__, *node_off);
0553         goto error;
0554     }
0555     befs_debug(sb, "Seekleaf to root node %llu", *node_off);
0556 
0557     if (this_node->head.all_key_count == 0 && befs_leafnode(this_node)) {
0558         befs_debug(sb, "<--- %s Tree is EMPTY", __func__);
0559         return BEFS_BT_EMPTY;
0560     }
0561 
0562     while (!befs_leafnode(this_node)) {
0563 
0564         if (this_node->head.all_key_count == 0) {
0565             befs_debug(sb, "%s encountered "
0566                    "an empty interior node: %llu. Using Overflow "
0567                    "node: %llu", __func__, *node_off,
0568                    this_node->head.overflow);
0569             *node_off = this_node->head.overflow;
0570         } else {
0571             fs64 *valarray = befs_bt_valarray(this_node);
0572             *node_off = fs64_to_cpu(sb, valarray[0]);
0573         }
0574         if (befs_bt_read_node(sb, ds, this_node, *node_off) != BEFS_OK) {
0575             befs_error(sb, "%s failed to read "
0576                    "node at %llu", __func__, *node_off);
0577             goto error;
0578         }
0579 
0580         befs_debug(sb, "Seekleaf to child node %llu", *node_off);
0581     }
0582     befs_debug(sb, "Node %llu is a leaf node", *node_off);
0583 
0584     return BEFS_OK;
0585 
0586       error:
0587     befs_debug(sb, "<--- %s ERROR", __func__);
0588     return BEFS_ERR;
0589 }
0590 
0591 /**
0592  * befs_leafnode - Determine if the btree node is a leaf node or an
0593  * interior node
0594  * @node: Pointer to node structure to test
0595  *
0596  * Return 1 if leaf, 0 if interior
0597  */
0598 static int
0599 befs_leafnode(struct befs_btree_node *node)
0600 {
0601     /* all interior nodes (and only interior nodes) have an overflow node */
0602     if (node->head.overflow == BEFS_BT_INVAL)
0603         return 1;
0604     else
0605         return 0;
0606 }
0607 
0608 /**
0609  * befs_bt_keylen_index - Finds start of keylen index in a node
0610  * @node: Pointer to the node structure to find the keylen index within
0611  *
0612  * Returns a pointer to the start of the key length index array
0613  * of the B+tree node *@node
0614  *
0615  * "The length of all the keys in the node is added to the size of the
0616  * header and then rounded up to a multiple of four to get the beginning
0617  * of the key length index" (p.88, practical filesystem design).
0618  *
0619  * Except that rounding up to 8 works, and rounding up to 4 doesn't.
0620  */
0621 static fs16 *
0622 befs_bt_keylen_index(struct befs_btree_node *node)
0623 {
0624     const int keylen_align = 8;
0625     unsigned long int off =
0626         (sizeof (befs_btree_nodehead) + node->head.all_key_length);
0627     ulong tmp = off % keylen_align;
0628 
0629     if (tmp)
0630         off += keylen_align - tmp;
0631 
0632     return (fs16 *) ((void *) node->od_node + off);
0633 }
0634 
0635 /**
0636  * befs_bt_valarray - Finds the start of value array in a node
0637  * @node: Pointer to the node structure to find the value array within
0638  *
0639  * Returns a pointer to the start of the value array
0640  * of the node pointed to by the node header
0641  */
0642 static fs64 *
0643 befs_bt_valarray(struct befs_btree_node *node)
0644 {
0645     void *keylen_index_start = (void *) befs_bt_keylen_index(node);
0646     size_t keylen_index_size = node->head.all_key_count * sizeof (fs16);
0647 
0648     return (fs64 *) (keylen_index_start + keylen_index_size);
0649 }
0650 
0651 /**
0652  * befs_bt_keydata - Finds start of keydata array in a node
0653  * @node: Pointer to the node structure to find the keydata array within
0654  *
0655  * Returns a pointer to the start of the keydata array
0656  * of the node pointed to by the node header
0657  */
0658 static char *
0659 befs_bt_keydata(struct befs_btree_node *node)
0660 {
0661     return (char *) ((void *) node->od_node + sizeof (befs_btree_nodehead));
0662 }
0663 
0664 /**
0665  * befs_bt_get_key - returns a pointer to the start of a key
0666  * @sb: filesystem superblock
0667  * @node: node in which to look for the key
0668  * @index: the index of the key to get
0669  * @keylen: modified to be the length of the key at @index
0670  *
0671  * Returns a valid pointer into @node on success.
0672  * Returns NULL on failure (bad input) and sets *@keylen = 0
0673  */
0674 static char *
0675 befs_bt_get_key(struct super_block *sb, struct befs_btree_node *node,
0676         int index, u16 * keylen)
0677 {
0678     int prev_key_end;
0679     char *keystart;
0680     fs16 *keylen_index;
0681 
0682     if (index < 0 || index > node->head.all_key_count) {
0683         *keylen = 0;
0684         return NULL;
0685     }
0686 
0687     keystart = befs_bt_keydata(node);
0688     keylen_index = befs_bt_keylen_index(node);
0689 
0690     if (index == 0)
0691         prev_key_end = 0;
0692     else
0693         prev_key_end = fs16_to_cpu(sb, keylen_index[index - 1]);
0694 
0695     *keylen = fs16_to_cpu(sb, keylen_index[index]) - prev_key_end;
0696 
0697     return keystart + prev_key_end;
0698 }
0699 
0700 /**
0701  * befs_compare_strings - compare two strings
0702  * @key1: pointer to the first key to be compared
0703  * @keylen1: length in bytes of key1
0704  * @key2: pointer to the second key to be compared
0705  * @keylen2: length in bytes of key2
0706  *
0707  * Returns 0 if @key1 and @key2 are equal.
0708  * Returns >0 if @key1 is greater.
0709  * Returns <0 if @key2 is greater.
0710  */
0711 static int
0712 befs_compare_strings(const void *key1, int keylen1,
0713              const void *key2, int keylen2)
0714 {
0715     int len = min_t(int, keylen1, keylen2);
0716     int result = strncmp(key1, key2, len);
0717     if (result == 0)
0718         result = keylen1 - keylen2;
0719     return result;
0720 }
0721 
0722 /* These will be used for non-string keyed btrees */
0723 #if 0
0724 static int
0725 btree_compare_int32(cont void *key1, int keylen1, const void *key2, int keylen2)
0726 {
0727     return *(int32_t *) key1 - *(int32_t *) key2;
0728 }
0729 
0730 static int
0731 btree_compare_uint32(cont void *key1, int keylen1,
0732              const void *key2, int keylen2)
0733 {
0734     if (*(u_int32_t *) key1 == *(u_int32_t *) key2)
0735         return 0;
0736     else if (*(u_int32_t *) key1 > *(u_int32_t *) key2)
0737         return 1;
0738 
0739     return -1;
0740 }
0741 static int
0742 btree_compare_int64(cont void *key1, int keylen1, const void *key2, int keylen2)
0743 {
0744     if (*(int64_t *) key1 == *(int64_t *) key2)
0745         return 0;
0746     else if (*(int64_t *) key1 > *(int64_t *) key2)
0747         return 1;
0748 
0749     return -1;
0750 }
0751 
0752 static int
0753 btree_compare_uint64(cont void *key1, int keylen1,
0754              const void *key2, int keylen2)
0755 {
0756     if (*(u_int64_t *) key1 == *(u_int64_t *) key2)
0757         return 0;
0758     else if (*(u_int64_t *) key1 > *(u_int64_t *) key2)
0759         return 1;
0760 
0761     return -1;
0762 }
0763 
0764 static int
0765 btree_compare_float(cont void *key1, int keylen1, const void *key2, int keylen2)
0766 {
0767     float result = *(float *) key1 - *(float *) key2;
0768     if (result == 0.0f)
0769         return 0;
0770 
0771     return (result < 0.0f) ? -1 : 1;
0772 }
0773 
0774 static int
0775 btree_compare_double(cont void *key1, int keylen1,
0776              const void *key2, int keylen2)
0777 {
0778     double result = *(double *) key1 - *(double *) key2;
0779     if (result == 0.0)
0780         return 0;
0781 
0782     return (result < 0.0) ? -1 : 1;
0783 }
0784 #endif              //0