btree.c 22 KB

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