blob: 24cf6fc4302122366ed444c7f5f3344b7cae7382 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/hfs/btree.c
3 *
4 * Copyright (C) 2001
5 * Brad Boyer (flar@allandria.com)
6 * (C) 2003 Ardis Technologies <roman@ardistech.com>
7 *
8 * Handle opening/closing btree
9 */
10
11#include <linux/pagemap.h>
Vignesh Babu BMe1b5c1d2007-05-08 00:24:30 -070012#include <linux/log2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
14#include "btree.h"
15
16/* Get a reference to a B*Tree and do some initial checks */
17struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id, btree_keycmp keycmp)
18{
19 struct hfs_btree *tree;
20 struct hfs_btree_header_rec *head;
21 struct address_space *mapping;
22 struct page *page;
23 unsigned int size;
24
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -070025 tree = kzalloc(sizeof(*tree), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 if (!tree)
27 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29 init_MUTEX(&tree->tree_lock);
30 spin_lock_init(&tree->hash_lock);
31 /* Set the correct compare function */
32 tree->sb = sb;
33 tree->cnid = id;
34 tree->keycmp = keycmp;
35
36 tree->inode = iget_locked(sb, id);
37 if (!tree->inode)
38 goto free_tree;
Eric Sesterhenn4d4ef9a2006-03-26 18:26:51 +020039 BUG_ON(!(tree->inode->i_state & I_NEW));
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 {
41 struct hfs_mdb *mdb = HFS_SB(sb)->mdb;
42 HFS_I(tree->inode)->flags = 0;
43 init_MUTEX(&HFS_I(tree->inode)->extents_lock);
44 switch (id) {
45 case HFS_EXT_CNID:
46 hfs_inode_read_fork(tree->inode, mdb->drXTExtRec, mdb->drXTFlSize,
47 mdb->drXTFlSize, be32_to_cpu(mdb->drXTClpSiz));
48 tree->inode->i_mapping->a_ops = &hfs_btree_aops;
49 break;
50 case HFS_CAT_CNID:
51 hfs_inode_read_fork(tree->inode, mdb->drCTExtRec, mdb->drCTFlSize,
52 mdb->drCTFlSize, be32_to_cpu(mdb->drCTClpSiz));
53 tree->inode->i_mapping->a_ops = &hfs_btree_aops;
54 break;
55 default:
56 BUG();
57 }
58 }
59 unlock_new_inode(tree->inode);
60
61 mapping = tree->inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -070062 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 if (IS_ERR(page))
Eric Sandeen46a39c12008-01-17 15:21:09 -080064 goto free_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 /* Load the header */
67 head = (struct hfs_btree_header_rec *)(kmap(page) + sizeof(struct hfs_bnode_desc));
68 tree->root = be32_to_cpu(head->root);
69 tree->leaf_count = be32_to_cpu(head->leaf_count);
70 tree->leaf_head = be32_to_cpu(head->leaf_head);
71 tree->leaf_tail = be32_to_cpu(head->leaf_tail);
72 tree->node_count = be32_to_cpu(head->node_count);
73 tree->free_nodes = be32_to_cpu(head->free_nodes);
74 tree->attributes = be32_to_cpu(head->attributes);
75 tree->node_size = be16_to_cpu(head->node_size);
76 tree->max_key_len = be16_to_cpu(head->max_key_len);
77 tree->depth = be16_to_cpu(head->depth);
78
79 size = tree->node_size;
Vignesh Babu BMe1b5c1d2007-05-08 00:24:30 -070080 if (!is_power_of_2(size))
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 goto fail_page;
82 if (!tree->node_count)
83 goto fail_page;
Eric Sandeen55581d02008-02-06 01:37:10 -080084 switch (id) {
85 case HFS_EXT_CNID:
86 if (tree->max_key_len != HFS_MAX_EXT_KEYLEN) {
87 printk(KERN_ERR "hfs: invalid extent max_key_len %d\n",
88 tree->max_key_len);
89 goto fail_page;
90 }
91 break;
92 case HFS_CAT_CNID:
93 if (tree->max_key_len != HFS_MAX_CAT_KEYLEN) {
94 printk(KERN_ERR "hfs: invalid catalog max_key_len %d\n",
95 tree->max_key_len);
96 goto fail_page;
97 }
98 break;
99 default:
100 BUG();
Eric Sandeencf059462008-01-08 15:33:20 -0800101 }
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 tree->node_size_shift = ffs(size) - 1;
104 tree->pages_per_bnode = (tree->node_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
105
106 kunmap(page);
107 page_cache_release(page);
108 return tree;
109
Eric Sandeen46a39c12008-01-17 15:21:09 -0800110fail_page:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 page_cache_release(page);
Eric Sandeen46a39c12008-01-17 15:21:09 -0800112free_inode:
Eric Sandeencf059462008-01-08 15:33:20 -0800113 tree->inode->i_mapping->a_ops = &hfs_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 iput(tree->inode);
Eric Sandeen46a39c12008-01-17 15:21:09 -0800115free_tree:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 kfree(tree);
117 return NULL;
118}
119
120/* Release resources used by a btree */
121void hfs_btree_close(struct hfs_btree *tree)
122{
123 struct hfs_bnode *node;
124 int i;
125
126 if (!tree)
127 return;
128
129 for (i = 0; i < NODE_HASH_SIZE; i++) {
130 while ((node = tree->node_hash[i])) {
131 tree->node_hash[i] = node->next_hash;
132 if (atomic_read(&node->refcnt))
Roman Zippel7cf3cc32006-01-18 17:43:07 -0800133 printk(KERN_ERR "hfs: node %d:%d still has %d user(s)!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 node->tree->cnid, node->this, atomic_read(&node->refcnt));
135 hfs_bnode_free(node);
136 tree->node_hash_cnt--;
137 }
138 }
139 iput(tree->inode);
140 kfree(tree);
141}
142
143void hfs_btree_write(struct hfs_btree *tree)
144{
145 struct hfs_btree_header_rec *head;
146 struct hfs_bnode *node;
147 struct page *page;
148
149 node = hfs_bnode_find(tree, 0);
150 if (IS_ERR(node))
151 /* panic? */
152 return;
153 /* Load the header */
154 page = node->page[0];
155 head = (struct hfs_btree_header_rec *)(kmap(page) + sizeof(struct hfs_bnode_desc));
156
157 head->root = cpu_to_be32(tree->root);
158 head->leaf_count = cpu_to_be32(tree->leaf_count);
159 head->leaf_head = cpu_to_be32(tree->leaf_head);
160 head->leaf_tail = cpu_to_be32(tree->leaf_tail);
161 head->node_count = cpu_to_be32(tree->node_count);
162 head->free_nodes = cpu_to_be32(tree->free_nodes);
163 head->attributes = cpu_to_be32(tree->attributes);
164 head->depth = cpu_to_be16(tree->depth);
165
166 kunmap(page);
167 set_page_dirty(page);
168 hfs_bnode_put(node);
169}
170
171static struct hfs_bnode *hfs_bmap_new_bmap(struct hfs_bnode *prev, u32 idx)
172{
173 struct hfs_btree *tree = prev->tree;
174 struct hfs_bnode *node;
175 struct hfs_bnode_desc desc;
176 __be32 cnid;
177
178 node = hfs_bnode_create(tree, idx);
179 if (IS_ERR(node))
180 return node;
181
182 if (!tree->free_nodes)
183 panic("FIXME!!!");
184 tree->free_nodes--;
185 prev->next = idx;
186 cnid = cpu_to_be32(idx);
187 hfs_bnode_write(prev, &cnid, offsetof(struct hfs_bnode_desc, next), 4);
188
189 node->type = HFS_NODE_MAP;
190 node->num_recs = 1;
191 hfs_bnode_clear(node, 0, tree->node_size);
192 desc.next = 0;
193 desc.prev = 0;
194 desc.type = HFS_NODE_MAP;
195 desc.height = 0;
196 desc.num_recs = cpu_to_be16(1);
197 desc.reserved = 0;
198 hfs_bnode_write(node, &desc, 0, sizeof(desc));
199 hfs_bnode_write_u16(node, 14, 0x8000);
200 hfs_bnode_write_u16(node, tree->node_size - 2, 14);
201 hfs_bnode_write_u16(node, tree->node_size - 4, tree->node_size - 6);
202
203 return node;
204}
205
206struct hfs_bnode *hfs_bmap_alloc(struct hfs_btree *tree)
207{
208 struct hfs_bnode *node, *next_node;
209 struct page **pagep;
210 u32 nidx, idx;
211 u16 off, len;
212 u8 *data, byte, m;
213 int i;
214
215 while (!tree->free_nodes) {
216 struct inode *inode = tree->inode;
217 u32 count;
218 int res;
219
220 res = hfs_extend_file(inode);
221 if (res)
222 return ERR_PTR(res);
223 HFS_I(inode)->phys_size = inode->i_size =
224 (loff_t)HFS_I(inode)->alloc_blocks *
225 HFS_SB(tree->sb)->alloc_blksz;
226 HFS_I(inode)->fs_blocks = inode->i_size >>
227 tree->sb->s_blocksize_bits;
228 inode_set_bytes(inode, inode->i_size);
229 count = inode->i_size >> tree->node_size_shift;
230 tree->free_nodes = count - tree->node_count;
231 tree->node_count = count;
232 }
233
234 nidx = 0;
235 node = hfs_bnode_find(tree, nidx);
236 if (IS_ERR(node))
237 return node;
238 len = hfs_brec_lenoff(node, 2, &off);
239
240 off += node->page_offset;
241 pagep = node->page + (off >> PAGE_CACHE_SHIFT);
242 data = kmap(*pagep);
243 off &= ~PAGE_CACHE_MASK;
244 idx = 0;
245
246 for (;;) {
247 while (len) {
248 byte = data[off];
249 if (byte != 0xff) {
250 for (m = 0x80, i = 0; i < 8; m >>= 1, i++) {
251 if (!(byte & m)) {
252 idx += i;
253 data[off] |= m;
254 set_page_dirty(*pagep);
255 kunmap(*pagep);
256 tree->free_nodes--;
257 mark_inode_dirty(tree->inode);
258 hfs_bnode_put(node);
259 return hfs_bnode_create(tree, idx);
260 }
261 }
262 }
263 if (++off >= PAGE_CACHE_SIZE) {
264 kunmap(*pagep);
265 data = kmap(*++pagep);
266 off = 0;
267 }
268 idx += 8;
269 len--;
270 }
271 kunmap(*pagep);
272 nidx = node->next;
273 if (!nidx) {
Roman Zippel7cf3cc32006-01-18 17:43:07 -0800274 printk(KERN_DEBUG "hfs: create new bmap node...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 next_node = hfs_bmap_new_bmap(node, idx);
276 } else
277 next_node = hfs_bnode_find(tree, nidx);
278 hfs_bnode_put(node);
279 if (IS_ERR(next_node))
280 return next_node;
281 node = next_node;
282
283 len = hfs_brec_lenoff(node, 0, &off);
284 off += node->page_offset;
285 pagep = node->page + (off >> PAGE_CACHE_SHIFT);
286 data = kmap(*pagep);
287 off &= ~PAGE_CACHE_MASK;
288 }
289}
290
291void hfs_bmap_free(struct hfs_bnode *node)
292{
293 struct hfs_btree *tree;
294 struct page *page;
295 u16 off, len;
296 u32 nidx;
297 u8 *data, byte, m;
298
299 dprint(DBG_BNODE_MOD, "btree_free_node: %u\n", node->this);
300 tree = node->tree;
301 nidx = node->this;
302 node = hfs_bnode_find(tree, 0);
303 if (IS_ERR(node))
304 return;
305 len = hfs_brec_lenoff(node, 2, &off);
306 while (nidx >= len * 8) {
307 u32 i;
308
309 nidx -= len * 8;
310 i = node->next;
311 hfs_bnode_put(node);
312 if (!i) {
313 /* panic */;
Roman Zippel7cf3cc32006-01-18 17:43:07 -0800314 printk(KERN_CRIT "hfs: unable to free bnode %u. bmap not found!\n", node->this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return;
316 }
317 node = hfs_bnode_find(tree, i);
318 if (IS_ERR(node))
319 return;
320 if (node->type != HFS_NODE_MAP) {
321 /* panic */;
Roman Zippel7cf3cc32006-01-18 17:43:07 -0800322 printk(KERN_CRIT "hfs: invalid bmap found! (%u,%d)\n", node->this, node->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 hfs_bnode_put(node);
324 return;
325 }
326 len = hfs_brec_lenoff(node, 0, &off);
327 }
328 off += node->page_offset + nidx / 8;
329 page = node->page[off >> PAGE_CACHE_SHIFT];
330 data = kmap(page);
331 off &= ~PAGE_CACHE_MASK;
332 m = 1 << (~nidx & 7);
333 byte = data[off];
334 if (!(byte & m)) {
Roman Zippel7cf3cc32006-01-18 17:43:07 -0800335 printk(KERN_CRIT "hfs: trying to free free bnode %u(%d)\n", node->this, node->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 kunmap(page);
337 hfs_bnode_put(node);
338 return;
339 }
340 data[off] = byte & ~m;
341 set_page_dirty(page);
342 kunmap(page);
343 hfs_bnode_put(node);
344 tree->free_nodes++;
345 mark_inode_dirty(tree->inode);
346}