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;
68 printk(KERN_ERR "hfs: unknown B*Tree requested\n");
72 size = tree->node_size;
73 if (!is_power_of_2(size))
75 if (!tree->node_count)
77 tree->node_size_shift = ffs(size) - 1;
79 tree->pages_per_bnode = (tree->node_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
82 page_cache_release(page);
86 tree->inode->i_mapping->a_ops = &hfsplus_aops;
87 page_cache_release(page);
94 /* Release resources used by a btree */
95 void hfs_btree_close(struct hfs_btree *tree)
97 struct hfs_bnode *node;
103 for (i = 0; i < NODE_HASH_SIZE; i++) {
104 while ((node = tree->node_hash[i])) {
105 tree->node_hash[i] = node->next_hash;
106 if (atomic_read(&node->refcnt))
107 printk(KERN_CRIT "hfs: node %d:%d still has %d user(s)!\n",
108 node->tree->cnid, node->this, atomic_read(&node->refcnt));
109 hfs_bnode_free(node);
110 tree->node_hash_cnt--;
117 void hfs_btree_write(struct hfs_btree *tree)
119 struct hfs_btree_header_rec *head;
120 struct hfs_bnode *node;
123 node = hfs_bnode_find(tree, 0);
127 /* Load the header */
128 page = node->page[0];
129 head = (struct hfs_btree_header_rec *)(kmap(page) + sizeof(struct hfs_bnode_desc));
131 head->root = cpu_to_be32(tree->root);
132 head->leaf_count = cpu_to_be32(tree->leaf_count);
133 head->leaf_head = cpu_to_be32(tree->leaf_head);
134 head->leaf_tail = cpu_to_be32(tree->leaf_tail);
135 head->node_count = cpu_to_be32(tree->node_count);
136 head->free_nodes = cpu_to_be32(tree->free_nodes);
137 head->attributes = cpu_to_be32(tree->attributes);
138 head->depth = cpu_to_be16(tree->depth);
141 set_page_dirty(page);
145 static struct hfs_bnode *hfs_bmap_new_bmap(struct hfs_bnode *prev, u32 idx)
147 struct hfs_btree *tree = prev->tree;
148 struct hfs_bnode *node;
149 struct hfs_bnode_desc desc;
152 node = hfs_bnode_create(tree, idx);
158 cnid = cpu_to_be32(idx);
159 hfs_bnode_write(prev, &cnid, offsetof(struct hfs_bnode_desc, next), 4);
161 node->type = HFS_NODE_MAP;
163 hfs_bnode_clear(node, 0, tree->node_size);
166 desc.type = HFS_NODE_MAP;
168 desc.num_recs = cpu_to_be16(1);
170 hfs_bnode_write(node, &desc, 0, sizeof(desc));
171 hfs_bnode_write_u16(node, 14, 0x8000);
172 hfs_bnode_write_u16(node, tree->node_size - 2, 14);
173 hfs_bnode_write_u16(node, tree->node_size - 4, tree->node_size - 6);
178 struct hfs_bnode *hfs_bmap_alloc(struct hfs_btree *tree)
180 struct hfs_bnode *node, *next_node;
187 while (!tree->free_nodes) {
188 struct inode *inode = tree->inode;
192 res = hfsplus_file_extend(inode);
195 HFSPLUS_I(inode).phys_size = inode->i_size =
196 (loff_t)HFSPLUS_I(inode).alloc_blocks <<
197 HFSPLUS_SB(tree->sb).alloc_blksz_shift;
198 HFSPLUS_I(inode).fs_blocks = HFSPLUS_I(inode).alloc_blocks <<
199 HFSPLUS_SB(tree->sb).fs_shift;
200 inode_set_bytes(inode, inode->i_size);
201 count = inode->i_size >> tree->node_size_shift;
202 tree->free_nodes = count - tree->node_count;
203 tree->node_count = count;
207 node = hfs_bnode_find(tree, nidx);
210 len = hfs_brec_lenoff(node, 2, &off);
212 off += node->page_offset;
213 pagep = node->page + (off >> PAGE_CACHE_SHIFT);
215 off &= ~PAGE_CACHE_MASK;
222 for (m = 0x80, i = 0; i < 8; m >>= 1, i++) {
226 set_page_dirty(*pagep);
229 mark_inode_dirty(tree->inode);
231 return hfs_bnode_create(tree, idx);
235 if (++off >= PAGE_CACHE_SIZE) {
237 data = kmap(*++pagep);
246 printk(KERN_DEBUG "hfs: create new bmap node...\n");
247 next_node = hfs_bmap_new_bmap(node, idx);
249 next_node = hfs_bnode_find(tree, nidx);
251 if (IS_ERR(next_node))
255 len = hfs_brec_lenoff(node, 0, &off);
256 off += node->page_offset;
257 pagep = node->page + (off >> PAGE_CACHE_SHIFT);
259 off &= ~PAGE_CACHE_MASK;
263 void hfs_bmap_free(struct hfs_bnode *node)
265 struct hfs_btree *tree;
271 dprint(DBG_BNODE_MOD, "btree_free_node: %u\n", node->this);
275 node = hfs_bnode_find(tree, 0);
278 len = hfs_brec_lenoff(node, 2, &off);
279 while (nidx >= len * 8) {
287 printk(KERN_CRIT "hfs: unable to free bnode %u. bmap not found!\n", node->this);
290 node = hfs_bnode_find(tree, i);
293 if (node->type != HFS_NODE_MAP) {
295 printk(KERN_CRIT "hfs: invalid bmap found! (%u,%d)\n", node->this, node->type);
299 len = hfs_brec_lenoff(node, 0, &off);
301 off += node->page_offset + nidx / 8;
302 page = node->page[off >> PAGE_CACHE_SHIFT];
304 off &= ~PAGE_CACHE_MASK;
305 m = 1 << (~nidx & 7);
308 printk(KERN_CRIT "hfs: trying to free free bnode %u(%d)\n", node->this, node->type);
313 data[off] = byte & ~m;
314 set_page_dirty(page);
318 mark_inode_dirty(tree->inode);