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/slab.h>
13 #include <linux/log2.h>
17 /* Get a reference to a B*Tree and do some initial checks */
18 struct hfs_btree
*hfs_btree_open(struct super_block
*sb
, u32 id
, btree_keycmp keycmp
)
20 struct hfs_btree
*tree
;
21 struct hfs_btree_header_rec
*head
;
22 struct address_space
*mapping
;
26 tree
= kzalloc(sizeof(*tree
), GFP_KERNEL
);
30 init_MUTEX(&tree
->tree_lock
);
31 spin_lock_init(&tree
->hash_lock
);
32 /* Set the correct compare function */
35 tree
->keycmp
= keycmp
;
37 tree
->inode
= iget_locked(sb
, id
);
40 BUG_ON(!(tree
->inode
->i_state
& I_NEW
));
42 struct hfs_mdb
*mdb
= HFS_SB(sb
)->mdb
;
43 HFS_I(tree
->inode
)->flags
= 0;
44 mutex_init(&HFS_I(tree
->inode
)->extents_lock
);
47 hfs_inode_read_fork(tree
->inode
, mdb
->drXTExtRec
, mdb
->drXTFlSize
,
48 mdb
->drXTFlSize
, be32_to_cpu(mdb
->drXTClpSiz
));
49 tree
->inode
->i_mapping
->a_ops
= &hfs_btree_aops
;
52 hfs_inode_read_fork(tree
->inode
, mdb
->drCTExtRec
, mdb
->drCTFlSize
,
53 mdb
->drCTFlSize
, be32_to_cpu(mdb
->drCTClpSiz
));
54 tree
->inode
->i_mapping
->a_ops
= &hfs_btree_aops
;
60 unlock_new_inode(tree
->inode
);
62 if (!HFS_I(tree
->inode
)->first_blocks
) {
63 printk(KERN_ERR
"hfs: invalid btree extent records (0 size).\n");
67 mapping
= tree
->inode
->i_mapping
;
68 page
= read_mapping_page(mapping
, 0, NULL
);
73 head
= (struct hfs_btree_header_rec
*)(kmap(page
) + sizeof(struct hfs_bnode_desc
));
74 tree
->root
= be32_to_cpu(head
->root
);
75 tree
->leaf_count
= be32_to_cpu(head
->leaf_count
);
76 tree
->leaf_head
= be32_to_cpu(head
->leaf_head
);
77 tree
->leaf_tail
= be32_to_cpu(head
->leaf_tail
);
78 tree
->node_count
= be32_to_cpu(head
->node_count
);
79 tree
->free_nodes
= be32_to_cpu(head
->free_nodes
);
80 tree
->attributes
= be32_to_cpu(head
->attributes
);
81 tree
->node_size
= be16_to_cpu(head
->node_size
);
82 tree
->max_key_len
= be16_to_cpu(head
->max_key_len
);
83 tree
->depth
= be16_to_cpu(head
->depth
);
85 size
= tree
->node_size
;
86 if (!is_power_of_2(size
))
88 if (!tree
->node_count
)
92 if (tree
->max_key_len
!= HFS_MAX_EXT_KEYLEN
) {
93 printk(KERN_ERR
"hfs: invalid extent max_key_len %d\n",
99 if (tree
->max_key_len
!= HFS_MAX_CAT_KEYLEN
) {
100 printk(KERN_ERR
"hfs: invalid catalog max_key_len %d\n",
109 tree
->node_size_shift
= ffs(size
) - 1;
110 tree
->pages_per_bnode
= (tree
->node_size
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
113 page_cache_release(page
);
117 page_cache_release(page
);
119 tree
->inode
->i_mapping
->a_ops
= &hfs_aops
;
126 /* Release resources used by a btree */
127 void hfs_btree_close(struct hfs_btree
*tree
)
129 struct hfs_bnode
*node
;
135 for (i
= 0; i
< NODE_HASH_SIZE
; i
++) {
136 while ((node
= tree
->node_hash
[i
])) {
137 tree
->node_hash
[i
] = node
->next_hash
;
138 if (atomic_read(&node
->refcnt
))
139 printk(KERN_ERR
"hfs: node %d:%d still has %d user(s)!\n",
140 node
->tree
->cnid
, node
->this, atomic_read(&node
->refcnt
));
141 hfs_bnode_free(node
);
142 tree
->node_hash_cnt
--;
149 void hfs_btree_write(struct hfs_btree
*tree
)
151 struct hfs_btree_header_rec
*head
;
152 struct hfs_bnode
*node
;
155 node
= hfs_bnode_find(tree
, 0);
159 /* Load the header */
160 page
= node
->page
[0];
161 head
= (struct hfs_btree_header_rec
*)(kmap(page
) + sizeof(struct hfs_bnode_desc
));
163 head
->root
= cpu_to_be32(tree
->root
);
164 head
->leaf_count
= cpu_to_be32(tree
->leaf_count
);
165 head
->leaf_head
= cpu_to_be32(tree
->leaf_head
);
166 head
->leaf_tail
= cpu_to_be32(tree
->leaf_tail
);
167 head
->node_count
= cpu_to_be32(tree
->node_count
);
168 head
->free_nodes
= cpu_to_be32(tree
->free_nodes
);
169 head
->attributes
= cpu_to_be32(tree
->attributes
);
170 head
->depth
= cpu_to_be16(tree
->depth
);
173 set_page_dirty(page
);
177 static struct hfs_bnode
*hfs_bmap_new_bmap(struct hfs_bnode
*prev
, u32 idx
)
179 struct hfs_btree
*tree
= prev
->tree
;
180 struct hfs_bnode
*node
;
181 struct hfs_bnode_desc desc
;
184 node
= hfs_bnode_create(tree
, idx
);
188 if (!tree
->free_nodes
)
192 cnid
= cpu_to_be32(idx
);
193 hfs_bnode_write(prev
, &cnid
, offsetof(struct hfs_bnode_desc
, next
), 4);
195 node
->type
= HFS_NODE_MAP
;
197 hfs_bnode_clear(node
, 0, tree
->node_size
);
200 desc
.type
= HFS_NODE_MAP
;
202 desc
.num_recs
= cpu_to_be16(1);
204 hfs_bnode_write(node
, &desc
, 0, sizeof(desc
));
205 hfs_bnode_write_u16(node
, 14, 0x8000);
206 hfs_bnode_write_u16(node
, tree
->node_size
- 2, 14);
207 hfs_bnode_write_u16(node
, tree
->node_size
- 4, tree
->node_size
- 6);
212 struct hfs_bnode
*hfs_bmap_alloc(struct hfs_btree
*tree
)
214 struct hfs_bnode
*node
, *next_node
;
223 while (!tree
->free_nodes
) {
224 struct inode
*inode
= tree
->inode
;
228 res
= hfs_extend_file(inode
);
231 HFS_I(inode
)->phys_size
= inode
->i_size
=
232 (loff_t
)HFS_I(inode
)->alloc_blocks
*
233 HFS_SB(tree
->sb
)->alloc_blksz
;
234 HFS_I(inode
)->fs_blocks
= inode
->i_size
>>
235 tree
->sb
->s_blocksize_bits
;
236 inode_set_bytes(inode
, inode
->i_size
);
237 count
= inode
->i_size
>> tree
->node_size_shift
;
238 tree
->free_nodes
= count
- tree
->node_count
;
239 tree
->node_count
= count
;
243 node
= hfs_bnode_find(tree
, nidx
);
246 len
= hfs_brec_lenoff(node
, 2, &off16
);
249 off
+= node
->page_offset
;
250 pagep
= node
->page
+ (off
>> PAGE_CACHE_SHIFT
);
252 off
&= ~PAGE_CACHE_MASK
;
259 for (m
= 0x80, i
= 0; i
< 8; m
>>= 1, i
++) {
263 set_page_dirty(*pagep
);
266 mark_inode_dirty(tree
->inode
);
268 return hfs_bnode_create(tree
, idx
);
272 if (++off
>= PAGE_CACHE_SIZE
) {
274 data
= kmap(*++pagep
);
283 printk(KERN_DEBUG
"hfs: create new bmap node...\n");
284 next_node
= hfs_bmap_new_bmap(node
, idx
);
286 next_node
= hfs_bnode_find(tree
, nidx
);
288 if (IS_ERR(next_node
))
292 len
= hfs_brec_lenoff(node
, 0, &off16
);
294 off
+= node
->page_offset
;
295 pagep
= node
->page
+ (off
>> PAGE_CACHE_SHIFT
);
297 off
&= ~PAGE_CACHE_MASK
;
301 void hfs_bmap_free(struct hfs_bnode
*node
)
303 struct hfs_btree
*tree
;
309 dprint(DBG_BNODE_MOD
, "btree_free_node: %u\n", node
->this);
312 node
= hfs_bnode_find(tree
, 0);
315 len
= hfs_brec_lenoff(node
, 2, &off
);
316 while (nidx
>= len
* 8) {
324 printk(KERN_CRIT
"hfs: unable to free bnode %u. bmap not found!\n", node
->this);
327 node
= hfs_bnode_find(tree
, i
);
330 if (node
->type
!= HFS_NODE_MAP
) {
332 printk(KERN_CRIT
"hfs: invalid bmap found! (%u,%d)\n", node
->this, node
->type
);
336 len
= hfs_brec_lenoff(node
, 0, &off
);
338 off
+= node
->page_offset
+ nidx
/ 8;
339 page
= node
->page
[off
>> PAGE_CACHE_SHIFT
];
341 off
&= ~PAGE_CACHE_MASK
;
342 m
= 1 << (~nidx
& 7);
345 printk(KERN_CRIT
"hfs: trying to free free bnode %u(%d)\n", node
->this, node
->type
);
350 data
[off
] = byte
& ~m
;
351 set_page_dirty(page
);
355 mark_inode_dirty(tree
->inode
);