5 * Brad Boyer (flar@allandria.com)
6 * (C) 2003 Ardis Technologies <roman@ardistech.com>
8 * Handle opening/closing btree
11 #include <linux/pagemap.h>
12 #include <linux/log2.h>
16 /* Get a reference to a B*Tree and do some initial checks */
17 struct hfs_btree
*hfs_btree_open(struct super_block
*sb
, u32 id
, btree_keycmp keycmp
)
19 struct hfs_btree
*tree
;
20 struct hfs_btree_header_rec
*head
;
21 struct address_space
*mapping
;
25 tree
= kzalloc(sizeof(*tree
), GFP_KERNEL
);
29 init_MUTEX(&tree
->tree_lock
);
30 spin_lock_init(&tree
->hash_lock
);
31 /* Set the correct compare function */
34 tree
->keycmp
= keycmp
;
36 tree
->inode
= iget_locked(sb
, id
);
39 BUG_ON(!(tree
->inode
->i_state
& I_NEW
));
41 struct hfs_mdb
*mdb
= HFS_SB(sb
)->mdb
;
42 HFS_I(tree
->inode
)->flags
= 0;
43 mutex_init(&HFS_I(tree
->inode
)->extents_lock
);
46 hfs_inode_read_fork(tree
->inode
, mdb
->drXTExtRec
, mdb
->drXTFlSize
,
47 mdb
->drXTFlSize
, be32_to_cpu(mdb
->drXTClpSiz
));
48 if (HFS_I(tree
->inode
)->alloc_blocks
>
49 HFS_I(tree
->inode
)->first_blocks
) {
50 printk(KERN_ERR
"hfs: invalid btree extent records\n");
51 unlock_new_inode(tree
->inode
);
55 tree
->inode
->i_mapping
->a_ops
= &hfs_btree_aops
;
58 hfs_inode_read_fork(tree
->inode
, mdb
->drCTExtRec
, mdb
->drCTFlSize
,
59 mdb
->drCTFlSize
, be32_to_cpu(mdb
->drCTClpSiz
));
61 if (!HFS_I(tree
->inode
)->first_blocks
) {
62 printk(KERN_ERR
"hfs: invalid btree extent records "
64 unlock_new_inode(tree
->inode
);
68 tree
->inode
->i_mapping
->a_ops
= &hfs_btree_aops
;
74 unlock_new_inode(tree
->inode
);
76 mapping
= tree
->inode
->i_mapping
;
77 page
= read_mapping_page(mapping
, 0, NULL
);
82 head
= (struct hfs_btree_header_rec
*)(kmap(page
) + sizeof(struct hfs_bnode_desc
));
83 tree
->root
= be32_to_cpu(head
->root
);
84 tree
->leaf_count
= be32_to_cpu(head
->leaf_count
);
85 tree
->leaf_head
= be32_to_cpu(head
->leaf_head
);
86 tree
->leaf_tail
= be32_to_cpu(head
->leaf_tail
);
87 tree
->node_count
= be32_to_cpu(head
->node_count
);
88 tree
->free_nodes
= be32_to_cpu(head
->free_nodes
);
89 tree
->attributes
= be32_to_cpu(head
->attributes
);
90 tree
->node_size
= be16_to_cpu(head
->node_size
);
91 tree
->max_key_len
= be16_to_cpu(head
->max_key_len
);
92 tree
->depth
= be16_to_cpu(head
->depth
);
94 size
= tree
->node_size
;
95 if (!is_power_of_2(size
))
97 if (!tree
->node_count
)
101 if (tree
->max_key_len
!= HFS_MAX_EXT_KEYLEN
) {
102 printk(KERN_ERR
"hfs: invalid extent max_key_len %d\n",
108 if (tree
->max_key_len
!= HFS_MAX_CAT_KEYLEN
) {
109 printk(KERN_ERR
"hfs: invalid catalog max_key_len %d\n",
118 tree
->node_size_shift
= ffs(size
) - 1;
119 tree
->pages_per_bnode
= (tree
->node_size
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
122 page_cache_release(page
);
126 page_cache_release(page
);
128 tree
->inode
->i_mapping
->a_ops
= &hfs_aops
;
135 /* Release resources used by a btree */
136 void hfs_btree_close(struct hfs_btree
*tree
)
138 struct hfs_bnode
*node
;
144 for (i
= 0; i
< NODE_HASH_SIZE
; i
++) {
145 while ((node
= tree
->node_hash
[i
])) {
146 tree
->node_hash
[i
] = node
->next_hash
;
147 if (atomic_read(&node
->refcnt
))
148 printk(KERN_ERR
"hfs: node %d:%d still has %d user(s)!\n",
149 node
->tree
->cnid
, node
->this, atomic_read(&node
->refcnt
));
150 hfs_bnode_free(node
);
151 tree
->node_hash_cnt
--;
158 void hfs_btree_write(struct hfs_btree
*tree
)
160 struct hfs_btree_header_rec
*head
;
161 struct hfs_bnode
*node
;
164 node
= hfs_bnode_find(tree
, 0);
168 /* Load the header */
169 page
= node
->page
[0];
170 head
= (struct hfs_btree_header_rec
*)(kmap(page
) + sizeof(struct hfs_bnode_desc
));
172 head
->root
= cpu_to_be32(tree
->root
);
173 head
->leaf_count
= cpu_to_be32(tree
->leaf_count
);
174 head
->leaf_head
= cpu_to_be32(tree
->leaf_head
);
175 head
->leaf_tail
= cpu_to_be32(tree
->leaf_tail
);
176 head
->node_count
= cpu_to_be32(tree
->node_count
);
177 head
->free_nodes
= cpu_to_be32(tree
->free_nodes
);
178 head
->attributes
= cpu_to_be32(tree
->attributes
);
179 head
->depth
= cpu_to_be16(tree
->depth
);
182 set_page_dirty(page
);
186 static struct hfs_bnode
*hfs_bmap_new_bmap(struct hfs_bnode
*prev
, u32 idx
)
188 struct hfs_btree
*tree
= prev
->tree
;
189 struct hfs_bnode
*node
;
190 struct hfs_bnode_desc desc
;
193 node
= hfs_bnode_create(tree
, idx
);
197 if (!tree
->free_nodes
)
201 cnid
= cpu_to_be32(idx
);
202 hfs_bnode_write(prev
, &cnid
, offsetof(struct hfs_bnode_desc
, next
), 4);
204 node
->type
= HFS_NODE_MAP
;
206 hfs_bnode_clear(node
, 0, tree
->node_size
);
209 desc
.type
= HFS_NODE_MAP
;
211 desc
.num_recs
= cpu_to_be16(1);
213 hfs_bnode_write(node
, &desc
, 0, sizeof(desc
));
214 hfs_bnode_write_u16(node
, 14, 0x8000);
215 hfs_bnode_write_u16(node
, tree
->node_size
- 2, 14);
216 hfs_bnode_write_u16(node
, tree
->node_size
- 4, tree
->node_size
- 6);
221 struct hfs_bnode
*hfs_bmap_alloc(struct hfs_btree
*tree
)
223 struct hfs_bnode
*node
, *next_node
;
232 while (!tree
->free_nodes
) {
233 struct inode
*inode
= tree
->inode
;
237 res
= hfs_extend_file(inode
);
240 HFS_I(inode
)->phys_size
= inode
->i_size
=
241 (loff_t
)HFS_I(inode
)->alloc_blocks
*
242 HFS_SB(tree
->sb
)->alloc_blksz
;
243 HFS_I(inode
)->fs_blocks
= inode
->i_size
>>
244 tree
->sb
->s_blocksize_bits
;
245 inode_set_bytes(inode
, inode
->i_size
);
246 count
= inode
->i_size
>> tree
->node_size_shift
;
247 tree
->free_nodes
= count
- tree
->node_count
;
248 tree
->node_count
= count
;
252 node
= hfs_bnode_find(tree
, nidx
);
255 len
= hfs_brec_lenoff(node
, 2, &off16
);
258 off
+= node
->page_offset
;
259 pagep
= node
->page
+ (off
>> PAGE_CACHE_SHIFT
);
261 off
&= ~PAGE_CACHE_MASK
;
268 for (m
= 0x80, i
= 0; i
< 8; m
>>= 1, i
++) {
272 set_page_dirty(*pagep
);
275 mark_inode_dirty(tree
->inode
);
277 return hfs_bnode_create(tree
, idx
);
281 if (++off
>= PAGE_CACHE_SIZE
) {
283 data
= kmap(*++pagep
);
292 printk(KERN_DEBUG
"hfs: create new bmap node...\n");
293 next_node
= hfs_bmap_new_bmap(node
, idx
);
295 next_node
= hfs_bnode_find(tree
, nidx
);
297 if (IS_ERR(next_node
))
301 len
= hfs_brec_lenoff(node
, 0, &off16
);
303 off
+= node
->page_offset
;
304 pagep
= node
->page
+ (off
>> PAGE_CACHE_SHIFT
);
306 off
&= ~PAGE_CACHE_MASK
;
310 void hfs_bmap_free(struct hfs_bnode
*node
)
312 struct hfs_btree
*tree
;
318 dprint(DBG_BNODE_MOD
, "btree_free_node: %u\n", node
->this);
321 node
= hfs_bnode_find(tree
, 0);
324 len
= hfs_brec_lenoff(node
, 2, &off
);
325 while (nidx
>= len
* 8) {
333 printk(KERN_CRIT
"hfs: unable to free bnode %u. bmap not found!\n", node
->this);
336 node
= hfs_bnode_find(tree
, i
);
339 if (node
->type
!= HFS_NODE_MAP
) {
341 printk(KERN_CRIT
"hfs: invalid bmap found! (%u,%d)\n", node
->this, node
->type
);
345 len
= hfs_brec_lenoff(node
, 0, &off
);
347 off
+= node
->page_offset
+ nidx
/ 8;
348 page
= node
->page
[off
>> PAGE_CACHE_SHIFT
];
350 off
&= ~PAGE_CACHE_MASK
;
351 m
= 1 << (~nidx
& 7);
354 printk(KERN_CRIT
"hfs: trying to free free bnode %u(%d)\n", node
->this, node
->type
);
359 data
[off
] = byte
& ~m
;
360 set_page_dirty(page
);
364 mark_inode_dirty(tree
->inode
);