btree.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * linux/fs/hfs/btree.h
  3. *
  4. * Copyright (C) 2001
  5. * Brad Boyer (flar@allandria.com)
  6. * (C) 2003 Ardis Technologies <roman@ardistech.com>
  7. */
  8. #include "hfs_fs.h"
  9. typedef int (*btree_keycmp)(const btree_key *, const btree_key *);
  10. #define NODE_HASH_SIZE 256
  11. /* A HFS BTree held in memory */
  12. struct hfs_btree {
  13. struct super_block *sb;
  14. struct inode *inode;
  15. btree_keycmp keycmp;
  16. u32 cnid;
  17. u32 root;
  18. u32 leaf_count;
  19. u32 leaf_head;
  20. u32 leaf_tail;
  21. u32 node_count;
  22. u32 free_nodes;
  23. u32 attributes;
  24. unsigned int node_size;
  25. unsigned int node_size_shift;
  26. unsigned int max_key_len;
  27. unsigned int depth;
  28. //unsigned int map1_size, map_size;
  29. struct mutex tree_lock;
  30. unsigned int pages_per_bnode;
  31. spinlock_t hash_lock;
  32. struct hfs_bnode *node_hash[NODE_HASH_SIZE];
  33. int node_hash_cnt;
  34. };
  35. /* A HFS BTree node in memory */
  36. struct hfs_bnode {
  37. struct hfs_btree *tree;
  38. u32 prev;
  39. u32 this;
  40. u32 next;
  41. u32 parent;
  42. u16 num_recs;
  43. u8 type;
  44. u8 height;
  45. struct hfs_bnode *next_hash;
  46. unsigned long flags;
  47. wait_queue_head_t lock_wq;
  48. atomic_t refcnt;
  49. unsigned int page_offset;
  50. struct page *page[0];
  51. };
  52. #define HFS_BNODE_ERROR 0
  53. #define HFS_BNODE_NEW 1
  54. #define HFS_BNODE_DELETED 2
  55. struct hfs_find_data {
  56. btree_key *key;
  57. btree_key *search_key;
  58. struct hfs_btree *tree;
  59. struct hfs_bnode *bnode;
  60. int record;
  61. int keyoffset, keylength;
  62. int entryoffset, entrylength;
  63. };
  64. /* btree.c */
  65. extern struct hfs_btree *hfs_btree_open(struct super_block *, u32, btree_keycmp);
  66. extern void hfs_btree_close(struct hfs_btree *);
  67. extern void hfs_btree_write(struct hfs_btree *);
  68. extern struct hfs_bnode * hfs_bmap_alloc(struct hfs_btree *);
  69. extern void hfs_bmap_free(struct hfs_bnode *node);
  70. /* bnode.c */
  71. extern void hfs_bnode_read(struct hfs_bnode *, void *, int, int);
  72. extern u16 hfs_bnode_read_u16(struct hfs_bnode *, int);
  73. extern u8 hfs_bnode_read_u8(struct hfs_bnode *, int);
  74. extern void hfs_bnode_read_key(struct hfs_bnode *, void *, int);
  75. extern void hfs_bnode_write(struct hfs_bnode *, void *, int, int);
  76. extern void hfs_bnode_write_u16(struct hfs_bnode *, int, u16);
  77. extern void hfs_bnode_write_u8(struct hfs_bnode *, int, u8);
  78. extern void hfs_bnode_clear(struct hfs_bnode *, int, int);
  79. extern void hfs_bnode_copy(struct hfs_bnode *, int,
  80. struct hfs_bnode *, int, int);
  81. extern void hfs_bnode_move(struct hfs_bnode *, int, int, int);
  82. extern void hfs_bnode_dump(struct hfs_bnode *);
  83. extern void hfs_bnode_unlink(struct hfs_bnode *);
  84. extern struct hfs_bnode *hfs_bnode_findhash(struct hfs_btree *, u32);
  85. extern struct hfs_bnode *hfs_bnode_find(struct hfs_btree *, u32);
  86. extern void hfs_bnode_unhash(struct hfs_bnode *);
  87. extern void hfs_bnode_free(struct hfs_bnode *);
  88. extern struct hfs_bnode *hfs_bnode_create(struct hfs_btree *, u32);
  89. extern void hfs_bnode_get(struct hfs_bnode *);
  90. extern void hfs_bnode_put(struct hfs_bnode *);
  91. /* brec.c */
  92. extern u16 hfs_brec_lenoff(struct hfs_bnode *, u16, u16 *);
  93. extern u16 hfs_brec_keylen(struct hfs_bnode *, u16);
  94. extern int hfs_brec_insert(struct hfs_find_data *, void *, int);
  95. extern int hfs_brec_remove(struct hfs_find_data *);
  96. /* bfind.c */
  97. extern int hfs_find_init(struct hfs_btree *, struct hfs_find_data *);
  98. extern void hfs_find_exit(struct hfs_find_data *);
  99. extern int __hfs_brec_find(struct hfs_bnode *, struct hfs_find_data *);
  100. extern int hfs_brec_find(struct hfs_find_data *);
  101. extern int hfs_brec_read(struct hfs_find_data *, void *, int);
  102. extern int hfs_brec_goto(struct hfs_find_data *, int);
  103. struct hfs_bnode_desc {
  104. __be32 next; /* (V) Number of the next node at this level */
  105. __be32 prev; /* (V) Number of the prev node at this level */
  106. u8 type; /* (F) The type of node */
  107. u8 height; /* (F) The level of this node (leaves=1) */
  108. __be16 num_recs; /* (V) The number of records in this node */
  109. u16 reserved;
  110. } __packed;
  111. #define HFS_NODE_INDEX 0x00 /* An internal (index) node */
  112. #define HFS_NODE_HEADER 0x01 /* The tree header node (node 0) */
  113. #define HFS_NODE_MAP 0x02 /* Holds part of the bitmap of used nodes */
  114. #define HFS_NODE_LEAF 0xFF /* A leaf (ndNHeight==1) node */
  115. struct hfs_btree_header_rec {
  116. __be16 depth; /* (V) The number of levels in this B-tree */
  117. __be32 root; /* (V) The node number of the root node */
  118. __be32 leaf_count; /* (V) The number of leaf records */
  119. __be32 leaf_head; /* (V) The number of the first leaf node */
  120. __be32 leaf_tail; /* (V) The number of the last leaf node */
  121. __be16 node_size; /* (F) The number of bytes in a node (=512) */
  122. __be16 max_key_len; /* (F) The length of a key in an index node */
  123. __be32 node_count; /* (V) The total number of nodes */
  124. __be32 free_nodes; /* (V) The number of unused nodes */
  125. u16 reserved1;
  126. __be32 clump_size; /* (F) clump size. not usually used. */
  127. u8 btree_type; /* (F) BTree type */
  128. u8 reserved2;
  129. __be32 attributes; /* (F) attributes */
  130. u32 reserved3[16];
  131. } __packed;
  132. #define BTREE_ATTR_BADCLOSE 0x00000001 /* b-tree not closed properly. not
  133. used by hfsplus. */
  134. #define HFS_TREE_BIGKEYS 0x00000002 /* key length is u16 instead of u8.
  135. used by hfsplus. */
  136. #define HFS_TREE_VARIDXKEYS 0x00000004 /* variable key length instead of
  137. max key length. use din catalog
  138. b-tree but not in extents
  139. b-tree (hfsplus). */