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
;
29 tree
= kzalloc(sizeof(*tree
), GFP_KERNEL
);
33 mutex_init(&tree
->tree_lock
);
34 spin_lock_init(&tree
->hash_lock
);
37 inode
= hfsplus_iget(sb
, id
);
42 if (!HFSPLUS_I(tree
->inode
)->first_blocks
) {
44 "hfs: invalid btree extent records (0 size).\n");
48 mapping
= tree
->inode
->i_mapping
;
49 page
= read_mapping_page(mapping
, 0, NULL
);
54 head
= (struct hfs_btree_header_rec
*)(kmap(page
) +
55 sizeof(struct hfs_bnode_desc
));
56 tree
->root
= be32_to_cpu(head
->root
);
57 tree
->leaf_count
= be32_to_cpu(head
->leaf_count
);
58 tree
->leaf_head
= be32_to_cpu(head
->leaf_head
);
59 tree
->leaf_tail
= be32_to_cpu(head
->leaf_tail
);
60 tree
->node_count
= be32_to_cpu(head
->node_count
);
61 tree
->free_nodes
= be32_to_cpu(head
->free_nodes
);
62 tree
->attributes
= be32_to_cpu(head
->attributes
);
63 tree
->node_size
= be16_to_cpu(head
->node_size
);
64 tree
->max_key_len
= be16_to_cpu(head
->max_key_len
);
65 tree
->depth
= be16_to_cpu(head
->depth
);
67 /* Verify the tree and set the correct compare function */
69 case HFSPLUS_EXT_CNID
:
70 if (tree
->max_key_len
!= HFSPLUS_EXT_KEYLEN
- sizeof(u16
)) {
71 printk(KERN_ERR
"hfs: invalid extent max_key_len %d\n",
75 if (tree
->attributes
& HFS_TREE_VARIDXKEYS
) {
76 printk(KERN_ERR
"hfs: invalid extent btree flag\n");
80 tree
->keycmp
= hfsplus_ext_cmp_key
;
82 case HFSPLUS_CAT_CNID
:
83 if (tree
->max_key_len
!= HFSPLUS_CAT_KEYLEN
- sizeof(u16
)) {
84 printk(KERN_ERR
"hfs: invalid catalog max_key_len %d\n",
88 if (!(tree
->attributes
& HFS_TREE_VARIDXKEYS
)) {
89 printk(KERN_ERR
"hfs: invalid catalog btree flag\n");
93 if (test_bit(HFSPLUS_SB_HFSX
, &HFSPLUS_SB(sb
)->flags
) &&
94 (head
->key_type
== HFSPLUS_KEY_BINARY
))
95 tree
->keycmp
= hfsplus_cat_bin_cmp_key
;
97 tree
->keycmp
= hfsplus_cat_case_cmp_key
;
98 set_bit(HFSPLUS_SB_CASEFOLD
, &HFSPLUS_SB(sb
)->flags
);
101 case HFSPLUS_ATTR_CNID
:
102 if (tree
->max_key_len
!= HFSPLUS_ATTR_KEYLEN
- sizeof(u16
)) {
103 printk(KERN_ERR
"hfs: invalid attributes max_key_len %d\n",
107 tree
->keycmp
= hfsplus_attr_bin_cmp_key
;
110 printk(KERN_ERR
"hfs: unknown B*Tree requested\n");
114 if (!(tree
->attributes
& HFS_TREE_BIGKEYS
)) {
115 printk(KERN_ERR
"hfs: invalid btree flag\n");
119 size
= tree
->node_size
;
120 if (!is_power_of_2(size
))
122 if (!tree
->node_count
)
125 tree
->node_size_shift
= ffs(size
) - 1;
127 tree
->pages_per_bnode
=
128 (tree
->node_size
+ PAGE_CACHE_SIZE
- 1) >>
132 page_cache_release(page
);
136 page_cache_release(page
);
138 tree
->inode
->i_mapping
->a_ops
= &hfsplus_aops
;
145 /* Release resources used by a btree */
146 void hfs_btree_close(struct hfs_btree
*tree
)
148 struct hfs_bnode
*node
;
154 for (i
= 0; i
< NODE_HASH_SIZE
; i
++) {
155 while ((node
= tree
->node_hash
[i
])) {
156 tree
->node_hash
[i
] = node
->next_hash
;
157 if (atomic_read(&node
->refcnt
))
158 printk(KERN_CRIT
"hfs: node %d:%d "
159 "still has %d user(s)!\n",
160 node
->tree
->cnid
, node
->this,
161 atomic_read(&node
->refcnt
));
162 hfs_bnode_free(node
);
163 tree
->node_hash_cnt
--;
170 int hfs_btree_write(struct hfs_btree
*tree
)
172 struct hfs_btree_header_rec
*head
;
173 struct hfs_bnode
*node
;
176 node
= hfs_bnode_find(tree
, 0);
180 /* Load the header */
181 page
= node
->page
[0];
182 head
= (struct hfs_btree_header_rec
*)(kmap(page
) +
183 sizeof(struct hfs_bnode_desc
));
185 head
->root
= cpu_to_be32(tree
->root
);
186 head
->leaf_count
= cpu_to_be32(tree
->leaf_count
);
187 head
->leaf_head
= cpu_to_be32(tree
->leaf_head
);
188 head
->leaf_tail
= cpu_to_be32(tree
->leaf_tail
);
189 head
->node_count
= cpu_to_be32(tree
->node_count
);
190 head
->free_nodes
= cpu_to_be32(tree
->free_nodes
);
191 head
->attributes
= cpu_to_be32(tree
->attributes
);
192 head
->depth
= cpu_to_be16(tree
->depth
);
195 set_page_dirty(page
);
200 static struct hfs_bnode
*hfs_bmap_new_bmap(struct hfs_bnode
*prev
, u32 idx
)
202 struct hfs_btree
*tree
= prev
->tree
;
203 struct hfs_bnode
*node
;
204 struct hfs_bnode_desc desc
;
207 node
= hfs_bnode_create(tree
, idx
);
213 cnid
= cpu_to_be32(idx
);
214 hfs_bnode_write(prev
, &cnid
, offsetof(struct hfs_bnode_desc
, next
), 4);
216 node
->type
= HFS_NODE_MAP
;
218 hfs_bnode_clear(node
, 0, tree
->node_size
);
221 desc
.type
= HFS_NODE_MAP
;
223 desc
.num_recs
= cpu_to_be16(1);
225 hfs_bnode_write(node
, &desc
, 0, sizeof(desc
));
226 hfs_bnode_write_u16(node
, 14, 0x8000);
227 hfs_bnode_write_u16(node
, tree
->node_size
- 2, 14);
228 hfs_bnode_write_u16(node
, tree
->node_size
- 4, tree
->node_size
- 6);
233 struct hfs_bnode
*hfs_bmap_alloc(struct hfs_btree
*tree
)
235 struct hfs_bnode
*node
, *next_node
;
244 while (!tree
->free_nodes
) {
245 struct inode
*inode
= tree
->inode
;
246 struct hfsplus_inode_info
*hip
= HFSPLUS_I(inode
);
250 res
= hfsplus_file_extend(inode
);
253 hip
->phys_size
= inode
->i_size
=
254 (loff_t
)hip
->alloc_blocks
<<
255 HFSPLUS_SB(tree
->sb
)->alloc_blksz_shift
;
257 hip
->alloc_blocks
<< HFSPLUS_SB(tree
->sb
)->fs_shift
;
258 inode_set_bytes(inode
, inode
->i_size
);
259 count
= inode
->i_size
>> tree
->node_size_shift
;
260 tree
->free_nodes
= count
- tree
->node_count
;
261 tree
->node_count
= count
;
265 node
= hfs_bnode_find(tree
, nidx
);
268 len
= hfs_brec_lenoff(node
, 2, &off16
);
271 off
+= node
->page_offset
;
272 pagep
= node
->page
+ (off
>> PAGE_CACHE_SHIFT
);
274 off
&= ~PAGE_CACHE_MASK
;
281 for (m
= 0x80, i
= 0; i
< 8; m
>>= 1, i
++) {
285 set_page_dirty(*pagep
);
288 mark_inode_dirty(tree
->inode
);
290 return hfs_bnode_create(tree
,
295 if (++off
>= PAGE_CACHE_SIZE
) {
297 data
= kmap(*++pagep
);
306 dprint(DBG_BNODE_MOD
, "hfs: create new bmap node.\n");
307 next_node
= hfs_bmap_new_bmap(node
, idx
);
309 next_node
= hfs_bnode_find(tree
, nidx
);
311 if (IS_ERR(next_node
))
315 len
= hfs_brec_lenoff(node
, 0, &off16
);
317 off
+= node
->page_offset
;
318 pagep
= node
->page
+ (off
>> PAGE_CACHE_SHIFT
);
320 off
&= ~PAGE_CACHE_MASK
;
324 void hfs_bmap_free(struct hfs_bnode
*node
)
326 struct hfs_btree
*tree
;
332 dprint(DBG_BNODE_MOD
, "btree_free_node: %u\n", node
->this);
336 node
= hfs_bnode_find(tree
, 0);
339 len
= hfs_brec_lenoff(node
, 2, &off
);
340 while (nidx
>= len
* 8) {
348 printk(KERN_CRIT
"hfs: unable to free bnode %u. "
353 node
= hfs_bnode_find(tree
, i
);
356 if (node
->type
!= HFS_NODE_MAP
) {
358 printk(KERN_CRIT
"hfs: invalid bmap found! "
360 node
->this, node
->type
);
364 len
= hfs_brec_lenoff(node
, 0, &off
);
366 off
+= node
->page_offset
+ nidx
/ 8;
367 page
= node
->page
[off
>> PAGE_CACHE_SHIFT
];
369 off
&= ~PAGE_CACHE_MASK
;
370 m
= 1 << (~nidx
& 7);
373 printk(KERN_CRIT
"hfs: trying to free free bnode "
375 node
->this, node
->type
);
380 data
[off
] = byte
& ~m
;
381 set_page_dirty(page
);
385 mark_inode_dirty(tree
->inode
);