btree.c 22 KB

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