Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/befs/btree.c |
| 3 | * |
| 4 | * Copyright (C) 2001-2002 Will Dyson <will_dyson@pobox.com> |
| 5 | * |
| 6 | * Licensed under the GNU GPL. See the file COPYING for details. |
| 7 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 8 | * 2002-02-05: Sergey S. Kostyliov added binary search within |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * btree nodes. |
| 10 | * |
| 11 | * Many thanks to: |
| 12 | * |
| 13 | * Dominic Giampaolo, author of "Practical File System |
| 14 | * Design with the Be File System", for such a helpful book. |
| 15 | * |
| 16 | * Marcus J. Ranum, author of the b+tree package in |
| 17 | * comp.sources.misc volume 10. This code is not copied from that |
| 18 | * work, but it is partially based on it. |
| 19 | * |
| 20 | * Makoto Kato, author of the original BeFS for linux filesystem |
| 21 | * driver. |
| 22 | */ |
| 23 | |
| 24 | #include <linux/kernel.h> |
| 25 | #include <linux/string.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/mm.h> |
| 28 | #include <linux/buffer_head.h> |
| 29 | |
| 30 | #include "befs.h" |
| 31 | #include "btree.h" |
| 32 | #include "datastream.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | /* |
| 35 | * The btree functions in this file are built on top of the |
| 36 | * datastream.c interface, which is in turn built on top of the |
| 37 | * io.c interface. |
| 38 | */ |
| 39 | |
| 40 | /* Befs B+tree structure: |
| 41 | * |
| 42 | * The first thing in the tree is the tree superblock. It tells you |
| 43 | * all kinds of useful things about the tree, like where the rootnode |
| 44 | * is located, and the size of the nodes (always 1024 with current version |
| 45 | * of BeOS). |
| 46 | * |
| 47 | * The rest of the tree consists of a series of nodes. Nodes contain a header |
| 48 | * (struct befs_btree_nodehead), the packed key data, an array of shorts |
| 49 | * containing the ending offsets for each of the keys, and an array of |
| 50 | * befs_off_t values. In interior nodes, the keys are the ending keys for |
| 51 | * the childnode they point to, and the values are offsets into the |
| 52 | * datastream containing the tree. |
| 53 | */ |
| 54 | |
| 55 | /* Note: |
| 56 | * |
| 57 | * The book states 2 confusing things about befs b+trees. First, |
| 58 | * it states that the overflow field of node headers is used by internal nodes |
| 59 | * to point to another node that "effectively continues this one". Here is what |
| 60 | * I believe that means. Each key in internal nodes points to another node that |
| 61 | * contains key values less than itself. Inspection reveals that the last key |
| 62 | * in the internal node is not the last key in the index. Keys that are |
| 63 | * greater than the last key in the internal node go into the overflow node. |
| 64 | * I imagine there is a performance reason for this. |
| 65 | * |
| 66 | * Second, it states that the header of a btree node is sufficient to |
| 67 | * distinguish internal nodes from leaf nodes. Without saying exactly how. |
| 68 | * After figuring out the first, it becomes obvious that internal nodes have |
| 69 | * overflow nodes and leafnodes do not. |
| 70 | */ |
| 71 | |
| 72 | /* |
| 73 | * Currently, this code is only good for directory B+trees. |
| 74 | * In order to be used for other BFS indexes, it needs to be extended to handle |
| 75 | * duplicate keys and non-string keytypes (int32, int64, float, double). |
| 76 | */ |
| 77 | |
| 78 | /* |
| 79 | * In memory structure of each btree node |
| 80 | */ |
Himangi Saraogi | 0f2a84f | 2014-10-13 15:53:18 -0700 | [diff] [blame] | 81 | struct befs_btree_node { |
Al Viro | a9721f3 | 2005-12-24 14:28:55 -0500 | [diff] [blame] | 82 | befs_host_btree_nodehead head; /* head of node converted to cpu byteorder */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | struct buffer_head *bh; |
| 84 | befs_btree_nodehead *od_node; /* on disk node */ |
Himangi Saraogi | 0f2a84f | 2014-10-13 15:53:18 -0700 | [diff] [blame] | 85 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
| 87 | /* local constants */ |
Salah Triki | 4bb5943 | 2016-07-27 04:11:53 +0100 | [diff] [blame] | 88 | static const befs_off_t BEFS_BT_INVAL = 0xffffffffffffffffULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
| 90 | /* local functions */ |
Al Viro | 22341d8 | 2016-05-10 14:24:06 -0400 | [diff] [blame] | 91 | static int befs_btree_seekleaf(struct super_block *sb, const befs_data_stream *ds, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | befs_btree_super * bt_super, |
Himangi Saraogi | 0f2a84f | 2014-10-13 15:53:18 -0700 | [diff] [blame] | 93 | struct befs_btree_node *this_node, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | befs_off_t * node_off); |
| 95 | |
Al Viro | 22341d8 | 2016-05-10 14:24:06 -0400 | [diff] [blame] | 96 | static int befs_bt_read_super(struct super_block *sb, const befs_data_stream *ds, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | befs_btree_super * sup); |
| 98 | |
Al Viro | 22341d8 | 2016-05-10 14:24:06 -0400 | [diff] [blame] | 99 | static int befs_bt_read_node(struct super_block *sb, const befs_data_stream *ds, |
Himangi Saraogi | 0f2a84f | 2014-10-13 15:53:18 -0700 | [diff] [blame] | 100 | struct befs_btree_node *node, |
| 101 | befs_off_t node_off); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
Himangi Saraogi | 0f2a84f | 2014-10-13 15:53:18 -0700 | [diff] [blame] | 103 | static int befs_leafnode(struct befs_btree_node *node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
Himangi Saraogi | 0f2a84f | 2014-10-13 15:53:18 -0700 | [diff] [blame] | 105 | static fs16 *befs_bt_keylen_index(struct befs_btree_node *node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
Himangi Saraogi | 0f2a84f | 2014-10-13 15:53:18 -0700 | [diff] [blame] | 107 | static fs64 *befs_bt_valarray(struct befs_btree_node *node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
Himangi Saraogi | 0f2a84f | 2014-10-13 15:53:18 -0700 | [diff] [blame] | 109 | static char *befs_bt_keydata(struct befs_btree_node *node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
Himangi Saraogi | 0f2a84f | 2014-10-13 15:53:18 -0700 | [diff] [blame] | 111 | static int befs_find_key(struct super_block *sb, |
| 112 | struct befs_btree_node *node, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | const char *findkey, befs_off_t * value); |
| 114 | |
Himangi Saraogi | 0f2a84f | 2014-10-13 15:53:18 -0700 | [diff] [blame] | 115 | static char *befs_bt_get_key(struct super_block *sb, |
| 116 | struct befs_btree_node *node, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | int index, u16 * keylen); |
| 118 | |
| 119 | static int befs_compare_strings(const void *key1, int keylen1, |
| 120 | const void *key2, int keylen2); |
| 121 | |
| 122 | /** |
| 123 | * befs_bt_read_super - read in btree superblock convert to cpu byteorder |
| 124 | * @sb: Filesystem superblock |
| 125 | * @ds: Datastream to read from |
| 126 | * @sup: Buffer in which to place the btree superblock |
| 127 | * |
| 128 | * Calls befs_read_datastream to read in the btree superblock and |
| 129 | * makes sure it is in cpu byteorder, byteswapping if necessary. |
| 130 | * |
| 131 | * On success, returns BEFS_OK and *@sup contains the btree superblock, |
| 132 | * in cpu byte order. |
| 133 | * |
| 134 | * On failure, BEFS_ERR is returned. |
| 135 | */ |
| 136 | static int |
Al Viro | 22341d8 | 2016-05-10 14:24:06 -0400 | [diff] [blame] | 137 | befs_bt_read_super(struct super_block *sb, const befs_data_stream *ds, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | befs_btree_super * sup) |
| 139 | { |
Fabian Frederick | 9f0f564 | 2015-06-25 15:03:43 -0700 | [diff] [blame] | 140 | struct buffer_head *bh; |
| 141 | befs_disk_btree_super *od_sup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
Fabian Frederick | dac52fc | 2014-04-03 14:50:23 -0700 | [diff] [blame] | 143 | befs_debug(sb, "---> %s", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
| 145 | bh = befs_read_datastream(sb, ds, 0, NULL); |
| 146 | |
| 147 | if (!bh) { |
| 148 | befs_error(sb, "Couldn't read index header."); |
| 149 | goto error; |
| 150 | } |
Al Viro | a9721f3 | 2005-12-24 14:28:55 -0500 | [diff] [blame] | 151 | od_sup = (befs_disk_btree_super *) bh->b_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | befs_dump_index_entry(sb, od_sup); |
| 153 | |
| 154 | sup->magic = fs32_to_cpu(sb, od_sup->magic); |
| 155 | sup->node_size = fs32_to_cpu(sb, od_sup->node_size); |
| 156 | sup->max_depth = fs32_to_cpu(sb, od_sup->max_depth); |
| 157 | sup->data_type = fs32_to_cpu(sb, od_sup->data_type); |
| 158 | sup->root_node_ptr = fs64_to_cpu(sb, od_sup->root_node_ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
| 160 | brelse(bh); |
| 161 | if (sup->magic != BEFS_BTREE_MAGIC) { |
| 162 | befs_error(sb, "Index header has bad magic."); |
| 163 | goto error; |
| 164 | } |
| 165 | |
Fabian Frederick | dac52fc | 2014-04-03 14:50:23 -0700 | [diff] [blame] | 166 | befs_debug(sb, "<--- %s", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | return BEFS_OK; |
| 168 | |
| 169 | error: |
Fabian Frederick | dac52fc | 2014-04-03 14:50:23 -0700 | [diff] [blame] | 170 | befs_debug(sb, "<--- %s ERROR", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | return BEFS_ERR; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * befs_bt_read_node - read in btree node and convert to cpu byteorder |
| 176 | * @sb: Filesystem superblock |
| 177 | * @ds: Datastream to read from |
| 178 | * @node: Buffer in which to place the btree node |
| 179 | * @node_off: Starting offset (in bytes) of the node in @ds |
| 180 | * |
| 181 | * Calls befs_read_datastream to read in the indicated btree node and |
| 182 | * makes sure its header fields are in cpu byteorder, byteswapping if |
| 183 | * necessary. |
Luis de Bethencourt | 2dfa8a6 | 2016-07-12 00:02:50 +0100 | [diff] [blame] | 184 | * Note: node->bh must be NULL when this function is called the first time. |
| 185 | * Don't forget brelse(node->bh) after last call. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | * |
| 187 | * On success, returns BEFS_OK and *@node contains the btree node that |
| 188 | * starts at @node_off, with the node->head fields in cpu byte order. |
| 189 | * |
| 190 | * On failure, BEFS_ERR is returned. |
| 191 | */ |
| 192 | |
| 193 | static int |
Al Viro | 22341d8 | 2016-05-10 14:24:06 -0400 | [diff] [blame] | 194 | befs_bt_read_node(struct super_block *sb, const befs_data_stream *ds, |
Himangi Saraogi | 0f2a84f | 2014-10-13 15:53:18 -0700 | [diff] [blame] | 195 | struct befs_btree_node *node, befs_off_t node_off) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | { |
| 197 | uint off = 0; |
| 198 | |
Fabian Frederick | dac52fc | 2014-04-03 14:50:23 -0700 | [diff] [blame] | 199 | befs_debug(sb, "---> %s", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
| 201 | if (node->bh) |
| 202 | brelse(node->bh); |
| 203 | |
| 204 | node->bh = befs_read_datastream(sb, ds, node_off, &off); |
| 205 | if (!node->bh) { |
Fabian Frederick | dac52fc | 2014-04-03 14:50:23 -0700 | [diff] [blame] | 206 | befs_error(sb, "%s failed to read " |
| 207 | "node at %llu", __func__, node_off); |
| 208 | befs_debug(sb, "<--- %s ERROR", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
| 210 | return BEFS_ERR; |
| 211 | } |
| 212 | node->od_node = |
| 213 | (befs_btree_nodehead *) ((void *) node->bh->b_data + off); |
| 214 | |
| 215 | befs_dump_index_node(sb, node->od_node); |
| 216 | |
| 217 | node->head.left = fs64_to_cpu(sb, node->od_node->left); |
| 218 | node->head.right = fs64_to_cpu(sb, node->od_node->right); |
| 219 | node->head.overflow = fs64_to_cpu(sb, node->od_node->overflow); |
| 220 | node->head.all_key_count = |
| 221 | fs16_to_cpu(sb, node->od_node->all_key_count); |
| 222 | node->head.all_key_length = |
| 223 | fs16_to_cpu(sb, node->od_node->all_key_length); |
| 224 | |
Fabian Frederick | dac52fc | 2014-04-03 14:50:23 -0700 | [diff] [blame] | 225 | befs_debug(sb, "<--- %s", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | return BEFS_OK; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * befs_btree_find - Find a key in a befs B+tree |
| 231 | * @sb: Filesystem superblock |
| 232 | * @ds: Datastream containing btree |
| 233 | * @key: Key string to lookup in btree |
| 234 | * @value: Value stored with @key |
| 235 | * |
Joe Perches | c78bad1 | 2008-02-03 17:33:42 +0200 | [diff] [blame] | 236 | * On success, returns BEFS_OK and sets *@value to the value stored |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | * with @key (usually the disk block number of an inode). |
| 238 | * |
| 239 | * On failure, returns BEFS_ERR or BEFS_BT_NOT_FOUND. |
| 240 | * |
| 241 | * Algorithm: |
| 242 | * Read the superblock and rootnode of the b+tree. |
| 243 | * Drill down through the interior nodes using befs_find_key(). |
| 244 | * Once at the correct leaf node, use befs_find_key() again to get the |
| 245 | * actuall value stored with the key. |
| 246 | */ |
| 247 | int |
Al Viro | 22341d8 | 2016-05-10 14:24:06 -0400 | [diff] [blame] | 248 | befs_btree_find(struct super_block *sb, const befs_data_stream *ds, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | const char *key, befs_off_t * value) |
| 250 | { |
Fabian Frederick | 9f0f564 | 2015-06-25 15:03:43 -0700 | [diff] [blame] | 251 | struct befs_btree_node *this_node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | befs_btree_super bt_super; |
| 253 | befs_off_t node_off; |
| 254 | int res; |
| 255 | |
Fabian Frederick | dac52fc | 2014-04-03 14:50:23 -0700 | [diff] [blame] | 256 | befs_debug(sb, "---> %s Key: %s", __func__, key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
| 258 | if (befs_bt_read_super(sb, ds, &bt_super) != BEFS_OK) { |
| 259 | befs_error(sb, |
| 260 | "befs_btree_find() failed to read index superblock"); |
| 261 | goto error; |
| 262 | } |
| 263 | |
Himangi Saraogi | 0f2a84f | 2014-10-13 15:53:18 -0700 | [diff] [blame] | 264 | this_node = kmalloc(sizeof(struct befs_btree_node), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | GFP_NOFS); |
| 266 | if (!this_node) { |
Fabian Frederick | dac52fc | 2014-04-03 14:50:23 -0700 | [diff] [blame] | 267 | befs_error(sb, "befs_btree_find() failed to allocate %zu " |
Himangi Saraogi | 0f2a84f | 2014-10-13 15:53:18 -0700 | [diff] [blame] | 268 | "bytes of memory", sizeof(struct befs_btree_node)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | goto error; |
| 270 | } |
| 271 | |
| 272 | this_node->bh = NULL; |
| 273 | |
| 274 | /* read in root node */ |
| 275 | node_off = bt_super.root_node_ptr; |
| 276 | if (befs_bt_read_node(sb, ds, this_node, node_off) != BEFS_OK) { |
| 277 | befs_error(sb, "befs_btree_find() failed to read " |
Fabian Frederick | dac52fc | 2014-04-03 14:50:23 -0700 | [diff] [blame] | 278 | "node at %llu", node_off); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | goto error_alloc; |
| 280 | } |
| 281 | |
| 282 | while (!befs_leafnode(this_node)) { |
| 283 | res = befs_find_key(sb, this_node, key, &node_off); |
Luis de Bethencourt | 672a851 | 2016-08-08 15:21:20 +0100 | [diff] [blame] | 284 | /* if no key set, try the overflow node */ |
| 285 | if (res == BEFS_BT_OVERFLOW) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | node_off = this_node->head.overflow; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | if (befs_bt_read_node(sb, ds, this_node, node_off) != BEFS_OK) { |
| 288 | befs_error(sb, "befs_btree_find() failed to read " |
Fabian Frederick | dac52fc | 2014-04-03 14:50:23 -0700 | [diff] [blame] | 289 | "node at %llu", node_off); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | goto error_alloc; |
| 291 | } |
| 292 | } |
| 293 | |
Luis de Bethencourt | 672a851 | 2016-08-08 15:21:20 +0100 | [diff] [blame] | 294 | /* at a leaf node now, check if it is correct */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | res = befs_find_key(sb, this_node, key, value); |
| 296 | |
| 297 | brelse(this_node->bh); |
| 298 | kfree(this_node); |
| 299 | |
| 300 | if (res != BEFS_BT_MATCH) { |
Luis de Bethencourt | 4c3897c | 2016-07-03 16:29:44 +0100 | [diff] [blame] | 301 | befs_error(sb, "<--- %s Key %s not found", __func__, key); |
| 302 | befs_debug(sb, "<--- %s ERROR", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | *value = 0; |
| 304 | return BEFS_BT_NOT_FOUND; |
| 305 | } |
Fabian Frederick | dac52fc | 2014-04-03 14:50:23 -0700 | [diff] [blame] | 306 | befs_debug(sb, "<--- %s Found key %s, value %llu", __func__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | key, *value); |
| 308 | return BEFS_OK; |
| 309 | |
| 310 | error_alloc: |
| 311 | kfree(this_node); |
| 312 | error: |
| 313 | *value = 0; |
Fabian Frederick | dac52fc | 2014-04-03 14:50:23 -0700 | [diff] [blame] | 314 | befs_debug(sb, "<--- %s ERROR", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | return BEFS_ERR; |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * befs_find_key - Search for a key within a node |
| 320 | * @sb: Filesystem superblock |
| 321 | * @node: Node to find the key within |
Fabian Frederick | 817e1d9 | 2014-06-06 14:36:17 -0700 | [diff] [blame] | 322 | * @findkey: Keystring to search for |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | * @value: If key is found, the value stored with the key is put here |
| 324 | * |
Luis de Bethencourt | 672a851 | 2016-08-08 15:21:20 +0100 | [diff] [blame] | 325 | * Finds exact match if one exists, and returns BEFS_BT_MATCH. |
| 326 | * If there is no match and node's value array is too small for key, return |
| 327 | * BEFS_BT_OVERFLOW. |
| 328 | * If no match and node should countain this key, return BEFS_BT_NOT_FOUND. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | * |
Luis de Bethencourt | 672a851 | 2016-08-08 15:21:20 +0100 | [diff] [blame] | 330 | * Uses binary search instead of a linear. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | */ |
| 332 | static int |
Himangi Saraogi | 0f2a84f | 2014-10-13 15:53:18 -0700 | [diff] [blame] | 333 | befs_find_key(struct super_block *sb, struct befs_btree_node *node, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | const char *findkey, befs_off_t * value) |
| 335 | { |
| 336 | int first, last, mid; |
| 337 | int eq; |
| 338 | u16 keylen; |
| 339 | int findkey_len; |
| 340 | char *thiskey; |
Al Viro | a9721f3 | 2005-12-24 14:28:55 -0500 | [diff] [blame] | 341 | fs64 *valarray; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | |
Fabian Frederick | dac52fc | 2014-04-03 14:50:23 -0700 | [diff] [blame] | 343 | befs_debug(sb, "---> %s %s", __func__, findkey); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | findkey_len = strlen(findkey); |
| 346 | |
Luis de Bethencourt | bb75e66 | 2016-08-08 15:21:21 +0100 | [diff] [blame^] | 347 | /* if node can not contain key, just skip this node */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | last = node->head.all_key_count - 1; |
| 349 | thiskey = befs_bt_get_key(sb, node, last, &keylen); |
| 350 | |
| 351 | eq = befs_compare_strings(thiskey, keylen, findkey, findkey_len); |
| 352 | if (eq < 0) { |
Luis de Bethencourt | 672a851 | 2016-08-08 15:21:20 +0100 | [diff] [blame] | 353 | befs_debug(sb, "<--- node can't contain %s", findkey); |
| 354 | return BEFS_BT_OVERFLOW; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | valarray = befs_bt_valarray(node); |
| 358 | |
| 359 | /* simple binary search */ |
| 360 | first = 0; |
| 361 | mid = 0; |
| 362 | while (last >= first) { |
| 363 | mid = (last + first) / 2; |
| 364 | befs_debug(sb, "first: %d, last: %d, mid: %d", first, last, |
| 365 | mid); |
| 366 | thiskey = befs_bt_get_key(sb, node, mid, &keylen); |
| 367 | eq = befs_compare_strings(thiskey, keylen, findkey, |
| 368 | findkey_len); |
| 369 | |
| 370 | if (eq == 0) { |
Fabian Frederick | dac52fc | 2014-04-03 14:50:23 -0700 | [diff] [blame] | 371 | befs_debug(sb, "<--- %s found %s at %d", |
| 372 | __func__, thiskey, mid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
| 374 | *value = fs64_to_cpu(sb, valarray[mid]); |
| 375 | return BEFS_BT_MATCH; |
| 376 | } |
| 377 | if (eq > 0) |
| 378 | last = mid - 1; |
| 379 | else |
| 380 | first = mid + 1; |
| 381 | } |
Luis de Bethencourt | 672a851 | 2016-08-08 15:21:20 +0100 | [diff] [blame] | 382 | |
| 383 | /* return an existing value so caller can arrive to a leaf node */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | if (eq < 0) |
| 385 | *value = fs64_to_cpu(sb, valarray[mid + 1]); |
| 386 | else |
| 387 | *value = fs64_to_cpu(sb, valarray[mid]); |
Luis de Bethencourt | 672a851 | 2016-08-08 15:21:20 +0100 | [diff] [blame] | 388 | befs_error(sb, "<--- %s %s not found", __func__, findkey); |
| 389 | befs_debug(sb, "<--- %s ERROR", __func__); |
| 390 | return BEFS_BT_NOT_FOUND; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | /** |
| 394 | * befs_btree_read - Traverse leafnodes of a btree |
| 395 | * @sb: Filesystem superblock |
| 396 | * @ds: Datastream containing btree |
| 397 | * @key_no: Key number (alphabetical order) of key to read |
| 398 | * @bufsize: Size of the buffer to return key in |
| 399 | * @keybuf: Pointer to a buffer to put the key in |
| 400 | * @keysize: Length of the returned key |
| 401 | * @value: Value stored with the returned key |
| 402 | * |
| 403 | * Heres how it works: Key_no is the index of the key/value pair to |
| 404 | * return in keybuf/value. |
| 405 | * Bufsize is the size of keybuf (BEFS_NAME_LEN+1 is a good size). Keysize is |
Fabian Frederick | 6cb103b | 2014-06-06 14:36:15 -0700 | [diff] [blame] | 406 | * the number of characters in the key (just a convenience). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | * |
| 408 | * Algorithm: |
| 409 | * Get the first leafnode of the tree. See if the requested key is in that |
| 410 | * node. If not, follow the node->right link to the next leafnode. Repeat |
| 411 | * until the (key_no)th key is found or the tree is out of keys. |
| 412 | */ |
| 413 | int |
Al Viro | 22341d8 | 2016-05-10 14:24:06 -0400 | [diff] [blame] | 414 | befs_btree_read(struct super_block *sb, const befs_data_stream *ds, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | loff_t key_no, size_t bufsize, char *keybuf, size_t * keysize, |
| 416 | befs_off_t * value) |
| 417 | { |
Himangi Saraogi | 0f2a84f | 2014-10-13 15:53:18 -0700 | [diff] [blame] | 418 | struct befs_btree_node *this_node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | befs_btree_super bt_super; |
Salah Triki | 143d2a6 | 2016-07-31 21:34:28 +0100 | [diff] [blame] | 420 | befs_off_t node_off; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | int cur_key; |
Al Viro | a9721f3 | 2005-12-24 14:28:55 -0500 | [diff] [blame] | 422 | fs64 *valarray; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | char *keystart; |
| 424 | u16 keylen; |
| 425 | int res; |
| 426 | |
| 427 | uint key_sum = 0; |
| 428 | |
Fabian Frederick | dac52fc | 2014-04-03 14:50:23 -0700 | [diff] [blame] | 429 | befs_debug(sb, "---> %s", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | |
| 431 | if (befs_bt_read_super(sb, ds, &bt_super) != BEFS_OK) { |
| 432 | befs_error(sb, |
| 433 | "befs_btree_read() failed to read index superblock"); |
| 434 | goto error; |
| 435 | } |
| 436 | |
Himangi Saraogi | 0f2a84f | 2014-10-13 15:53:18 -0700 | [diff] [blame] | 437 | this_node = kmalloc(sizeof(struct befs_btree_node), GFP_NOFS); |
| 438 | if (this_node == NULL) { |
Fabian Frederick | dac52fc | 2014-04-03 14:50:23 -0700 | [diff] [blame] | 439 | befs_error(sb, "befs_btree_read() failed to allocate %zu " |
Himangi Saraogi | 0f2a84f | 2014-10-13 15:53:18 -0700 | [diff] [blame] | 440 | "bytes of memory", sizeof(struct befs_btree_node)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | goto error; |
| 442 | } |
| 443 | |
| 444 | node_off = bt_super.root_node_ptr; |
| 445 | this_node->bh = NULL; |
| 446 | |
| 447 | /* seeks down to first leafnode, reads it into this_node */ |
| 448 | res = befs_btree_seekleaf(sb, ds, &bt_super, this_node, &node_off); |
| 449 | if (res == BEFS_BT_EMPTY) { |
| 450 | brelse(this_node->bh); |
| 451 | kfree(this_node); |
| 452 | *value = 0; |
| 453 | *keysize = 0; |
Fabian Frederick | dac52fc | 2014-04-03 14:50:23 -0700 | [diff] [blame] | 454 | befs_debug(sb, "<--- %s Tree is EMPTY", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | return BEFS_BT_EMPTY; |
| 456 | } else if (res == BEFS_ERR) { |
| 457 | goto error_alloc; |
| 458 | } |
| 459 | |
| 460 | /* find the leaf node containing the key_no key */ |
| 461 | |
| 462 | while (key_sum + this_node->head.all_key_count <= key_no) { |
| 463 | |
| 464 | /* no more nodes to look in: key_no is too large */ |
Salah Triki | 4bb5943 | 2016-07-27 04:11:53 +0100 | [diff] [blame] | 465 | if (this_node->head.right == BEFS_BT_INVAL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | *keysize = 0; |
| 467 | *value = 0; |
| 468 | befs_debug(sb, |
Fabian Frederick | dac52fc | 2014-04-03 14:50:23 -0700 | [diff] [blame] | 469 | "<--- %s END of keys at %llu", __func__, |
| 470 | (unsigned long long) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | key_sum + this_node->head.all_key_count); |
| 472 | brelse(this_node->bh); |
| 473 | kfree(this_node); |
| 474 | return BEFS_BT_END; |
| 475 | } |
| 476 | |
| 477 | key_sum += this_node->head.all_key_count; |
| 478 | node_off = this_node->head.right; |
| 479 | |
| 480 | if (befs_bt_read_node(sb, ds, this_node, node_off) != BEFS_OK) { |
Fabian Frederick | dac52fc | 2014-04-03 14:50:23 -0700 | [diff] [blame] | 481 | befs_error(sb, "%s failed to read node at %llu", |
| 482 | __func__, (unsigned long long)node_off); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | goto error_alloc; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | /* how many keys into this_node is key_no */ |
| 488 | cur_key = key_no - key_sum; |
| 489 | |
| 490 | /* get pointers to datastructures within the node body */ |
| 491 | valarray = befs_bt_valarray(this_node); |
| 492 | |
| 493 | keystart = befs_bt_get_key(sb, this_node, cur_key, &keylen); |
| 494 | |
Fabian Frederick | dac52fc | 2014-04-03 14:50:23 -0700 | [diff] [blame] | 495 | befs_debug(sb, "Read [%llu,%d]: keysize %d", |
| 496 | (long long unsigned int)node_off, (int)cur_key, |
| 497 | (int)keylen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | |
| 499 | if (bufsize < keylen + 1) { |
Fabian Frederick | dac52fc | 2014-04-03 14:50:23 -0700 | [diff] [blame] | 500 | befs_error(sb, "%s keybuf too small (%zu) " |
| 501 | "for key of size %d", __func__, bufsize, keylen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | brelse(this_node->bh); |
| 503 | goto error_alloc; |
Fabian Frederick | 6cb103b | 2014-06-06 14:36:15 -0700 | [diff] [blame] | 504 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | |
Fabian Frederick | 6cb103b | 2014-06-06 14:36:15 -0700 | [diff] [blame] | 506 | strlcpy(keybuf, keystart, keylen + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | *value = fs64_to_cpu(sb, valarray[cur_key]); |
| 508 | *keysize = keylen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | |
Fabian Frederick | dac52fc | 2014-04-03 14:50:23 -0700 | [diff] [blame] | 510 | befs_debug(sb, "Read [%llu,%d]: Key \"%.*s\", Value %llu", node_off, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | cur_key, keylen, keybuf, *value); |
| 512 | |
| 513 | brelse(this_node->bh); |
| 514 | kfree(this_node); |
| 515 | |
Fabian Frederick | dac52fc | 2014-04-03 14:50:23 -0700 | [diff] [blame] | 516 | befs_debug(sb, "<--- %s", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | |
| 518 | return BEFS_OK; |
| 519 | |
| 520 | error_alloc: |
| 521 | kfree(this_node); |
| 522 | |
| 523 | error: |
| 524 | *keysize = 0; |
| 525 | *value = 0; |
Fabian Frederick | dac52fc | 2014-04-03 14:50:23 -0700 | [diff] [blame] | 526 | befs_debug(sb, "<--- %s ERROR", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | return BEFS_ERR; |
| 528 | } |
| 529 | |
| 530 | /** |
| 531 | * befs_btree_seekleaf - Find the first leafnode in the btree |
| 532 | * @sb: Filesystem superblock |
| 533 | * @ds: Datastream containing btree |
| 534 | * @bt_super: Pointer to the superblock of the btree |
| 535 | * @this_node: Buffer to return the leafnode in |
| 536 | * @node_off: Pointer to offset of current node within datastream. Modified |
| 537 | * by the function. |
| 538 | * |
| 539 | * |
| 540 | * Helper function for btree traverse. Moves the current position to the |
| 541 | * start of the first leaf node. |
| 542 | * |
| 543 | * Also checks for an empty tree. If there are no keys, returns BEFS_BT_EMPTY. |
| 544 | */ |
| 545 | static int |
Al Viro | 22341d8 | 2016-05-10 14:24:06 -0400 | [diff] [blame] | 546 | befs_btree_seekleaf(struct super_block *sb, const befs_data_stream *ds, |
Himangi Saraogi | 0f2a84f | 2014-10-13 15:53:18 -0700 | [diff] [blame] | 547 | befs_btree_super *bt_super, |
| 548 | struct befs_btree_node *this_node, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | befs_off_t * node_off) |
| 550 | { |
| 551 | |
Fabian Frederick | dac52fc | 2014-04-03 14:50:23 -0700 | [diff] [blame] | 552 | befs_debug(sb, "---> %s", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | |
| 554 | if (befs_bt_read_node(sb, ds, this_node, *node_off) != BEFS_OK) { |
Fabian Frederick | dac52fc | 2014-04-03 14:50:23 -0700 | [diff] [blame] | 555 | befs_error(sb, "%s failed to read " |
| 556 | "node at %llu", __func__, *node_off); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | goto error; |
| 558 | } |
Fabian Frederick | dac52fc | 2014-04-03 14:50:23 -0700 | [diff] [blame] | 559 | befs_debug(sb, "Seekleaf to root node %llu", *node_off); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | |
| 561 | if (this_node->head.all_key_count == 0 && befs_leafnode(this_node)) { |
Fabian Frederick | dac52fc | 2014-04-03 14:50:23 -0700 | [diff] [blame] | 562 | befs_debug(sb, "<--- %s Tree is EMPTY", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | return BEFS_BT_EMPTY; |
| 564 | } |
| 565 | |
| 566 | while (!befs_leafnode(this_node)) { |
| 567 | |
| 568 | if (this_node->head.all_key_count == 0) { |
Fabian Frederick | dac52fc | 2014-04-03 14:50:23 -0700 | [diff] [blame] | 569 | befs_debug(sb, "%s encountered " |
| 570 | "an empty interior node: %llu. Using Overflow " |
| 571 | "node: %llu", __func__, *node_off, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | this_node->head.overflow); |
| 573 | *node_off = this_node->head.overflow; |
| 574 | } else { |
Al Viro | a9721f3 | 2005-12-24 14:28:55 -0500 | [diff] [blame] | 575 | fs64 *valarray = befs_bt_valarray(this_node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | *node_off = fs64_to_cpu(sb, valarray[0]); |
| 577 | } |
| 578 | if (befs_bt_read_node(sb, ds, this_node, *node_off) != BEFS_OK) { |
Fabian Frederick | dac52fc | 2014-04-03 14:50:23 -0700 | [diff] [blame] | 579 | befs_error(sb, "%s failed to read " |
| 580 | "node at %llu", __func__, *node_off); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | goto error; |
| 582 | } |
| 583 | |
Fabian Frederick | dac52fc | 2014-04-03 14:50:23 -0700 | [diff] [blame] | 584 | befs_debug(sb, "Seekleaf to child node %llu", *node_off); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | } |
Fabian Frederick | dac52fc | 2014-04-03 14:50:23 -0700 | [diff] [blame] | 586 | befs_debug(sb, "Node %llu is a leaf node", *node_off); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | |
| 588 | return BEFS_OK; |
| 589 | |
| 590 | error: |
Fabian Frederick | dac52fc | 2014-04-03 14:50:23 -0700 | [diff] [blame] | 591 | befs_debug(sb, "<--- %s ERROR", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | return BEFS_ERR; |
| 593 | } |
| 594 | |
| 595 | /** |
| 596 | * befs_leafnode - Determine if the btree node is a leaf node or an |
| 597 | * interior node |
| 598 | * @node: Pointer to node structure to test |
| 599 | * |
| 600 | * Return 1 if leaf, 0 if interior |
| 601 | */ |
| 602 | static int |
Himangi Saraogi | 0f2a84f | 2014-10-13 15:53:18 -0700 | [diff] [blame] | 603 | befs_leafnode(struct befs_btree_node *node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | { |
| 605 | /* all interior nodes (and only interior nodes) have an overflow node */ |
Salah Triki | 4bb5943 | 2016-07-27 04:11:53 +0100 | [diff] [blame] | 606 | if (node->head.overflow == BEFS_BT_INVAL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | return 1; |
| 608 | else |
| 609 | return 0; |
| 610 | } |
| 611 | |
| 612 | /** |
| 613 | * befs_bt_keylen_index - Finds start of keylen index in a node |
| 614 | * @node: Pointer to the node structure to find the keylen index within |
| 615 | * |
| 616 | * Returns a pointer to the start of the key length index array |
| 617 | * of the B+tree node *@node |
| 618 | * |
| 619 | * "The length of all the keys in the node is added to the size of the |
| 620 | * header and then rounded up to a multiple of four to get the beginning |
| 621 | * of the key length index" (p.88, practical filesystem design). |
| 622 | * |
| 623 | * Except that rounding up to 8 works, and rounding up to 4 doesn't. |
| 624 | */ |
Al Viro | a9721f3 | 2005-12-24 14:28:55 -0500 | [diff] [blame] | 625 | static fs16 * |
Himangi Saraogi | 0f2a84f | 2014-10-13 15:53:18 -0700 | [diff] [blame] | 626 | befs_bt_keylen_index(struct befs_btree_node *node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | { |
| 628 | const int keylen_align = 8; |
| 629 | unsigned long int off = |
| 630 | (sizeof (befs_btree_nodehead) + node->head.all_key_length); |
| 631 | ulong tmp = off % keylen_align; |
| 632 | |
| 633 | if (tmp) |
| 634 | off += keylen_align - tmp; |
| 635 | |
Al Viro | a9721f3 | 2005-12-24 14:28:55 -0500 | [diff] [blame] | 636 | return (fs16 *) ((void *) node->od_node + off); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | /** |
| 640 | * befs_bt_valarray - Finds the start of value array in a node |
| 641 | * @node: Pointer to the node structure to find the value array within |
| 642 | * |
| 643 | * Returns a pointer to the start of the value array |
| 644 | * of the node pointed to by the node header |
| 645 | */ |
Al Viro | a9721f3 | 2005-12-24 14:28:55 -0500 | [diff] [blame] | 646 | static fs64 * |
Himangi Saraogi | 0f2a84f | 2014-10-13 15:53:18 -0700 | [diff] [blame] | 647 | befs_bt_valarray(struct befs_btree_node *node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | { |
| 649 | void *keylen_index_start = (void *) befs_bt_keylen_index(node); |
Al Viro | a9721f3 | 2005-12-24 14:28:55 -0500 | [diff] [blame] | 650 | size_t keylen_index_size = node->head.all_key_count * sizeof (fs16); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | |
Al Viro | a9721f3 | 2005-12-24 14:28:55 -0500 | [diff] [blame] | 652 | return (fs64 *) (keylen_index_start + keylen_index_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | /** |
| 656 | * befs_bt_keydata - Finds start of keydata array in a node |
| 657 | * @node: Pointer to the node structure to find the keydata array within |
| 658 | * |
| 659 | * Returns a pointer to the start of the keydata array |
| 660 | * of the node pointed to by the node header |
| 661 | */ |
| 662 | static char * |
Himangi Saraogi | 0f2a84f | 2014-10-13 15:53:18 -0700 | [diff] [blame] | 663 | befs_bt_keydata(struct befs_btree_node *node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | { |
| 665 | return (char *) ((void *) node->od_node + sizeof (befs_btree_nodehead)); |
| 666 | } |
| 667 | |
| 668 | /** |
| 669 | * befs_bt_get_key - returns a pointer to the start of a key |
| 670 | * @sb: filesystem superblock |
| 671 | * @node: node in which to look for the key |
| 672 | * @index: the index of the key to get |
| 673 | * @keylen: modified to be the length of the key at @index |
| 674 | * |
| 675 | * Returns a valid pointer into @node on success. |
| 676 | * Returns NULL on failure (bad input) and sets *@keylen = 0 |
| 677 | */ |
| 678 | static char * |
Himangi Saraogi | 0f2a84f | 2014-10-13 15:53:18 -0700 | [diff] [blame] | 679 | befs_bt_get_key(struct super_block *sb, struct befs_btree_node *node, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | int index, u16 * keylen) |
| 681 | { |
| 682 | int prev_key_end; |
| 683 | char *keystart; |
Al Viro | a9721f3 | 2005-12-24 14:28:55 -0500 | [diff] [blame] | 684 | fs16 *keylen_index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | |
| 686 | if (index < 0 || index > node->head.all_key_count) { |
| 687 | *keylen = 0; |
| 688 | return NULL; |
| 689 | } |
| 690 | |
| 691 | keystart = befs_bt_keydata(node); |
| 692 | keylen_index = befs_bt_keylen_index(node); |
| 693 | |
| 694 | if (index == 0) |
| 695 | prev_key_end = 0; |
| 696 | else |
| 697 | prev_key_end = fs16_to_cpu(sb, keylen_index[index - 1]); |
| 698 | |
| 699 | *keylen = fs16_to_cpu(sb, keylen_index[index]) - prev_key_end; |
| 700 | |
| 701 | return keystart + prev_key_end; |
| 702 | } |
| 703 | |
| 704 | /** |
| 705 | * befs_compare_strings - compare two strings |
| 706 | * @key1: pointer to the first key to be compared |
| 707 | * @keylen1: length in bytes of key1 |
| 708 | * @key2: pointer to the second key to be compared |
Fabian Frederick | 817e1d9 | 2014-06-06 14:36:17 -0700 | [diff] [blame] | 709 | * @keylen2: length in bytes of key2 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | * |
| 711 | * Returns 0 if @key1 and @key2 are equal. |
| 712 | * Returns >0 if @key1 is greater. |
| 713 | * Returns <0 if @key2 is greater.. |
| 714 | */ |
| 715 | static int |
| 716 | befs_compare_strings(const void *key1, int keylen1, |
| 717 | const void *key2, int keylen2) |
| 718 | { |
| 719 | int len = min_t(int, keylen1, keylen2); |
| 720 | int result = strncmp(key1, key2, len); |
| 721 | if (result == 0) |
| 722 | result = keylen1 - keylen2; |
| 723 | return result; |
| 724 | } |
| 725 | |
| 726 | /* These will be used for non-string keyed btrees */ |
| 727 | #if 0 |
| 728 | static int |
| 729 | btree_compare_int32(cont void *key1, int keylen1, const void *key2, int keylen2) |
| 730 | { |
| 731 | return *(int32_t *) key1 - *(int32_t *) key2; |
| 732 | } |
| 733 | |
| 734 | static int |
| 735 | btree_compare_uint32(cont void *key1, int keylen1, |
| 736 | const void *key2, int keylen2) |
| 737 | { |
| 738 | if (*(u_int32_t *) key1 == *(u_int32_t *) key2) |
| 739 | return 0; |
| 740 | else if (*(u_int32_t *) key1 > *(u_int32_t *) key2) |
| 741 | return 1; |
| 742 | |
| 743 | return -1; |
| 744 | } |
| 745 | static int |
| 746 | btree_compare_int64(cont void *key1, int keylen1, const void *key2, int keylen2) |
| 747 | { |
| 748 | if (*(int64_t *) key1 == *(int64_t *) key2) |
| 749 | return 0; |
| 750 | else if (*(int64_t *) key1 > *(int64_t *) key2) |
| 751 | return 1; |
| 752 | |
| 753 | return -1; |
| 754 | } |
| 755 | |
| 756 | static int |
| 757 | btree_compare_uint64(cont void *key1, int keylen1, |
| 758 | const void *key2, int keylen2) |
| 759 | { |
| 760 | if (*(u_int64_t *) key1 == *(u_int64_t *) key2) |
| 761 | return 0; |
| 762 | else if (*(u_int64_t *) key1 > *(u_int64_t *) key2) |
| 763 | return 1; |
| 764 | |
| 765 | return -1; |
| 766 | } |
| 767 | |
| 768 | static int |
| 769 | btree_compare_float(cont void *key1, int keylen1, const void *key2, int keylen2) |
| 770 | { |
| 771 | float result = *(float *) key1 - *(float *) key2; |
| 772 | if (result == 0.0f) |
| 773 | return 0; |
| 774 | |
| 775 | return (result < 0.0f) ? -1 : 1; |
| 776 | } |
| 777 | |
| 778 | static int |
| 779 | btree_compare_double(cont void *key1, int keylen1, |
| 780 | const void *key2, int keylen2) |
| 781 | { |
| 782 | double result = *(double *) key1 - *(double *) key2; |
| 783 | if (result == 0.0) |
| 784 | return 0; |
| 785 | |
| 786 | return (result < 0.0) ? -1 : 1; |
| 787 | } |
| 788 | #endif //0 |