1 // SPDX-License-Identifier: GPL-2.0
6 * Brad Boyer (flar@allandria.com)
7 * (C) 2003 Ardis Technologies <roman@ardistech.com>
9 * Handle opening/closing btree
12 #include <linux/pagemap.h>
13 #include <linux/slab.h>
14 #include <linux/log2.h>
18 /* Get a reference to a B*Tree and do some initial checks */
19 struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id, btree_keycmp keycmp)
21 struct hfs_btree *tree;
22 struct hfs_btree_header_rec *head;
23 struct address_space *mapping;
27 tree = kzalloc(sizeof(*tree), GFP_KERNEL);
31 mutex_init(&tree->tree_lock);
32 spin_lock_init(&tree->hash_lock);
33 /* Set the correct compare function */
36 tree->keycmp = keycmp;
38 tree->inode = iget_locked(sb, id);
41 BUG_ON(!(tree->inode->i_state & I_NEW));
43 struct hfs_mdb *mdb = HFS_SB(sb)->mdb;
44 HFS_I(tree->inode)->flags = 0;
45 mutex_init(&HFS_I(tree->inode)->extents_lock);
48 hfs_inode_read_fork(tree->inode, mdb->drXTExtRec, mdb->drXTFlSize,
49 mdb->drXTFlSize, be32_to_cpu(mdb->drXTClpSiz));
50 if (HFS_I(tree->inode)->alloc_blocks >
51 HFS_I(tree->inode)->first_blocks) {
52 pr_err("invalid btree extent records\n");
53 unlock_new_inode(tree->inode);
57 tree->inode->i_mapping->a_ops = &hfs_btree_aops;
60 hfs_inode_read_fork(tree->inode, mdb->drCTExtRec, mdb->drCTFlSize,
61 mdb->drCTFlSize, be32_to_cpu(mdb->drCTClpSiz));
63 if (!HFS_I(tree->inode)->first_blocks) {
64 pr_err("invalid btree extent records (0 size)\n");
65 unlock_new_inode(tree->inode);
69 tree->inode->i_mapping->a_ops = &hfs_btree_aops;
75 unlock_new_inode(tree->inode);
77 mapping = tree->inode->i_mapping;
78 page = read_mapping_page(mapping, 0, NULL);
83 head = (struct hfs_btree_header_rec *)(kmap(page) + sizeof(struct hfs_bnode_desc));
84 tree->root = be32_to_cpu(head->root);
85 tree->leaf_count = be32_to_cpu(head->leaf_count);
86 tree->leaf_head = be32_to_cpu(head->leaf_head);
87 tree->leaf_tail = be32_to_cpu(head->leaf_tail);
88 tree->node_count = be32_to_cpu(head->node_count);
89 tree->free_nodes = be32_to_cpu(head->free_nodes);
90 tree->attributes = be32_to_cpu(head->attributes);
91 tree->node_size = be16_to_cpu(head->node_size);
92 tree->max_key_len = be16_to_cpu(head->max_key_len);
93 tree->depth = be16_to_cpu(head->depth);
95 size = tree->node_size;
96 if (!is_power_of_2(size))
98 if (!tree->node_count)
102 if (tree->max_key_len != HFS_MAX_EXT_KEYLEN) {
103 pr_err("invalid extent max_key_len %d\n",
109 if (tree->max_key_len != HFS_MAX_CAT_KEYLEN) {
110 pr_err("invalid catalog max_key_len %d\n",
119 tree->node_size_shift = ffs(size) - 1;
120 tree->pages_per_bnode = (tree->node_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
129 tree->inode->i_mapping->a_ops = &hfs_aops;
136 /* Release resources used by a btree */
137 void hfs_btree_close(struct hfs_btree *tree)
139 struct hfs_bnode *node;
145 for (i = 0; i < NODE_HASH_SIZE; i++) {
146 while ((node = tree->node_hash[i])) {
147 tree->node_hash[i] = node->next_hash;
148 if (atomic_read(&node->refcnt))
149 pr_err("node %d:%d still has %d user(s)!\n",
150 node->tree->cnid, node->this,
151 atomic_read(&node->refcnt));
152 hfs_bnode_free(node);
153 tree->node_hash_cnt--;
160 void hfs_btree_write(struct hfs_btree *tree)
162 struct hfs_btree_header_rec *head;
163 struct hfs_bnode *node;
166 node = hfs_bnode_find(tree, 0);
170 /* Load the header */
171 page = node->page[0];
172 head = (struct hfs_btree_header_rec *)(kmap(page) + sizeof(struct hfs_bnode_desc));
174 head->root = cpu_to_be32(tree->root);
175 head->leaf_count = cpu_to_be32(tree->leaf_count);
176 head->leaf_head = cpu_to_be32(tree->leaf_head);
177 head->leaf_tail = cpu_to_be32(tree->leaf_tail);
178 head->node_count = cpu_to_be32(tree->node_count);
179 head->free_nodes = cpu_to_be32(tree->free_nodes);
180 head->attributes = cpu_to_be32(tree->attributes);
181 head->depth = cpu_to_be16(tree->depth);
184 set_page_dirty(page);
188 static struct hfs_bnode *hfs_bmap_new_bmap(struct hfs_bnode *prev, u32 idx)
190 struct hfs_btree *tree = prev->tree;
191 struct hfs_bnode *node;
192 struct hfs_bnode_desc desc;
195 node = hfs_bnode_create(tree, idx);
199 if (!tree->free_nodes)
203 cnid = cpu_to_be32(idx);
204 hfs_bnode_write(prev, &cnid, offsetof(struct hfs_bnode_desc, next), 4);
206 node->type = HFS_NODE_MAP;
208 hfs_bnode_clear(node, 0, tree->node_size);
211 desc.type = HFS_NODE_MAP;
213 desc.num_recs = cpu_to_be16(1);
215 hfs_bnode_write(node, &desc, 0, sizeof(desc));
216 hfs_bnode_write_u16(node, 14, 0x8000);
217 hfs_bnode_write_u16(node, tree->node_size - 2, 14);
218 hfs_bnode_write_u16(node, tree->node_size - 4, tree->node_size - 6);
223 /* Make sure @tree has enough space for the @rsvd_nodes */
224 int hfs_bmap_reserve(struct hfs_btree *tree, int rsvd_nodes)
226 struct inode *inode = tree->inode;
230 while (tree->free_nodes < rsvd_nodes) {
231 res = hfs_extend_file(inode);
234 HFS_I(inode)->phys_size = inode->i_size =
235 (loff_t)HFS_I(inode)->alloc_blocks *
236 HFS_SB(tree->sb)->alloc_blksz;
237 HFS_I(inode)->fs_blocks = inode->i_size >>
238 tree->sb->s_blocksize_bits;
239 inode_set_bytes(inode, inode->i_size);
240 count = inode->i_size >> tree->node_size_shift;
241 tree->free_nodes += count - tree->node_count;
242 tree->node_count = count;
247 struct hfs_bnode *hfs_bmap_alloc(struct hfs_btree *tree)
249 struct hfs_bnode *node, *next_node;
258 res = hfs_bmap_reserve(tree, 1);
263 node = hfs_bnode_find(tree, nidx);
266 len = hfs_brec_lenoff(node, 2, &off16);
269 off += node->page_offset;
270 pagep = node->page + (off >> PAGE_SHIFT);
279 for (m = 0x80, i = 0; i < 8; m >>= 1, i++) {
283 set_page_dirty(*pagep);
286 mark_inode_dirty(tree->inode);
288 return hfs_bnode_create(tree, idx);
292 if (++off >= PAGE_SIZE) {
294 data = kmap(*++pagep);
303 printk(KERN_DEBUG "create new bmap node...\n");
304 next_node = hfs_bmap_new_bmap(node, idx);
306 next_node = hfs_bnode_find(tree, nidx);
308 if (IS_ERR(next_node))
312 len = hfs_brec_lenoff(node, 0, &off16);
314 off += node->page_offset;
315 pagep = node->page + (off >> PAGE_SHIFT);
321 void hfs_bmap_free(struct hfs_bnode *node)
323 struct hfs_btree *tree;
329 hfs_dbg(BNODE_MOD, "btree_free_node: %u\n", node->this);
332 node = hfs_bnode_find(tree, 0);
335 len = hfs_brec_lenoff(node, 2, &off);
336 while (nidx >= len * 8) {
343 pr_crit("unable to free bnode %u. bmap not found!\n",
349 node = hfs_bnode_find(tree, i);
352 if (node->type != HFS_NODE_MAP) {
354 pr_crit("invalid bmap found! (%u,%d)\n",
355 node->this, node->type);
359 len = hfs_brec_lenoff(node, 0, &off);
361 off += node->page_offset + nidx / 8;
362 page = node->page[off >> PAGE_SHIFT];
365 m = 1 << (~nidx & 7);
368 pr_crit("trying to free free bnode %u(%d)\n",
369 node->this, node->type);
374 data[off] = byte & ~m;
375 set_page_dirty(page);
379 mark_inode_dirty(tree->inode);