2 * linux/fs/hfsplus/btree.c
5 * Brad Boyer (flar@allandria.com)
6 * (C) 2003 Ardis Technologies <roman@ardistech.com>
8 * Handle opening/closing btree
11 #include <linux/slab.h>
12 #include <linux/pagemap.h>
13 #include <linux/log2.h>
15 #include "hfsplus_fs.h"
16 #include "hfsplus_raw.h"
19 /* Get a reference to a B*Tree and do some initial checks */
20 struct hfs_btree
*hfs_btree_open(struct super_block
*sb
, u32 id
)
22 struct hfs_btree
*tree
;
23 struct hfs_btree_header_rec
*head
;
24 struct address_space
*mapping
;
28 tree
= kzalloc(sizeof(*tree
), GFP_KERNEL
);
32 init_MUTEX(&tree
->tree_lock
);
33 spin_lock_init(&tree
->hash_lock
);
36 tree
->inode
= iget(sb
, id
);
40 mapping
= tree
->inode
->i_mapping
;
41 page
= read_mapping_page(mapping
, 0, NULL
);
46 head
= (struct hfs_btree_header_rec
*)(kmap(page
) + sizeof(struct hfs_bnode_desc
));
47 tree
->root
= be32_to_cpu(head
->root
);
48 tree
->leaf_count
= be32_to_cpu(head
->leaf_count
);
49 tree
->leaf_head
= be32_to_cpu(head
->leaf_head
);
50 tree
->leaf_tail
= be32_to_cpu(head
->leaf_tail
);
51 tree
->node_count
= be32_to_cpu(head
->node_count
);
52 tree
->free_nodes
= be32_to_cpu(head
->free_nodes
);
53 tree
->attributes
= be32_to_cpu(head
->attributes
);
54 tree
->node_size
= be16_to_cpu(head
->node_size
);
55 tree
->max_key_len
= be16_to_cpu(head
->max_key_len
);
56 tree
->depth
= be16_to_cpu(head
->depth
);
58 /* Set the correct compare function */
59 if (id
== HFSPLUS_EXT_CNID
) {
60 tree
->keycmp
= hfsplus_ext_cmp_key
;
61 } else if (id
== HFSPLUS_CAT_CNID
) {
62 if ((HFSPLUS_SB(sb
).flags
& HFSPLUS_SB_HFSX
) &&
63 (head
->key_type
== HFSPLUS_KEY_BINARY
))
64 tree
->keycmp
= hfsplus_cat_bin_cmp_key
;
66 tree
->keycmp
= hfsplus_cat_case_cmp_key
;
67 HFSPLUS_SB(sb
).flags
|= HFSPLUS_SB_CASEFOLD
;
70 printk(KERN_ERR
"hfs: unknown B*Tree requested\n");
74 size
= tree
->node_size
;
75 if (!is_power_of_2(size
))
77 if (!tree
->node_count
)
79 tree
->node_size_shift
= ffs(size
) - 1;
81 tree
->pages_per_bnode
= (tree
->node_size
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
84 page_cache_release(page
);
88 tree
->inode
->i_mapping
->a_ops
= &hfsplus_aops
;
89 page_cache_release(page
);
96 /* Release resources used by a btree */
97 void hfs_btree_close(struct hfs_btree
*tree
)
99 struct hfs_bnode
*node
;
105 for (i
= 0; i
< NODE_HASH_SIZE
; i
++) {
106 while ((node
= tree
->node_hash
[i
])) {
107 tree
->node_hash
[i
] = node
->next_hash
;
108 if (atomic_read(&node
->refcnt
))
109 printk(KERN_CRIT
"hfs: node %d:%d still has %d user(s)!\n",
110 node
->tree
->cnid
, node
->this, atomic_read(&node
->refcnt
));
111 hfs_bnode_free(node
);
112 tree
->node_hash_cnt
--;
119 void hfs_btree_write(struct hfs_btree
*tree
)
121 struct hfs_btree_header_rec
*head
;
122 struct hfs_bnode
*node
;
125 node
= hfs_bnode_find(tree
, 0);
129 /* Load the header */
130 page
= node
->page
[0];
131 head
= (struct hfs_btree_header_rec
*)(kmap(page
) + sizeof(struct hfs_bnode_desc
));
133 head
->root
= cpu_to_be32(tree
->root
);
134 head
->leaf_count
= cpu_to_be32(tree
->leaf_count
);
135 head
->leaf_head
= cpu_to_be32(tree
->leaf_head
);
136 head
->leaf_tail
= cpu_to_be32(tree
->leaf_tail
);
137 head
->node_count
= cpu_to_be32(tree
->node_count
);
138 head
->free_nodes
= cpu_to_be32(tree
->free_nodes
);
139 head
->attributes
= cpu_to_be32(tree
->attributes
);
140 head
->depth
= cpu_to_be16(tree
->depth
);
143 set_page_dirty(page
);
147 static struct hfs_bnode
*hfs_bmap_new_bmap(struct hfs_bnode
*prev
, u32 idx
)
149 struct hfs_btree
*tree
= prev
->tree
;
150 struct hfs_bnode
*node
;
151 struct hfs_bnode_desc desc
;
154 node
= hfs_bnode_create(tree
, idx
);
160 cnid
= cpu_to_be32(idx
);
161 hfs_bnode_write(prev
, &cnid
, offsetof(struct hfs_bnode_desc
, next
), 4);
163 node
->type
= HFS_NODE_MAP
;
165 hfs_bnode_clear(node
, 0, tree
->node_size
);
168 desc
.type
= HFS_NODE_MAP
;
170 desc
.num_recs
= cpu_to_be16(1);
172 hfs_bnode_write(node
, &desc
, 0, sizeof(desc
));
173 hfs_bnode_write_u16(node
, 14, 0x8000);
174 hfs_bnode_write_u16(node
, tree
->node_size
- 2, 14);
175 hfs_bnode_write_u16(node
, tree
->node_size
- 4, tree
->node_size
- 6);
180 struct hfs_bnode
*hfs_bmap_alloc(struct hfs_btree
*tree
)
182 struct hfs_bnode
*node
, *next_node
;
189 while (!tree
->free_nodes
) {
190 struct inode
*inode
= tree
->inode
;
194 res
= hfsplus_file_extend(inode
);
197 HFSPLUS_I(inode
).phys_size
= inode
->i_size
=
198 (loff_t
)HFSPLUS_I(inode
).alloc_blocks
<<
199 HFSPLUS_SB(tree
->sb
).alloc_blksz_shift
;
200 HFSPLUS_I(inode
).fs_blocks
= HFSPLUS_I(inode
).alloc_blocks
<<
201 HFSPLUS_SB(tree
->sb
).fs_shift
;
202 inode_set_bytes(inode
, inode
->i_size
);
203 count
= inode
->i_size
>> tree
->node_size_shift
;
204 tree
->free_nodes
= count
- tree
->node_count
;
205 tree
->node_count
= count
;
209 node
= hfs_bnode_find(tree
, nidx
);
212 len
= hfs_brec_lenoff(node
, 2, &off
);
214 off
+= node
->page_offset
;
215 pagep
= node
->page
+ (off
>> PAGE_CACHE_SHIFT
);
217 off
&= ~PAGE_CACHE_MASK
;
224 for (m
= 0x80, i
= 0; i
< 8; m
>>= 1, i
++) {
228 set_page_dirty(*pagep
);
231 mark_inode_dirty(tree
->inode
);
233 return hfs_bnode_create(tree
, idx
);
237 if (++off
>= PAGE_CACHE_SIZE
) {
239 data
= kmap(*++pagep
);
248 printk(KERN_DEBUG
"hfs: create new bmap node...\n");
249 next_node
= hfs_bmap_new_bmap(node
, idx
);
251 next_node
= hfs_bnode_find(tree
, nidx
);
253 if (IS_ERR(next_node
))
257 len
= hfs_brec_lenoff(node
, 0, &off
);
258 off
+= node
->page_offset
;
259 pagep
= node
->page
+ (off
>> PAGE_CACHE_SHIFT
);
261 off
&= ~PAGE_CACHE_MASK
;
265 void hfs_bmap_free(struct hfs_bnode
*node
)
267 struct hfs_btree
*tree
;
273 dprint(DBG_BNODE_MOD
, "btree_free_node: %u\n", node
->this);
277 node
= hfs_bnode_find(tree
, 0);
280 len
= hfs_brec_lenoff(node
, 2, &off
);
281 while (nidx
>= len
* 8) {
289 printk(KERN_CRIT
"hfs: unable to free bnode %u. bmap not found!\n", node
->this);
292 node
= hfs_bnode_find(tree
, i
);
295 if (node
->type
!= HFS_NODE_MAP
) {
297 printk(KERN_CRIT
"hfs: invalid bmap found! (%u,%d)\n", node
->this, node
->type
);
301 len
= hfs_brec_lenoff(node
, 0, &off
);
303 off
+= node
->page_offset
+ nidx
/ 8;
304 page
= node
->page
[off
>> PAGE_CACHE_SHIFT
];
306 off
&= ~PAGE_CACHE_MASK
;
307 m
= 1 << (~nidx
& 7);
310 printk(KERN_CRIT
"hfs: trying to free free bnode %u(%d)\n", node
->this, node
->type
);
315 data
[off
] = byte
& ~m
;
316 set_page_dirty(page
);
320 mark_inode_dirty(tree
->inode
);