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