1 /* SPDX-License-Identifier: GPL-2.0 */
6 * Brad Boyer (flar@allandria.com)
7 * (C) 2003 Ardis Technologies <roman@ardistech.com>
12 typedef int (*btree_keycmp
)(const btree_key
*, const btree_key
*);
14 #define NODE_HASH_SIZE 256
16 /* A HFS BTree held in memory */
18 struct super_block
*sb
;
31 unsigned int node_size
;
32 unsigned int node_size_shift
;
33 unsigned int max_key_len
;
36 //unsigned int map1_size, map_size;
37 struct mutex tree_lock
;
39 unsigned int pages_per_bnode
;
41 struct hfs_bnode
*node_hash
[NODE_HASH_SIZE
];
45 /* A HFS BTree node in memory */
47 struct hfs_btree
*tree
;
58 struct hfs_bnode
*next_hash
;
60 wait_queue_head_t lock_wq
;
62 unsigned int page_offset
;
66 #define HFS_BNODE_ERROR 0
67 #define HFS_BNODE_NEW 1
68 #define HFS_BNODE_DELETED 2
70 struct hfs_find_data
{
72 btree_key
*search_key
;
73 struct hfs_btree
*tree
;
74 struct hfs_bnode
*bnode
;
76 int keyoffset
, keylength
;
77 int entryoffset
, entrylength
;
82 extern struct hfs_btree
*hfs_btree_open(struct super_block
*, u32
, btree_keycmp
);
83 extern void hfs_btree_close(struct hfs_btree
*);
84 extern void hfs_btree_write(struct hfs_btree
*);
85 extern struct hfs_bnode
* hfs_bmap_alloc(struct hfs_btree
*);
86 extern void hfs_bmap_free(struct hfs_bnode
*node
);
89 extern void hfs_bnode_read(struct hfs_bnode
*, void *, int, int);
90 extern u16
hfs_bnode_read_u16(struct hfs_bnode
*, int);
91 extern u8
hfs_bnode_read_u8(struct hfs_bnode
*, int);
92 extern void hfs_bnode_read_key(struct hfs_bnode
*, void *, int);
93 extern void hfs_bnode_write(struct hfs_bnode
*, void *, int, int);
94 extern void hfs_bnode_write_u16(struct hfs_bnode
*, int, u16
);
95 extern void hfs_bnode_write_u8(struct hfs_bnode
*, int, u8
);
96 extern void hfs_bnode_clear(struct hfs_bnode
*, int, int);
97 extern void hfs_bnode_copy(struct hfs_bnode
*, int,
98 struct hfs_bnode
*, int, int);
99 extern void hfs_bnode_move(struct hfs_bnode
*, int, int, int);
100 extern void hfs_bnode_dump(struct hfs_bnode
*);
101 extern void hfs_bnode_unlink(struct hfs_bnode
*);
102 extern struct hfs_bnode
*hfs_bnode_findhash(struct hfs_btree
*, u32
);
103 extern struct hfs_bnode
*hfs_bnode_find(struct hfs_btree
*, u32
);
104 extern void hfs_bnode_unhash(struct hfs_bnode
*);
105 extern void hfs_bnode_free(struct hfs_bnode
*);
106 extern struct hfs_bnode
*hfs_bnode_create(struct hfs_btree
*, u32
);
107 extern void hfs_bnode_get(struct hfs_bnode
*);
108 extern void hfs_bnode_put(struct hfs_bnode
*);
111 extern u16
hfs_brec_lenoff(struct hfs_bnode
*, u16
, u16
*);
112 extern u16
hfs_brec_keylen(struct hfs_bnode
*, u16
);
113 extern int hfs_brec_insert(struct hfs_find_data
*, void *, int);
114 extern int hfs_brec_remove(struct hfs_find_data
*);
117 extern int hfs_find_init(struct hfs_btree
*, struct hfs_find_data
*);
118 extern void hfs_find_exit(struct hfs_find_data
*);
119 extern int __hfs_brec_find(struct hfs_bnode
*, struct hfs_find_data
*);
120 extern int hfs_brec_find(struct hfs_find_data
*);
121 extern int hfs_brec_read(struct hfs_find_data
*, void *, int);
122 extern int hfs_brec_goto(struct hfs_find_data
*, int);
125 struct hfs_bnode_desc
{
126 __be32 next
; /* (V) Number of the next node at this level */
127 __be32 prev
; /* (V) Number of the prev node at this level */
128 u8 type
; /* (F) The type of node */
129 u8 height
; /* (F) The level of this node (leaves=1) */
130 __be16 num_recs
; /* (V) The number of records in this node */
134 #define HFS_NODE_INDEX 0x00 /* An internal (index) node */
135 #define HFS_NODE_HEADER 0x01 /* The tree header node (node 0) */
136 #define HFS_NODE_MAP 0x02 /* Holds part of the bitmap of used nodes */
137 #define HFS_NODE_LEAF 0xFF /* A leaf (ndNHeight==1) node */
139 struct hfs_btree_header_rec
{
140 __be16 depth
; /* (V) The number of levels in this B-tree */
141 __be32 root
; /* (V) The node number of the root node */
142 __be32 leaf_count
; /* (V) The number of leaf records */
143 __be32 leaf_head
; /* (V) The number of the first leaf node */
144 __be32 leaf_tail
; /* (V) The number of the last leaf node */
145 __be16 node_size
; /* (F) The number of bytes in a node (=512) */
146 __be16 max_key_len
; /* (F) The length of a key in an index node */
147 __be32 node_count
; /* (V) The total number of nodes */
148 __be32 free_nodes
; /* (V) The number of unused nodes */
150 __be32 clump_size
; /* (F) clump size. not usually used. */
151 u8 btree_type
; /* (F) BTree type */
153 __be32 attributes
; /* (F) attributes */
157 #define BTREE_ATTR_BADCLOSE 0x00000001 /* b-tree not closed properly. not
159 #define HFS_TREE_BIGKEYS 0x00000002 /* key length is u16 instead of u8.
161 #define HFS_TREE_VARIDXKEYS 0x00000004 /* variable key length instead of
162 max key length. use din catalog
163 b-tree but not in extents